Getting the size of the data of a Pointer
Solution 1
That's not something you can do in C without maintaining the information yourself. You created the arrays, so you knew their sizes at one point, you just have to keep track of it on your own. You could create a data structure to help you, or just maintain the array and size information carefully without any data structure at all.
In addition, your code is using strlen()
to get the size of the string - make sure you remember that the size returned will not include the terminating null character ('\0'
); the in-memory size of a string constant is strlen(string) + 1
.
Solution 2
The pointer doesn't keep track of the number of objects that are allocated behind the (first) one it points to. That's because it can't: malloc()
just returns the address of the first element, nothing else.
Therefor there's no way to find out the size of a dynamically allocated array. You have to keep track of it for yourself.
Solution 3
sizeof
only knows the full size of the array if that's statically known at compile time. For a pointer, it will return the size of the memory address, i.e. 4 on 32-bit, 8 on 64-bit.
When you use pointers, it's up to you to know and keep track of the size of the data it points to. The language implementation won't do it for you like it would for arrays.
Solution 4
There is no standard way to do this. You have to keep the length of the allocated memory in another variable, or use a 'container' that keeps track for you.
Some platforms do have functions for finding the number of bytes in an allocation, e.g. Symbian as an AllocSize(ptr)
function.
Solution 5
A pointer only points to where the data begins. It doesn't know anything about the size of the data.
Comments
-
systemsfault over 3 years
I tried the following code in order to see how to get size of the data of a pointer:
#include <stdio.h> #include <stdlib.h> #include <string.h> int main(){ char *test_char_ptr = "This is just a test"; int *test_int_ptr = (int *) malloc(16*sizeof(int)); for(int i = 0; i<16; i++){ test_int_ptr[i] = i; } printf("%s\n",test_char_ptr); printf("Test char 1: %d\n", (int)sizeof test_char_ptr ); printf("Test char 2:%d\n", (int)sizeof *test_char_ptr ); printf("Test char 3: %d\n", (int)((strlen(test_char_ptr)+1) * sizeof(char))); printf("Test int 1:%d\n", (int)sizeof test_int_ptr ); printf("Test int 2:%d\n", (int)sizeof *test_int_ptr ); return EXIT_SUCCESS; }
And code's output is(On 32 bit gcc 4.3):
This is just a test Test char 1: 4 Test char 2:1 Test char 3: 20 Test int 1:4 Test int 2:4
I thought that sizeof(*test_char_ptr) would give me the sizeof the data inside of the *test_char_ptr. But instead it gave me 1 , which i think that is the sizeof char instead of the data. The same is for the test_int_ptr. Long story short, my question is how can i get the sizeof data inside a pointer or dynamically memory allocated array.
-
Carl Norum over 14 yearsYou should be able to edit your post and clean it up then; I'll modify my answer appropriately if/when that happens.
-
asveikau over 14 yearsI believe you misinterpreted my statement, or I have not stated it clearly. sizeof(* p) may be 1, but sizeof(p) is the same as sizeof(void*)
-
sbi over 14 years@asveikau: I can now see that I have misinterpreted you. Sorry.
-
Predrag Manojlovic over 9 yearsIt doesn't need to be that he allocated memory. There are some functions returning allocated memory and other subsystems releasing/modifying it. CreateDIBSection is example.
-
Carl Norum over 9 years@PredragManojlovic - what is
CreateDIBSection
and what does it have to do with this question? -
Predrag Manojlovic over 9 years@CarlNorum It is a system function which allocates buffer for you (at this point you do know the size). It belongs to a subsystem which will eventually modify buffer size or release it. So this is a case where you cannot know the size of buffer and you might need to have a way to determine size or validate buffer pointer. This is just a response to: "You created the arrays, so you knew their sizes" :)
-
DADi590 about 2 yearsWhat if it's a pointer to an array of specific size? For example,
int (*something)[3]; sizeof(*something);
. Will it work? I've got Cppcheck here complaining, but I'm not sure why, because I think it would work (known at compile-time, right?) -
Carl Norum about 2 yearsThat should work fine, yes. What's the error you get?