validate pointer that was returned from malloc

K

Keith Thompson

Eric Sosman said:
That's not very specific, is it?

I posed the question because a core dump is usually
the best and most desirable outcome in a case like this.
Consider: There's not much corrective action you can
safely take, because there's no reason to believe the
bad pointer you've detected is the first bad pointer
the program has used. It may well have scribbled all
over memory before you had a chance to notice, and it
would be perilous to proceed on the basis of information
already known to be suspect. All you know is that the
program has gone off the rails, and badly -- a hard stop
will at least prevent the amok program from doing further
damage to disk files and the like, and a hard stop with
a core dump to facilitate post-mortem analysis is usually
welcome.

Agreed. In fact, the best corrective action you can take is to fix
the bug that caused the bad pointer in the first place. A core dump
is specifically intended to help you do that.
 
R

Raymond Martineau

This is still not guaranteed.

#include <stdlib.h>
#include <my_wrappers.h>

void foo(void)
{
char *ptr = my_malloc(5) /* allocated memory at address 0x1000 */
free(ptr); /* Oops, the wrong free was used */

If you want to violate standards, you can define free() as a modified
wrapper to call my_free() instead. While it might work on some compilers,
it's not something that should be used for portable code.
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top