#177566 - fluffypants - Wed Sep 05, 2012 4:26 am
Hi everyone. Longtime lurker, first time poster. I am running into an issue dynamically allocating arrays. I'll post the section of code after I give a quick overview of the problem.
I am using nitroFS to read in a file containing texture data. The format is something like this:
Nothing too complicated there. I've stuck various iprintf statements for debugging to ensure that the values I'm getting are correct. So far no problems, I get the exact values I see in my file.
However, since I don't know the size of the palette and tiles beforehand, I have to dynamically allocate pent and tent (paletteEntries and tileEntries, I realize I should rename these to something more descriptive) as I parse the file. This works for the first array, but as soon as I malloc the second array, the last 4 positions in the first get clobbered. I'm baffled.
Here's my entry point from main:
and here's the function in question:
This is the part that I'm confused about:
The first print statement gives me:
23254 which is correct. After the malloc line, that same print now gives me 0. If I comment that line out then pent[7] retains the correct value. Also, the issue happens regardless of how much memory I am allocating (even 1 byte creates a problem). Most of the paletteEntries are fine, it's just a couple at the tail end of my array that get clobbered, and the tent array is just fine.
I'm still very much a novice, so feel free to point out any glaring problems with my code, related or not to the issue at hand. And thanks for reading thus far.
I am using nitroFS to read in a file containing texture data. The format is something like this:
Code: |
//Palette length
10 //Palette entries R,G,B newline deliniated 1 1 1 21 21 21 10 7 10 .... //Tile length 96 //Tiledata len*64 (8x8) 1 1 5 6 ... |
Nothing too complicated there. I've stuck various iprintf statements for debugging to ensure that the values I'm getting are correct. So far no problems, I get the exact values I see in my file.
However, since I don't know the size of the palette and tiles beforehand, I have to dynamically allocate pent and tent (paletteEntries and tileEntries, I realize I should rename these to something more descriptive) as I parse the file. This works for the first array, but as soon as I malloc the second array, the last 4 positions in the first get clobbered. I'm baffled.
Here's my entry point from main:
Code: |
int main(int argc, char **argv) {
int paletteLen; u16 *paletteEntries; int tileLen; u8 *tileEntries; ReadGraphics(&paletteLen, &paletteEntries, &tileLen, &tileEntries, "bg_tiles.h"); .... } |
and here's the function in question:
Code: |
void ReadGraphics(int *plen, u16 **pent, int *tlen, u8 **tent, char *fileName)
{ if (nitroFSInit()) { { // now, try reading a file to make sure things are working OK. FILE* fileIn = fopen(fileName,"rb"); if(fileIn) { int i,j; int R, G, B; { char line[64] = { 0 }; // Get palette length readLine(line, fileIn); // first line is a comment readLine(line, fileIn); // Palette length *plen = (int)atoi(line); //allocate a big enough palette *pent = (u16 *) malloc(*plen); // Get palette entries readLine(line, fileIn); // line is a comment for(i = 0; i < *plen; i++) { readLine(line, fileIn); R = (int)atoi(line); readLine(line, fileIn); G = (int)atoi(line); readLine(line, fileIn); B = (int)atoi(line); (*pent)[i] = (u16)RGB15(R, G, B); } // Get tile block len readLine(line, fileIn); // line is a comment readLine(line, fileIn); // number of 8x8 blocks *tlen = (int)atoi(line); // Get tiles readLine(line, fileIn); // line is a comment iprintf("pE func: %d\n",(*pent)[7]); *tent = (u8*) malloc((*tlen)*64); iprintf("pE func: %d\n",(*pent)[7]); for(i = 0; i < *tlen; i++) { for(j = 0; j < 64; j++) { readLine(line, fileIn); (*tent)[i*64 + j] = (u8)atoi(line); } } } fclose(fileIn); } } } else { iprintf("nitroFSInit failure: terminating\n"); } } |
This is the part that I'm confused about:
Code: |
iprintf("pE func: %d\n",(*pent)[7]);
*tent = (u8*) malloc((*tlen)*64); iprintf("pE func: %d\n",(*pent)[7]); |
The first print statement gives me:
23254 which is correct. After the malloc line, that same print now gives me 0. If I comment that line out then pent[7] retains the correct value. Also, the issue happens regardless of how much memory I am allocating (even 1 byte creates a problem). Most of the paletteEntries are fine, it's just a couple at the tail end of my array that get clobbered, and the tent array is just fine.
I'm still very much a novice, so feel free to point out any glaring problems with my code, related or not to the issue at hand. And thanks for reading thus far.