Skip to contentSkip to conversation

malloc and free

DISCUSSION Ankit Jain 3 participants 3 messages Dec 24, 2004 – Dec 25, 2004

lore
Share & tools
lore

Ankit Jain

author
#0 lore raw

hi

routine xyz uses malloc and free functions. it gives accurate and correct result if called once.

but if the function is called in a loop N number of times then probably it gives segmentation fault.

what is the reason? can any body guess or test code is needed?

thanks

ankit jain

________________________________________________________________________
Yahoo! Messenger - Communicate instantly..."Ping"
your friends today! Download Messenger Now
http://uk.messenger.yahoo.com/download/index.html
-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs

#1 lore raw
Quoted 10 lines · Ankit Jain

hi

routine xyz uses malloc and free functions. it gives accurate and correct result if called once.

but if the function is called in a loop N number of times then probably it gives segmentation fault.

what is the reason? can any body guess or test code is needed?

Probably test code is needed. But if you want a guess ... the "free" call contains an error that leads to a memory leak. If you call the routine once, that's no big deal, and the routine will appear to return "accurate and correct result". But if you call it a lot, memory consumption goes up past the point where the kernel can support it and a segfault results.

That's just a shot in the dark, though ... and even it assumes that N is a big number (thousands at least), not 10 or 20. Your use of "probably" does make it an appealing guess, though.

Really, though, you are posing the question, "What can go wrong with malloc() and free()?" Put that way, it is obvious that it is too vagure for a troubleshooting list. Let's see the code, as well as whatever is calling the code.

-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs

#2 lore raw

Hi,
I have read that the malloc-ed memory is not actually freed upon
calling free, but is still maintained in the processes' malloc pool of memory. So can that add to the memory leak attributing a segfault?
Please someone tell if I am wrong!!

Quoted 35 lines · Ray Olszewski
Quoted 10 lines · Ankit Jain

hi

routine xyz uses malloc and free functions. it gives accurate and correct result if called once.

but if the function is called in a loop N number of times then probably it gives segmentation fault.

what is the reason? can any body guess or test code is needed?

Probably test code is needed. But if you want a guess ... the "free" call contains an error that leads to a memory leak. If you call the routine once, that's no big deal, and the routine will appear to return "accurate and correct result". But if you call it a lot, memory consumption goes up past the point where the kernel can support it and a segfault results.

That's just a shot in the dark, though ... and even it assumes that N is a big number (thousands at least), not 10 or 20. Your use of "probably" does make it an appealing guess, though.

Really, though, you are posing the question, "What can go wrong with malloc() and free()?" Put that way, it is obvious that it is too vagure for a troubleshooting list. Let's see the code, as well as whatever is calling the code.

-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs

Signature · 3 lines
With regards,

Jagadeesh Bhaskar P