Re: is it possible to size a pointer's target?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Andrew Ross wrote:
>  
>  Hi Everyone,
>  
> I'm interested in whether it is possible to get the size of a variable a
> pointer is pointing at.
>  
> I have a function that accepts a char * pointer. This can either be stack or
> heap memory but more typically stack where I see it.
>  
> It would help me tremendously if I could figure out the size of the variable
> at the other end of the pointer. I can't think of a way to do this
> unfortunately.
>  
> strlen won't work as the variable is null when passed in
> sizeof(*ptr) returns 1, even though the memory is larger
>  
> sizeof(ptr) returns 4 (on my 32 bit machine) of course
>  
> Can anyone think of how to do this, or am I out of luck?
>   

This isn't a GCC question but I'll answer just the same.

The only way a function can know the length of an array pointed to by a
pointer is by the caller passing that information to the function.

Probably the best way if you want to clear up the clutter is to use a
struct which has both a pointer and length (maybe stored as an int).

Tom

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux