I'm trying to use a lighter primitive array, because storing hundreds of thousands NSNumbers in a NSMutableDictionary is causing like 40-50MB memory consumption..
This is my code:
NSUInteger highestModelID = [self.externalDB intForQuery:@"SELECT `id` FROM `models` ORDER BY `id` DESC"];
CGFloat modelsPricesForCustomer[highestModelID];
memset(modelsPricesForCustomer, 0, highestModelID*sizeof(NSUInteger));
NSLog(@"%i", highestModelID);
NSLog(@"%lu", (sizeof modelsPricesForCustomer));
highestModelID
is 34078
, yet the size says it's 136312
. Do I allocate it wrong or read it wrong?
highestModelID
is the number of elements in modelsPricesForCustomer
, not its size. sizeof
, on the other hand, returns the size of the array in bytes, i.e. highestModelID*sizeof(CGFloat)
Since sizeof(CGFloat)
on your system is 4, the results are correct:
34078 * 4 = 136312
Note:
memset(modelsPricesForCustomer, 0, highestModelID*sizeof(NSUInteger));
// ^^^^^^^^^^
should be
memset(modelsPricesForCustomer, 0, highestModelID*sizeof(CGFloat));
// ^^^^^^^
to avoid clearing part of memory or going past the end of allocated buffer when sizeof(NSUInteger) != sizeof(CGFloat)