views:

1537

answers:

10

Is there a way in c to find out the size of dynamically allocated memory?
For e.g., Suppose I say
char* p = malloc(sizeof(char)*100);
Now is there a way to find out the size of memory associated with p?

+2  A: 

No, the C runtime library does not provide such a function.

Some libraries may provide platform- or compiler-specific functions that can get this information, but generally the way to keep track of this information is in another integer variable.

Greg Hewgill
+1  A: 

I would expect this to be implementation dependent.
If you got the header data structure, you could cast it back on the pointer and get the size.

nik
Which header data structure ?
nos
@nos, if an implementation carved out the required size along with a mem-manager header returning the location after the header as the size-allocated memory block. Such a header is likely to store the size itself. If the header typedef was known, the allocated pointer could be moved back to access the header and its field. In such an implementation the memory-manager would itself implement the `free` operation likewise for low-accounting-maintenance (and high-risk).
nik
Depending on how the memory-manager implements accounting for the `free` operation, it may-or-may-not be feasible to derive the size from the pointer (knowledge of the implementation is however necessary). Some implementations may however choose to give this primitive in the api (in which case internal knowledge will not be required).
nik
A: 

No, there isn't.

Electro
A: 

If you use malloc then you can not get the size.

In the other hand, if you use OS API to dynamically allocate memory, like Windows heap functions, then it's possible to do that.

Nick D
+6  A: 

There is no standard way to find this information. However, some implementations provide functions like msize to do this. For example:

Keep in mind though, that malloc will allocate a minimum of the size requested, so you should check if msize variant for your implementation actually returns the size of the object or the memory actually allocated on the heap.

ars
+8  A: 

comp.lang.c FAQ list · Question 7.27 -

Q. So can I query the malloc package to find out how big an allocated block is?

A. Unfortunately, there is no standard or portable way. (Some compilers provide nonstandard extensions.) If you need to know, you'll have to keep track of it yourself. (See also question 7.28.)

Alex Reynolds
+1  A: 

Like everyone else already said: No there isn't.

Also, I would always avoid all the vendor-specific functions here, because when you find that you really need to use them, that's generally a sign that you're doing it wrong. You should either store the size separately, or not have to know it at all. Using vendor functions is the quickest way to lose one of the main beefits of writing in C, portability.

Enno
A: 

The C mentality is to provide the programmer with tools to help him with his job, not to provide abstractions which change the nature of his job. C also tries to avoid making things easier/safer if this happens at the expense of the performance limit.

Certain things you might like to do with a region of memory only require the location of the start of the region. Such things include working with null-terminated strings, manipulating the first n bytes of the region (if the region is known to be at least this large), and so forth.

Basically, keeping track of the length of a region is extra work, and if C did it automatically, it would sometimes be doing it unnecessarily.

Many library functions (for instance fread()) require a pointer to the start of a region, and also the size of this region. If you need the size of a region, you must keep track of it.

Yes, malloc() implementations usually keep track of a region's size, but they may do this indirectly, or round it up to some value, or not keep it at all. Even if they support it, finding the size this way might be slow compared with keeping track of it yourself.

If you need a data structure that knows how big each region is, C can do that for you. Just use a struct that keeps track of how large the region is as well as a pointer to the region.

Artelius
A: 
manish
A: 

This code will probably work on most Windows installations:

template <class T>
int get_allocated_bytes(T* ptr)
{
 return *((int*)ptr-4);
}

template <class T>
int get_allocated_elements(T* ptr)
{
 return get_allocated_bytes(ptr)/sizeof(T);
}
H. Acker