Learn why using a separate pointer is critical when dealing with dynamic memory allocation in C, and how it prevents errors in your code. --- This video is based on the question https://stackoverflow.com/q/67071133/ asked by the user 'JB P' ( https://stackoverflow.com/u/14673587/ ) and on the answer https://stackoverflow.com/a/67071556/ provided by the user 'Vlad from Moscow' ( https://stackoverflow.com/u/2877241/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions. Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: Why I cannot use allocated pointer in this case? Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/l... The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license. If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com. --- Understanding Why You Need to Use a Separate Pointer with Dynamic Memory in C When working with pointers and dynamic memory allocation in C, you might encounter an error stating: "pointer being freed was not allocated." This can be confusing, especially when you’re trying to manage memory effectively in your applications. In this guide, we will dive deeper into why this error occurs and how to correctly use pointers when allocating memory. The Problem: Pointer Mismanagement Example Scenario Let's consider the function ft_strjoin, which is designed to concatenate multiple strings with a specified separator. Here's a snippet of that function to give context: [[See Video to Reveal this Text or Code Snippet]] The Confusion In the code, read_head is initialized to string, which is the pointer that points to the memory allocated for the concatenated string. However, as you manipulate read_head throughout the function (for instance, by moving the pointer ahead to insert new strings), it eventually points to the end of the constructed string rather than the starting location of the allocated memory. This leads to a crucial problem: when you try to free read_head instead of string, the program fails with the message about freeing an unallocated pointer. The Solution: Using a Separate Pointer Function Correctness Instead of using just one pointer to manage your dynamically allocated memory, it's important to keep a separate pointer that holds the original address of the allocated memory. Here's a breakdown of why this is necessary: Preservation of the Original Pointer: By keeping string intact, you retain access to the allocated memory. Pointing with read_head: This pointer can be manipulated freely in your function to build your concatenated string, while string remains unchanged to allow proper memory management. Code Correction Here’s how you can correct your initial code: [[See Video to Reveal this Text or Code Snippet]] Conclusion Memory management in C can be tricky, especially with pointers. Always remember to: Use a separate pointer to manage dynamic allocations. Make sure you return a properly allocated pointer, as shown in the corrected example. By following these guidelines, you can avoid runtime errors, keeping your applications robust and error-free. Understanding this aspect of pointer and memory management is crucial for anyone working with C! If you have further questions about pointers or dynamic memory allocation, feel free to leave your comments below!