How to calculate the size of the pointer memory?

In one function, I wrote:

char *ab; ab=malloc(10); 

Then in another function I want to know the size of memory indicated by the pointer ab . Is there a way to find out that ab points to 10 characters of memory?

+7
source share
9 answers

This is a deep secret that only free () knows for sure. This is probably on your system, but completely implementation dependent.

A bit awkward, but if you want to keep everything together:

  typedef struct
 {// size of data followed by data (C only trick! NOT for C ++)
     int dimension;  // number of data elements
     int data [1];  // variable number of data elements
 } malloc_int_t;

 malloc_int_t * ab;

 int dimension = 10;
 ab = malloc (sizeof (* ab) + (dimension-1) * sizeof (int));
 ab-> dimension = dimension;

 ab-> data [n] // data access 

I changed the data type to int to make the code a more general template.

+3
source

No, you have no standard way to do this. You must pass the size of the pointer memory along with the pointer, this is a common solution.

those. instead

 void f(char* x) { //... } 

using

 void f(char *x, size_t length) { //.... } 

and in your code

  char *ab = malloc( 10 ); f( ab, 10 ); 
+7
source

You cannot (portable anyway). You must track the size yourself.

Some malloc implementations may provide you with an API to access this information, but there are no standard standards for this.

+2
source

There is no way you have to save the size of the allocated memory in another variable.

+1
source

No Unfortunately.

You need to pass the block size along with the pointer.

+1
source

Not.

Now, speaking of this, there are no portable hacks, but it is not safe to rely on them.

If you know with 100% certainty that the memory was allocated through malloc (), you can rewind the pointer to several bytes and check the "malloc node", which is used to track which parts of the memory were allocated and which were not. However, I cannot stress this - never depend on it.

+1
source

Size is what you passed to malloc , you can use a global variable or macro to remember it.

0
source

It is not possible to infer the size of the allocated memory from the pointer itself. Since ab is char * , sizeof(ab) same as sizeof(char *) , which obviously does not match the size of the allocated memory fragment.

Since you called malloc with the required size, you know what size. Skip this number with a pointer to a function that needs to know the size.

0
source

I had a structure and a char pointer pointing to its memory address. Therefore, associating this with your question, I wanted to find the size of the memory cell that he pointed to, for example, the size of the structure. So, it’s logical that you do, find the size of the object that the pointer creates. This worked for me:

  unsigned char * buffer= Library1Structure; int x=sizeof(Library1Structure); 

So, the x value tells me the size of the memory location that the pointer buffer points to.

0
source

All Articles