Operating System - HP-UX
1753499 Members
4297 Online
108794 Solutions
New Discussion юеВ

Size of memory block pointed by a pointer?how to get it?

 
SOLVED
Go to solution
Wkdunreal
Advisor

Size of memory block pointed by a pointer?how to get it?

Hi,

Am new to HP UX , is there a way to find out the size of memory allocated to a pointer on hp ux?

For example we can use the _msize() on windows to find the size of memory allocated to a pointer .

#include
#include

void main()
{

void *buffer;
buffer = malloc( 999 );
printf( "Size of block is %u bytes\n", _msize( buffer ) );

}

produces the output:
Size of block is 1000 bytes

Is there any way this can be done on HP UX too?

Thank you very much in advance.
Regards
8 REPLIES 8
Wkdunreal
Advisor

Re: Size of memory block pointed by a pointer?how to get it?

I had managed to get the function

malloc_usable_size(void*)

in malloc.h header file which does return the number of bytes to which a pointer i pointing, but it returns the

bytes allocated by malloc + some padding value.

It wont return the exact value allocated.

Reason why i ask this question is :

There is this millions of lines of code for which we dont know how many thousands of mallocs are called.

The easy way out could have been to create a Vector class type of object and maintain a size allocated by malloc by writing this in the wrapper for malloc..

But since the code was written years ago things aint that flexible .

and hence the reason :)

Thanks again :D
Regards
Dennis Handly
Acclaimed Contributor
Solution

Re: Size of memory block pointed by a pointer?how to get it?

>Size of memory block pointed by a pointer? how to get it?

There is no documented way to get it.

>in malloc.h header file

Where? What OS version?

>It wont return the exact value allocated.

This is close enough.

>we don't know how many thousands of mallocs are called.

You could use wdb's "info heap" command to give you what is currently allocated.
Wkdunreal
Advisor

Re: Size of memory block pointed by a pointer?how to get it?

Am using Unix on HP UX 11.31 ia64.

Coding is being done in C.

Thanks a lot for reply Dennis :)
Helped me a lot

Regards :D
Steven Schweda
Honored Contributor

Re: Size of memory block pointed by a pointer?how to get it?

> [...] but it returns the
> bytes allocated by malloc + some padding
> value.
>
> It wont return the exact value allocated.

Perhaps it returns the exact value
_allocated_, which may differ from the exact
value _requested_.

You could write your own malloc()+free(),
which could remember+forget more.
Wkdunreal
Advisor

Re: Size of memory block pointed by a pointer?how to get it?

Yes it is exactly as you have said.
More memory is allocated than what is requested.

Observation:

if 1*sizeof(int) is allocated it returns 28 bytes

if 4*sizeof(int) is allocated it returns 28

for bigger values of 100s , 1000s and 10000s of sizeof(int), it returned the exact value requested for + 12 bytes.

Thanks and Regards :)
Hein van den Heuvel
Honored Contributor

Re: Size of memory block pointed by a pointer?how to get it?

>> More memory is allocated than what is requested.

All malloc implementations have control area, before the returned address where the requested or allocated size is stored. Otherwise the 'free' would need to provide that.
The overhead is typically padded to make sure that the first databyte is aligned 'nicely' in memory. (16 bytes for many architectures)

The allocation is also rounded up.

>> if 1*sizeof(int) is allocated it returns 28 bytes
if 4*sizeof(int) is allocated it returns 28

Right. There is no point in doing a malloc for just 1 integer as it takes more space for the application to hold the pointer than it takes to hold the value.

>> for bigger values of 100s , 1000s and 10000s of sizeof(int), it returned the exact value requested for + 12 bytes.

That's probably because you asked for nice 'round (2-squared) numbers. Allocate something 'odd' and more fudge factor will appear.

The EXACT behavior depends on the malloc code used which on HP UX can be tweaked using the 'mallopt' call.

Check out the man page for mallopt, and be sure to check out 'mallinfo' while you are there as well.

Enjoy,
Hein.


Wkdunreal
Advisor

Re: Size of memory block pointed by a pointer?how to get it?

Wow The allocation algorithm was a surprise to me. It indeed is a great technique.

And yes ofcourse as I haggered the value of the number of sizeof(int), I got a variation of padded value from +8 to +16 or so.

Thanks a lot for your Valuable input Hein.
Helped a lot :)
Dennis Handly
Acclaimed Contributor

Re: Size of memory block pointed by a pointer?how to get it?

>The allocation algorithm was a surprise to me. It indeed is a great technique.

mallopt(3) also has a small block allocator that allocates lots of space for N blocks.

There are thread arenas and there is a new product for 11.31, MallocNG.

On Integrity, malloc returns 16 byte aligned, vs 8 for PA.