C pointers 
Author Message
 C pointers

Hello everyone,

I have a small question:  assuming you have a function which gets a
pointer as parameter.

Is there a means to check and make sure the pointer has been
allocated?
Is there a means to check _how much space_ has been allocated to this
pointer?

This is about error-checking, not about conding rules, so requesting
the caller to give the size of the memory space allocated won't help.

thanks

  Serge

------------------------------------------------------------------------------
  The opinions expressed are mine and none other's
------------------------------------------------------------------------------
Serge Marelli

--



Sun, 09 Feb 2003 03:00:00 GMT  
 C pointers

Quote:

> I have a small question:  assuming you have a function which gets a
> pointer as parameter.

> Is there a means to check and make sure the pointer has been
> allocated?
> Is there a means to check _how much space_ has been allocated to this
> pointer?

No, and no. Alas.

Richard
--



Mon, 10 Feb 2003 03:00:00 GMT  
 C pointers

Quote:
> I have a small question:  assuming you have a function which gets a
> pointer as parameter.
> Is there a means to check and make sure the pointer has been
> allocated?
> Is there a means to check _how much space_ has been allocated to this
> pointer?

There is no portable way, and often no practical way.
The minimum reasonable check would be that the pointer is not null.

There are some debugging malloc libraries that have facilities
to help track down problems in the use of dynamic memory, and
some of these may provide exactly the functions you want.
--



Mon, 10 Feb 2003 03:00:00 GMT  
 C pointers

Quote:
> Is there a means to check and make sure the pointer has been
> allocated?
> Is there a means to check _how much space_ has been allocated to this
> pointer?

There is no Standard way.

--

 __ San Jose, CA, US / 37 20 N 121 53 W / ICQ16063900 / &tSftDotIotE
/  \ It is fatal to enter any war without the will to win it.
\__/ Douglas MacArthur
    Erik Max Francis' bookmarks / http://www.alcyone.com/max/links/
 A highly categorized list of Web links.
--



Mon, 10 Feb 2003 03:00:00 GMT  
 C pointers
Serge *Anonymous Coward* Marelli

Quote:
> I have a small question:  assuming you have a function which gets a
> pointer as parameter.

> Is there a means to check and make sure the pointer has been
> allocated?

No.

Quote:
> Is there a means to check _how much space_ has been allocated to this
> pointer?

No.

The above to answers apply to standard C.  Your compiler and C library of
course include implementations that may provide this information.  They will
not provide it through standard C library functions.  It is, of course,
possible in debug builds to redefine malloc via a macro to another function
that adds this information to a nice little convenient list and returns it
when asked.  But this assumes that you've got access to all source code that
allocates memory.  If you're using something like strdup'ed strings, or any
third party library that allocates memory, this won't work.  It's also a lot
of complexity for debug code, and may end up with its own bugs.... what a
mess!

Chris Smith
--



Mon, 10 Feb 2003 03:00:00 GMT  
 C pointers
On 23 Aug 2000 16:57:14 GMT,

Marelli) wrote in comp.lang.c.moderated:

Quote:
> Hello everyone,

> I have a small question:  assuming you have a function which gets a
> pointer as parameter.

> Is there a means to check and make sure the pointer has been
> allocated?

No, there is not.  About the only check you can really make on a
pointer is to compare it to NULL.  However if the pointer is
uninitialized, like in this example:

int *x; /* no initialization */
if (x == NULL)

....then even comparing its value to NULL is undefined behavior.

Quote:
> Is there a means to check _how much space_ has been allocated to this
> pointer?

No, if you call functions that need that information add a size_t
parameter to tell the function how much space has been allocated.

Quote:
> This is about error-checking, not about conding rules, so requesting
> the caller to give the size of the memory space allocated won't help.

> thanks

>   Serge

> ------------------------------------------------------------------------------
>   The opinions expressed are mine and none other's
> ------------------------------------------------------------------------------
> Serge Marelli


If you can't change the coding rules of the caller of the function
there really isn't anything you can do.  If you can change some things
but not the function interface, you can put a wrapper around malloc()
that records the amount of memory allocated to a pointer and pass
pointers to a function that returns this value, if the memory was
provided by the wrapper.

But if the pointer is uninitialized there is really nothing you can do
about it.

Jack Klein
--
Home: http://jackklein.home.att.net
--



Mon, 10 Feb 2003 03:00:00 GMT  
 C pointers

Quote:
> Is there a means to check and make sure the pointer has been
> allocated?

No. The only useful check you can carry out for a given pointer value
is comparing it to NULL. If it's not NULL, it can be anything: valid
pointer to malloc()ed storage, automatic storage, or to static
storage, or random garbage.

It's impossible to check that in a portable way. You need large
amounts of in-depth knowledge about the underyling operating system,
and access to OS-specific internal variables and data structures to
check if a pointer points into the 'arena' of the program or not.

The only working methods to ensure pointer values are always sane are
a rigorous coding discipline, and extensive use of checking tools like
lclint or purify / checker-gcc.

Quote:
> Is there a means to check _how much space_ has been allocated to this
> pointer?

By obvious extension of the first answer: no.

--

Even if all the snow were burnt, ashes would remain.
--



Mon, 10 Feb 2003 03:00:00 GMT  
 C pointers


Quote:
>Is there a means to check and make sure the pointer has been
>allocated?

No, unless the caller is meticulous about always passing a legal address
or a null pointer.

Quote:
>Is there a means to check _how much space_ has been allocated to this
>pointer?

No.

Francis Glassborow      Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
--



Mon, 10 Feb 2003 03:00:00 GMT  
 C pointers

Quote:
>I have a small question:  assuming you have a function which gets a
>pointer as parameter.

>Is there a means to check and make sure the pointer has been
>allocated?

No.  The function might have been called like this: func((char *)123)
in which case no space has been allocated to its argument.

Quote:
>Is there a means to check _how much space_ has been allocated to this
>pointer?

If the function's argument was a legal pointer p, the compiler has
allocated sizeof p bytes for it.

Quote:
>This is about error-checking, not about conding rules, so requesting
>the caller to give the size of the memory space allocated won't help.

I think you have poorly phrased your questions.  You're probably
interested to know how much space has been allocated to the place the
pointer points to, not to the pointer itself.  In this case, the answer
to all your questions is NO.  The function is not even able to distinguish
between a valid and an invalid pointer value.

Dan
--
Dan Pop
CERN, IT Division

Mail:  CERN - IT, Bat. 31 1-014, CH-1211 Geneve 23, Switzerland
--



Mon, 10 Feb 2003 03:00:00 GMT  
 C pointers

Quote:
> Hello everyone,
> I have a small question:  assuming you have a function which gets a
> pointer as parameter.
> Is there a means to check and make sure the pointer has been
> allocated?

No. You would have to compare the pointer to do so. However, you can
only (safely) compare an initialized pointer with NULL and other
pointers to the same object.

Quote:
> Is there a means to check _how much space_ has been allocated to this
> pointer?

No. For starters, you don't have a clue where the memory came from, so
you wouldn't know which interface to use. Also, all portable memory
allocation mechanisms (auto variables, globals, malloc()) lack such
a query interface.

Michiel Salters
--



Mon, 10 Feb 2003 03:00:00 GMT  
 C pointers

Quote:

> Hello everyone,

> I have a small question:  assuming you have a function which gets a
> pointer as parameter.

> Is there a means to check and make sure the pointer has been
> allocated?

In Standard C, the argument value could be any of the following:

1. A null pointer
2. A pointer to a static, extern or auto object or part of such an object
3. A pointer past the end of a static, extern or auto object
4. The result of a malloc/calloc/realloc call
5. A pointer into or just past the end of a block allocated by malloc etc.
6. Some illegal value, causing undefined + bad behavior.

In Standard C, all you can check is whether the pointer is NULL or not.
And your program might crash or worse if the pointer has some illegal
value.

Some implementations give you implementation defined functions that let
you check whether you can read or write through that pointer, and the
check won't have any side effects (including crashing) even if the pointer
has an illegal value. Storing through that pointer could still have {*filter*}
side effects if it is an illegal value or a pointer past the end of an
object.

And some implementations (especially debugging ones) could actually tell
you whether any pointer points to or inside a malloc block, and what the
size of that block is. But that is completely non standard.

Quote:
> Is there a means to check _how much space_ has been allocated to this
> pointer?

> This is about error-checking, not about conding rules, so requesting
> the caller to give the size of the memory space allocated won't help.

> thanks

>   Serge

> ------------------------------------------------------------------------------
>   The opinions expressed are mine and none other's
> ------------------------------------------------------------------------------
> Serge Marelli

> --


--



Mon, 10 Feb 2003 03:00:00 GMT  
 C pointers
Serge *Anonymous Coward* Marelli

Quote:
> I have a small question:  assuming you have a function which gets a
> pointer as parameter.

> Is there a means to check and make sure the pointer has been
> allocated?

Yes, check to see if it is null.
   if (ptr==0) {
      /* error trap */
   }
   else {
      /* normal code */
   }

Quote:
> Is there a means to check _how much space_ has been allocated to this
> pointer?

No.

Quote:
> This is about error-checking, not about conding rules, so requesting
> the caller to give the size of the memory space allocated won't help.

<sarcasm>
Always a fun problem.
</sarcasm>
--
Mark Gordon
Dyslexic C Programmer
At least the compiler makes sure I spell variable names consistently wrong
--



Mon, 10 Feb 2003 03:00:00 GMT  
 C pointers
Serge *Anonymous Coward* Marelli is of the opinion:

Quote:
>I have a small question:  assuming you have a function which gets a
>pointer as parameter.

>Is there a means to check and make sure the pointer has been
>allocated?

Not in general.  I guess for a system that has signals you could help check
by setting up a SIGSEGV handler and try writing to the pointer, but that
is likely to be very unpleasant if you're not supposed to be changing valid
data there...

Quote:
>Is there a means to check _how much space_ has been allocated to this
>pointer?

Not without knowing the specifics of the malloc() implementation, in which
case you could go into the storage data and see ow much you got.  The most
unportable of unportables, though.

Quote:
>This is about error-checking, not about conding rules, so requesting
>the caller to give the size of the memory space allocated won't help.

Pity.  You could write your own memory management routines that had a struct
which stored the size of each chunk along with the associated pointer to
memory, and the free routine could zero the pointer or the count when it was
freed.

--
-----------------------------------------------------------------------
#include <disclaimer.h>
Matthew Palmer

--



Mon, 10 Feb 2003 03:00:00 GMT  
 C pointers

Quote:

> However, you can
> only (safely) compare an initialized pointer with NULL and other
> pointers to the same object.

Do you mean the following isn't safe?

int i, j;
int *p = &i;  /* initialized pointer */
p != &j;      /* compare to a pointer to a different object */
--



Mon, 10 Feb 2003 03:00:00 GMT  
 C pointers

Quote:
>Serge *Anonymous Coward* Marelli


>> Is there a means to check and make sure the pointer has been
>> allocated?

>Yes, check to see if it is null.
>   if (ptr==0) {
>      /* error trap */
>   }
>   else {
>      /* normal code */
>   }

???

And if ptr is non-null, does that mean that it points to an allocated
memory block?  By what kind of logic?

Dan
--
Dan Pop
CERN, IT Division

Mail:  CERN - IT, Bat. 31 1-014, CH-1211 Geneve 23, Switzerland
--



Thu, 13 Feb 2003 03:00:00 GMT  
 
 [ 26 post ]  Go to page: [1] [2]

 Relevant Pages 

1. Newbie: separate big .cs file into small .cs files

2. Dereferencing f-pointers, arrays of f-pointers, pointers to f-pointers

3. How to show/call Form2.cs from Form1.cs ?

4. Include code in other Cs files

5. Reuse of cs files, namespace, arch advice pls

6. word - automatic numbering/bold/underline/italics

7. How to Generate .cs file at Runtime

8. newbe/cs student, need help w/ code

9. Serial.cs

10. Compile CS source code using ICodeCompiler

11. Two CS files (using namespaces)

12. My .cs files were deleted!?

 

 
Powered by phpBB® Forum Software