Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

pointer's size

Status
Not open for further replies.

hughLg

Programmer
Feb 18, 2002
136
MY
when i declare a pointer like:

Code:
int *ip

is it possible to know the size allocated for this pointer at any time in run time? the sizeof keyword can't do this...
 
It depends on how you intend to use it. You could write your own malloc which could return the size of the block it allocated if the block was queried. However, if the pointer is just set to point to an arbitrary piece of data, it is not possible to find out how big that piece of data is.

If the pointer was pointing to the middle of an array, would you want to know how big the entire array was or just from the point you pointed to until the end of the array?

The only reason I can think of for wanting to do this is if you have a message that you wish to transmit and you don't know how long it is.
 
also... always, a pointer size is 4 bytes, at leasy on the systems I know. so sizeof(ip) will return most certanly, 4.
[red]Nosferatu[/red]
We are what we eat...
There's no such thing as free meal...
once stated: methane@personal.ro
 
There is a hack that you can probably use. This is very hardware and compiler specific so the code wont be portable. And it will work only if the pointer has not been moved around.

Hack
malloc() normally stores the size of the allocation one word before the start of the array. i.e if malloc() return you the address 0xc010 you have to read the address 0xc00c on a x86 machine.
You can use a debugger to find out the exact location where the size is stored and then use it in your program.

cheers
amit
crazy_indian@lycos.com

to bug is human to debug devine
 
I agree with ami123 the size could be stored just before the allocated data.
I also agree that this is really non portable. You should only use this for debugging or profiling purpose: the application should not rely on this.

Note that the size you found this way can be greater than the size passed to the malloc function: there is usually some rounding up to some alignment. The size of the memory block allocated by malloc(127) could be 128 bytes. In fact the size of the 'header' (where you are just looking for the size of the block) is also added. So malloc(127) could allocate a block of size 132 or 136 (or more), keep the first 4 or 8 (or more) bytes to store managment data (like size of block, address of the next allocated block and so on) and return you the address of the first non managment data byte.

To find where the size of the allocated block is stored with your malloc library, use a debugger or try something like this, changing size and offset and looking for a pattern:
Code:
int size = 128, offset = -1;
char *p = malloc(size);
printf("size ? : %d\n", ((int *)p)[offset]);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top