what can cause free to segfault

  • Thread starter Ramprasad A Padmanabhan
  • Start date
I

Irrwahn Grausewitz

Peter Pichler said:
Or *before* the block (e.g. past something else, seemingly completely
unrelated), probably even more likely and definitely more difficult to track
down. Believe, been there, done that :)

.... got the t-shirt, wore it out. ;-)
IMHO these are among the worst kinds of errors one can think of.
 
D

Dan Pop

In said:
Does that, then, suggest a macro wrapper for free() such as

#define FREE( ptr ) free(ptr); ptr=NULL;

If it looks like a function call, it must behave like a function call,
at the syntactical level, at least, so the proper definition is:

#define FREE(ptr) (free(ptr), ptr = NULL)

and, due to its limitations, ptr must not be an expression with side
effects or not evaluating to a modifiable lvalue.

The anal programmer may want to write (ptr) = NULL instead ;-)

If you're wondering what's wrong with your version, consider:

if (foo) FREE(p);

What is the value of p after this "statement" if foo evaluates to 0?

Dan
 
D

Dan Pop

In said:
... got the t-shirt, wore it out. ;-)
IMHO these are among the worst kinds of errors one can think of.

Therefore, they're worth paying the additional attention necessary to
avoid making them in the first place.

Dan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,093
Messages
2,570,613
Members
47,230
Latest member
RenaldoDut

Latest Threads

Top