How to find out the size of allocated memory of a pointer variable in c

I ran into some kind of problem, in which case you may like your ideas.

main() { char *p=NULL; p=(char *)malloc(2000 * sizeof(char)); printf("size of p = %d\n",sizeof (p)); } 

In this program, it prints 4 that (char *) value, but I need how many bytes allocated for that.

+2
c memory-management pointers malloc size
Aug 26 2018-11-11T00:
source share
6 answers

It is impossible to find out how much memory was allocated only with a pointer. do sizeof (p) will get the size of the pointer variable p , which it takes at compile time, and which is the size of the pointer. That is, the memory that the pointer variable takes to store the pointer variable p . Inside p , the original address of the memory block is stored.

As soon as you allocate some memory using malloc , it will return the original address of the memory block, but the end of the block will not be found from it, since there is no terminator for the block. You define the end of the block, so you need to identify it by any means, so keep it somewhere. Therefore, you need to save the length of the block somewhere to find out where the block pointed to by p ends.

Note. Although the memory allocation structure keeps track of allocated and unallocated blocks, therefore, we can know the allocated length of the memory block from these structures, but these structures are inaccessible for users to use if any library function does not provide them. Therefore, code using such a function is not portable (indicated @Rudy Velthuis). Therefore, it is best to monitor the structure.

+3
Aug 26 '11 at 8:41
source share

You can also implement a wrapper for malloc and add tags (for example, allocated size and other meta-information) for free before the pointer returned by malloc. This is actually the method by which the C ++ compiler loses objects with references to virtual classes. Here is one working example:

 #include <stdlib.h> #include <stdio.h> void * my_malloc(size_t s) { size_t * ret = malloc(sizeof(size_t) + s); *ret = s; return &ret[1]; } void my_free(void * ptr) { free( (size_t*)ptr - 1); } size_t allocated_size(void * ptr) { return ((size_t*)ptr)[-1]; } int main(int argc, const char ** argv) { int * array = my_malloc(sizeof(int) * 3); printf("%u\n", allocated_size(array)); my_free(array); return 0; } 

The advantage of this method over a structure with size and pointer

  struct pointer { size_t size; void *p; }; 

is that you only need to replace malloc and free calls. All other pointer operations do not require refactoring.

+3
Feb 10 '16 at 21:37
source share

You cannot use sizeof in this case, since p is a pointer, not an array, but since you allocate it, you already know:

 main() { size_t arr_size = 2000; char *p=NULL; p=malloc(arr_size * sizeof(char)); printf("size of p = %d\n",arr_size); } 

Change If malloc cannot allocate the size you need, it will not give you a pointer to a smaller buffer, but it will return NULL.

+1
Aug 26 '11 at 8:39
source share

You need to track it in a variable if you want to know it later:

 char *p = NULL; int sizeofp = 2000*sizeof(char); p = (char *)malloc(sizeofp); printf("size of p = %d\n",sizeofp); 
+1
Aug 26 2018-11-11T00:
source share

Although it is possible that some libraries will allow you to determine the size of the allocated buffer, this will not be a standard C function, and you should look at your library for your own documents for this.

However, if there are many places where you need to know the size of the allocated memory, the cleanest way to do this is to save the size next to the pointer. I.e:

 struct pointer { size_t size; void *p; }; 

Then each time you malloc pointer, you also write the size in the size field. However, the problem with this method is that you must point to a pointer every time you use it. If you were in C ++, I would suggest using template classes. However, in this case, it is not difficult, just create as many structures as you have. For example,

 struct charPtr { size_t size; char *p; }; struct intPtr { size_t size; int *p; }; struct objectPtr { size_t size; struct object *p; }; 

Given similar names, once you define a pointer, you don’t need extra effort (like casting) to access the array. Usage example:

 struct intPtr array; array.p = malloc(1000 * sizeof *array.p); array.size = array.p?1000:0; ... for (i = 0; i < array.size; ++i) printf("%s%d", i?" ":"", array.p[i]); printf("\n"); 
+1
Aug 26 '11 at 8:59
source share

There is no portable way, but for windows:

 #include <stdio.h> #include <malloc.h> #if defined( _MSC_VER ) || defined( __int64 ) /* for VisualC++ or MinGW/gcc */ #define howmanybytes(ptr) ((unsigned long)_msize(ptr)) #else #error no known way #endif int main() { char *x=malloc(1234); printf( "%lu", howmanybytes(x) ); return 0; } 
-one
Aug 26 2018-11-11T00:
source share



All Articles