size of a variable from a pointer

S

serrand

Hello all,

i wonder if we can know the size of a variable from a pointer :

static void *my_free_hook (void *ptr, const void *caller)
{
size_t sz;
/* bla bla bla */
/* HOW CAN I DETERMINE SIZE OF *ptr ? */
printf ("size of freed memory : %u", sz);
}

sizeof (ptr) is the size of a pointer (4 on my system)... and sizeof(*ptr) is the size of the first byte = 1 ...

all idea welcome ...

Xavier
 
A

Artie Gold

serrand said:
Hello all,

i wonder if we can know the size of a variable from a pointer :

static void *my_free_hook (void *ptr, const void *caller)
{
size_t sz;
/* bla bla bla */
/* HOW CAN I DETERMINE SIZE OF *ptr ? */
printf ("size of freed memory : %u", sz);
}

sizeof (ptr) is the size of a pointer (4 on my system)... and
sizeof(*ptr) is the size of the first byte = 1 ...

all idea welcome ...

Xavier

All right. What are you *really* trying to do?
....(and you can't dereference a pointer to void (i.e. void *) in any case...

HTH,
--ag
 
I

Ian Collins

serrand said:
Hello all,

i wonder if we can know the size of a variable from a pointer :

static void *my_free_hook (void *ptr, const void *caller)
{
size_t sz;
/* bla bla bla */
/* HOW CAN I DETERMINE SIZE OF *ptr ? */
printf ("size of freed memory : %u", sz);
}

sizeof (ptr) is the size of a pointer (4 on my system)... and
sizeof(*ptr) is the size of the first byte = 1 ...

all idea welcome ...

You can't, you gave to pass the size.
 
P

pete

serrand said:
Hello all,

i wonder if we can know the size of a variable from a pointer :

static void *my_free_hook (void *ptr, const void *caller)

static void *my_free_hook (void *ptr, const void *caller, size_t sz);
 
K

Keith Thompson

serrand said:
i wonder if we can know the size of a variable from a pointer :

static void *my_free_hook (void *ptr, const void *caller)
{
size_t sz;
/* bla bla bla */
/* HOW CAN I DETERMINE SIZE OF *ptr ? */
printf ("size of freed memory : %u", sz);
}

sizeof (ptr) is the size of a pointer (4 on my system)... and
sizeof(*ptr) is the size of the first byte = 1 ...

sizeof(*ptr) is a constraint violation; it's equivalent to
sizeof(void), and since void is an incomplete type, it has no size.
(I think gcc allows this as an extension.)

Given a void* pointer value, there's no direct way to determine the
size of the object it points to. free() is able to determine the
number of bytes that need to be released, but it does so in a manner
that's not specified by the standard, and there's no portable way for
you to get that information yourself.

If you want to know this, you need to keep track of it yourself.
 
S

serrand

pete said:
static void *my_free_hook (void *ptr, const void *caller, size_t sz);

in this case i can't modify the function call...
what's more, principle is : I DO NOT KNOW the size : it's what i want ;-)

i wanted to trace allocation and deallocation in order to check Sum(allocation) - Sum(deallocation) = 0 ...

Ok... i can modify all my types in order to put size of instance in the first field... not pretty nice...

thanks for the ideas...

xavier
 
M

Mark McIntyre

in this case i can't modify the function call...
what's more, principle is : I DO NOT KNOW the size : it's what i want ;-)

theres no way to do this in C, unless your'e lucky enough to know that
the pointer always points to memory terminated by a specific guard
byte or series of bytes, eg one or more '\0'.

Mark McIntyre
 
K

Kenneth Brody

serrand said:
in this case i can't modify the function call...
what's more, principle is : I DO NOT KNOW the size : it's what i want ;-)

i wanted to trace allocation and deallocation in order to check
Sum(allocation) - Sum(deallocation) = 0 ...

Ok... i can modify all my types in order to put size of instance in the
first field... not pretty nice...

thanks for the ideas...

You can modify your my_malloc_hook() function to save the size somewhere.
Perhaps some not-very-portable-but-probably-will-work-on-your-systems
method of malloc'ing sz+sizeof(size_t), storing the size at the start of
the buffer, and returning ptr+sizeof(size_t). Then, my_free_hook() can
look for the size at ((char *)ptr-sizeof(size_t)), and free that address.

There is probably a portable solution you could come up with using the
above as a starting point. (If you could guarantee proper alignment of
the returned pointer, it might become well-defined behavior.)

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:[email protected]>
 

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
474,262
Messages
2,571,056
Members
48,769
Latest member
Clifft

Latest Threads

Top