I’m having a issue getting the size of a struct pointer after allocating the memory using malloc or realloc. I’ve worked around this by keeping track of the memory in a separate counter, but I would like to know if this is a bug or if there is a way to properly query the size of a struct pointer.
Sample code demonstrates that no matter how much memory I allocate to the struct pointer it always returns 4 when querying using the sizeof() method.
typedef struct {
int modelID;
int bufferPosition;
int bufferSize;
} Model;
Model *models = malloc(10000 * sizeof(Model));
NSLog(@"sizeof(models) = %lu", sizeof(models)); //this prints: sizeof(models) = 4
If I understand you correctly you want to get at the size of the allocated buffer.
sizeofif the wrong way to go since it is evaluated at compile time. The size of the buffer is a runtime concept.You would need a way to query you C library to return the allocation size for the pointer to the buffer.
Some systems have a way to get that kind of information, for instance
malloc_sizeon Mac OS.