Velocity Reviews > Test if pointer points to allocated memory

# Test if pointer points to allocated memory

Andrew
Guest
Posts: n/a

 01-21-2004
Is there anyway to test if a pointer points to allocated memory or
not?
For example if I have a pointer such as char *p is there a standard
way to test whether an assignment such as the following has been
applied?
p = (char *) malloc(sizeof(char) * n);

Joona I Palaste
Guest
Posts: n/a

 01-21-2004
Andrew <(E-Mail Removed)> scribbled the following:
> Is there anyway to test if a pointer points to allocated memory or
> not?
> For example if I have a pointer such as char *p is there a standard
> way to test whether an assignment such as the following has been
> applied?
> p = (char *) malloc(sizeof(char) * n);

Not portably.

--
/-- Joona Palaste ((E-Mail Removed)) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Outside of a dog, a book is a man's best friend. Inside a dog, it's too dark
- Groucho Marx

pete
Guest
Posts: n/a

 01-21-2004
Andrew wrote:
>
> Is there anyway to test if a pointer points to allocated memory or
> not?
> For example if I have a pointer such as char *p is there a standard
> way to test whether an assignment such as the following has been
> applied?
> p = (char *) malloc(sizeof(char) * n);

If a pointer is uninitialized or has been freed
without being reassigned, then you can't evaluate the pointer.

If you know that a pointer value is either NULL or
points to memory, you can check for NULL.

I think you could just add another object to keep
track of if that line was executed.

int applied = 0;

p = (char *) malloc(sizeof(char) * n);
applied = 1;

I also suspect that you have a question about the
return value of malloc, but I'm not sure.

After a malloc assignment has been made,
you always need to check for NULL.

p = malloc(n * sizeof *p);
if (p == NULL) {
/* deal with it intelligently */
}

--
pete

Dan Pop
Guest
Posts: n/a

 01-21-2004
In <(E-Mail Removed) > http://www.velocityreviews.com/forums/(E-Mail Removed) (Andrew) writes:

>Is there anyway to test if a pointer points to allocated memory or
>not?
>For example if I have a pointer such as char *p is there a standard
>way to test whether an assignment such as the following has been
>applied?
>p = (char *) malloc(sizeof(char) * n);

If the pointer has not yet been initialised or if it points to memory
that has been already freed, you're not even allowed to evaluate it in
a portable C program.

In a well designed program, you don't need such a check: you should
know whether it is initialised or not. The alternative takes a lot of
programming discipline: initialise each pointer with NULL at the point of
definition and reset it to NULL as soon as the object it used to point to
no longer exists (or is about to disappear). You may find the
following macro useful for this purpose:

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

but it is far from solving the problem, because there may be other
pointers pointing into the block being free'd. But malloc and friends
are not the full story. Consider:

char *global;

void foo(void)
{
char buff[100];
global = buff;
...
}

You need a "global = NULL;" before returning from foo(), because the life
of buff ends at that point.

So, if you're *extremely* careful, you can always use p != NULL to tell
whether p is pointing to some object or not (bugs caused by omitting to
reset a pointer can be very difficult to track). IMHO, it's much easier
to avoid the need of such checks in the first place.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: (E-Mail Removed)

CBFalconer
Guest
Posts: n/a

 01-21-2004
Andrew wrote:
>
> Is there anyway to test if a pointer points to allocated memory or
> not?
> For example if I have a pointer such as char *p is there a standard
> way to test whether an assignment such as the following has been
> applied?
> p = (char *) malloc(sizeof(char) * n);

Don't cast malloc. It is unnecessary and hides errors.

execute "free(p);" and the program goes BOOM you can be fairly
sure that p was not malloced, or has already been freed. I'm sure
this is a great help and comfort to you.

--
Chuck F ((E-Mail Removed)) ((E-Mail Removed))
Available for consulting/temporary embedded and systems.

Fred L. Kleinschmidt
Guest
Posts: n/a

 01-21-2004

Andrew wrote:
>
> Is there anyway to test if a pointer points to allocated memory or
> not?
> For example if I have a pointer such as char *p is there a standard
> way to test whether an assignment such as the following has been
> applied?
> p = (char *) malloc(sizeof(char) * n);

Other answers to this post address the pointer being set to malloc'd
memory.
But even if you are very careful to initialize all pointers to NULL, and
re-set them to NULL when you free allocated memory, you can never be
sure a non-NULL pointer points to allocated memory - it may point to
some static or heap string:

static char hello[] = "Hello";
char *p = NULL;
....
p = hello;

If you later test p for non-NULL in order to determine whether to free
it, you will be in big trouble.
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Common User Interface Services
M/S 2R-94 (206)544-5225

Sean Kenwrick
Guest
Posts: n/a

 01-21-2004

"Andrew" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> Is there anyway to test if a pointer points to allocated memory or
> not?
> For example if I have a pointer such as char *p is there a standard
> way to test whether an assignment such as the following has been
> applied?
> p = (char *) malloc(sizeof(char) * n);

You could try realloc(char * ptr,size) to try and reallocate the memory the
pointer is referencing. Although it is not clear whether realloc() will fail
gracefully (E.g. by returning NULL) or whether it will crash your
application if it is not a valid pointer - I suppose this depends on the
implementation. Its a long shot but its your only hope....

Sean

CBFalconer
Guest
Posts: n/a

 01-22-2004
Sean Kenwrick wrote:
> "Andrew" <(E-Mail Removed)> wrote in message
>
> > Is there anyway to test if a pointer points to allocated memory
> > or not? For example if I have a pointer such as char *p is there
> > a standard way to test whether an assignment such as the
> > following has been applied?
> >
> > p = (char *) malloc(sizeof(char) * n);

>
> You could try realloc(char * ptr,size) to try and reallocate the
> memory the pointer is referencing. Although it is not clear whether
> realloc() will fail gracefully (E.g. by returning NULL) or ..snip..

It is clearly undefined behaviour leading to frisky nasal demons.

--
Chuck F ((E-Mail Removed)) ((E-Mail Removed))
Available for consulting/temporary embedded and systems.

E. Robert Tisdale
Guest
Posts: n/a

 01-22-2004
Andrew wrote:

> Is there anyway to test
> if a pointer points to allocated memory or not?
> For example, if I have a pointer such as char *p
> is there a standard way to test whether an assignment
> such as the following has been applied?

> char* p = (char*)malloc(sizeof(char)*n);

if (p > (char*)(&p)) {
// p probably points to a character in automatic storage
// (the program stack)
}
else {
// p probably points to static data or free storage
}

But, of course, the ANSI/ISO C standards do *not* specify this.

Jack Klein
Guest
Posts: n/a

 01-22-2004
On Wed, 21 Jan 2004 19:17:17 -0800, "E. Robert Tisdale"
<(E-Mail Removed)> wrote in comp.lang.c:

> Andrew wrote:
>
> > Is there anyway to test
> > if a pointer points to allocated memory or not?
> > For example, if I have a pointer such as char *p
> > is there a standard way to test whether an assignment
> > such as the following has been applied?

>
> > char* p = (char*)malloc(sizeof(char)*n);

>
> if (p > (char*)(&p)) {

Undefined behavior. p and &p are not pointers to the same object or
one past the same object or array.

> // p probably points to a character in automatic storage
> // (the program stack)

Not all processors have a stack. Even for those that do, you are
making the unwarranted and unproven assumption that "the stack"
resides at higher memory addresses than other areas. I know of
several architectures where processor hardware requires that the stack
be in low memory.

> }
> else {
> // p probably points to static data or free storage
> }

Or is uninitialized. Or null. Or points to allocated memory that has
been free.

> But, of course, the ANSI/ISO C standards do *not* specify this.

Neither does anyone with any sense, you included.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html