Reputation: 681
I need to analyze one part of the song. When I tested on iPhone simulator I used 2d array which was myArray[150][220500]
big.. So.. I can't get this code work on device, because it does not have enough memory for that (Code works with smaller 2d array, but it's not enough for analyze).
Any ideas how could I get this work on device?
Upvotes: 0
Views: 52
Reputation: 57169
That is too much data to allocate on the stack. According to the documentation iOS only has 1MB of stack space on the main thread and secondary threads get 512KB. Assuming myArray
is declared as an int
(4 bytes) you are allocating at least
(4 * 150 * 220500) bytes = 132300000 bytes = 126.171 MB
You will need to allocate the memory on the heap instead using malloc
or calloc
.
//Initialize array
int **myArray = malloc(150 * sizeof(*myArray));
for(unsigned int i = 0; i < 150; ++i)
{
//Use calloc if you want all 0's
myArray[i] = malloc(220500 * sizeof(**myArray));
}
//Use array
myArray[10][2000] = 36;
NSLog(@"[10][2000] = %d", myArray[10][2000]);
//Delete array - You must do this to prevent memory leaks
for(unsigned int i = 0; i < 150; ++i)
{
free(myArray[i]);
}
free(myArray);
Upvotes: 4