free char*: invalid next size (fast) [duplicate]

Your code is wrong. You are allocating space for a single pointer (malloc(sizeof(char*))), but no characters. You are overwriting your allocated space with all the strings, causing undefined behavior (in tihs particular case, corrupting malloc()‘s book-keeping data). You don’t need to allocate space for the pointer (res), it’s a local variable. You must allocate space … Read more

Are some allocators lazy?

On Linux, malloc requests memory with sbrk() or mmap() – either way, your address space is expanded immediately, but Linux does not assign actual pages of physical memory until the first write to the page in question. You can see the address space expansion in the VIRT column, while the actual, physical memory usage in … Read more

aligned malloc() in GCC?

Since the question was asked, a new function was standardized by C11: void *aligned_alloc(size_t alignment, size_t size); and it is available in glibc (not on windows as far as I know). It takes its arguments in the same order as memalign, the reverse of Microsoft’s _aligned_malloc, and uses the same free function as usual for … Read more

What is a Memory Heap?

Presumably you mean heap from a memory allocation point of view, not from a data structure point of view (the term has multiple meanings). A very simple explanation is that the heap is the portion of memory where dynamically allocated memory resides (i.e. memory allocated via malloc). Memory allocated from the heap will remain allocated … Read more