C Linked List Memory Usage (Possible memory leak)?

You don't say how you are checking memory usage, but I'm going to guess that you are using ps or something similar to see how much memory the OS has given the process Depending no your memory allocator, calling free may or may not return the memory to the OS. So even though you are calling free, you will not see the memory footprint decrease from the OS's point of view The allocator may keep a cache of memory that is given to it by the OS. A call to malloc will first look in this cache to see if it can find a big enough block and if so malloc can return without asking the OS for more memory.

If it can't find a big enough block malloc will ask the OS for more memory and add it to it's cache But free may simply add the memory back to the cache and never return it to the OS So, what you may be doing is seeing the allocators cache and not any memory leak.

You don't say how you are checking memory usage, but I'm going to guess that you are using ps or something similar to see how much memory the OS has given the process. Depending no your memory allocator, calling free may or may not return the memory to the OS. So even though you are calling free, you will not see the memory footprint decrease from the OS's point of view.

The allocator may keep a cache of memory that is given to it by the OS. A call to malloc will first look in this cache to see if it can find a big enough block and if so, malloc can return without asking the OS for more memory. If it can't find a big enough block, malloc will ask the OS for more memory and add it to it's cache.

But free may simply add the memory back to the cache and never return it to the OS. So, what you may be doing is seeing the allocators cache and not any memory leak.

Yes that could be the issue... I was looking at memory usage in Task Manager in WinXP, however I noticed the same thing running the code under Linux too. As you said, it depends on the allocator, sometimes free may not free memory at that specific point in time I guess. Thanks.

– user287400 Mar 5 '10 at 20:28 @AlexMcNeil: It's very common for a process not to return unused memory to the OS while it's running, and with modern memory management there usually isn't any reason to. If the memory footprint goes up over time, while the program isn't handling more data, there's likely a leak. If the memory footprint stays steady while the program gets rid of data, that's not evidence of a leak.

– David Thornley Mar 5 '10 at 22:13.

As was mentioned, I would not trust the memory usage reported by the task manager as there are other factors beyond your control that impact it (how malloc/free are implemented, etc). One way you can test for memory leaks is by writing your own wrapper functions around the existing malloc and free functions similar to: void* my_malloc(size_t len) { void* ptr = malloc(len); printf("Allocated %u bytes at %p\n", len, ptr); return ptr; } void my_free(void* ptr) { printf("Freeing memory at %p\n", ptr); free(ptr); } Now, you will get a log of all memory that is dynamically allocated or freed. From here, it should be fairly obvious if you leak a block of memory (the more complex your program is, the longer your log will be and the more difficult this task will be).

All good suggestions... I think the main issue was bad use of strncat and also free depending on implementation/allocator. Thanks! – user287400 Mar 6 '10 at 12:12.

Your program contains incorrect argv in main, incorrect usage of strncat, and strange memory allocation. Some of these should of shown up as warnings. The argv is a non-issue, but if the others showed up as warning, you needed to heed them.

Don't ignore warnings. These changes clean it up. The biggest thing was that you don't seem to have a good grasp on the NUL ('\0') character (different than NULL pointer) used to terminate C strings, and how that effects str(n)cat.

The mixed usage of str* functions with memory functions (*alloc/free) was likely part of the confusion. Be careful. #include ... int main( int argc, char* argv ) /* or int main(void) */ ... sz = 2; str = (char*) malloc(sz); /* allocate 2 bytes, shortest non-trivial C string */ assert(str!

= NULL); strncpy(str, "A", sz); /* copy 'A' and '\0' into the memory that str points to */ ... /* Add to list */ for (j = 0; j.

Correct regarding argv, I was sure I wrote argv... (I wrote this kind of quickly). And correct on strncat. However, I didn't think you could check memory that had been malloc'ed was NULL.

And I was under the impression casting malloc is a bad practice. Thanks! – user287400 Mar 6 '10 at 12:11.

These changes clean it up. The biggest thing was that you don't seem to have a good grasp on the NUL ('\0') character (different than NULL pointer) used to terminate C strings, and how that effects str(n)cat. The mixed usage of str* functions with memory functions (*alloc/free) was likely part of the confusion.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions