I recently realized the existence of void pointers. These are quite useful if the primitive type for a data storage class is not known. Though, I have a question as to whether I am using them correctly.
My storage class will allocate memory and keep track of the byte size of the individual pieces. The user then set and extract bytes via void * pointers.
The storage class then copies bytes to and from the storage memory as needed. Basically my own crude implementation of NSMutableArray/NSMutableData.
The user gets back a void * pointer and then casts this into whatever is needed. Yes, probably too much responsibility is given to the user.
Anyway, I am concerned about memory leaks. To return a value, I memcpy() the bytes into a newly created memory malloc(). But there is no corresponding free().
The user has to free the memory. But calling free after casting and dereferencing causes a crash. Does memory have to be freed in this case? See last for loop.
Thanks for reading, replying and hopefully a lesson in pointers.
My storage class will allocate memory and keep track of the byte size of the individual pieces. The user then set and extract bytes via void * pointers.
The storage class then copies bytes to and from the storage memory as needed. Basically my own crude implementation of NSMutableArray/NSMutableData.
The user gets back a void * pointer and then casts this into whatever is needed. Yes, probably too much responsibility is given to the user.
Anyway, I am concerned about memory leaks. To return a value, I memcpy() the bytes into a newly created memory malloc(). But there is no corresponding free().
The user has to free the memory. But calling free after casting and dereferencing causes a crash. Does memory have to be freed in this case? See last for loop.
Thanks for reading, replying and hopefully a lesson in pointers.
Code:
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSLog(@"Hello, World!");
//create memory for storage
int numberOfElements = 10;
int byteSizeOfElement = sizeof(double*); // or float, int, BOOL, ... determined by user, not storage class
void *storage = calloc(numberOfElements, byteSizeOfElement);
//set values
for (int i = 0; i < numberOfElements; i++)
{
double element = i*4.5;
memcpy(storage+i*byteSizeOfElement,&element,byteSizeOfElement);
}
//read out values
for (int i = 0; i < numberOfElements; i++)
{
void *pointerGivenByInstanceFunction = memcpy(malloc(byteSizeOfElement),storage+i*byteSizeOfElement,byteSizeOfElement);
double returnElement = *(double *)pointerGivenByInstanceFunction; // or float, int, BOOL, ...
NSLog(@"%g",returnElement);
free(pointerGivenByInstanceFunction);
}
NSLog(@"Down the rabbit hole");
//read out values bis
for (int i = 0; i < numberOfElements; i++)
{
//get element in one line as in: double returnElement = *(double *)[storageInstance elementAtIndex:i];
double returnElement = *(double *)memcpy(malloc(byteSizeOfElement),storage+i*byteSizeOfElement,byteSizeOfElement); //memory leak? Function that (will eventually) return void* pointer has no corresponding free()
NSLog(@"%g",returnElement);
free(&returnElement); //causes crash
}
NSLog(@"bye, World!");
[pool drain];
return 0;
}
Last edited: