Ask your own question, for FREE!
Computer Science 16 Online
OpenStudy (anonymous):

Help! My program seems to have memory leaks! http://ideone.com/qgDc0 How do I resolve this problem, and prevent it in the future (in C) ?

OpenStudy (anonymous):

dont use C :D

OpenStudy (anonymous):

But I need to use C, since that's the language I'm learning atm :(

OpenStudy (shadowfiend):

engs = strconcat(translate_thousand(integer % 1000), padstring(strconcat(pows_to_engs[placescount], padstring(engs)))); strconcat mallocs, but you only free engs. The intermediary call to strconcat is never tracked, and therefore never freed. In general, valgrind: http://valgrind.org/ is often used for tracking down memory leaks. There are some decent UIs for it out there. Mac OS X also comes with Instruments, which has memory management helpers, though I'm not sure if they're aimed more to malloc/free or something else. Generally speaking, you want to architect your program so that the malloc and free for any given thing happens in the same function, otherwise you're begging for memory leaks. Your strconcat should take in a pre-malloced pointer to work with, such that you have both the malloc(s) and free(s) in the calling function. This way it's easy to check and make sure that you have balanced mallocs and frees.

OpenStudy (anonymous):

I've improved it a little bit, but I've also discovered another problem: when you input 0 it's calling free() more than it should. http://ideone.com/3P3os

OpenStudy (anonymous):

Wish I could learn how to properly manage memory in C.... I'm just a novice and already my code is exploding in several, colorful ways.

OpenStudy (anonymous):

Alright. I've nearly eliminated all memory leaks and bad free() calls: http://ideone.com/5pBaJ Anyone spot that last memory leak?

OpenStudy (anonymous):

http://ideone.com/9LMM5 No more memory leaks! :-D

OpenStudy (anonymous):

http://ideone.com/NHiTY

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!