c - Strange malloc bad access -


i got problem on allocating memory using malloc in xcode

when use smaller block_size (256) code has no problem if use larger block_size (65536) xcode stop @ "state1[t] = (int*) malloc(sizeof(int) * 4);" , tell me bad_access. how solve problem?

thanks

int main(int argc, const char * argv[]) {      // insert code here...     int **state1;     int t = 0;     int block_size = 65535;     state1 = (int **)malloc(sizeof(int) * block_size);     printf("%d",block_size);     (t=0 ; t < block_size-1 ; t++) {         state1[t] = (int*) malloc(sizeof(int) * 4);     }     printf("end");     return 0; } 

the first malloc should be

state1 = malloc(sizeof(int *) * block_size); 

because allocate array of pointers. on 64-bit platform makes difference! people prefer write

state1 = malloc(sizeof(*state1) * block_size); 

to avoid kind of error.

remark: in c, need not cast return value of malloc().


Comments

Popular posts from this blog

assembly - 8086 TASM: Illegal Indexing Mode -

Java, LWJGL, OpenGL 1.1, decoding BufferedImage to Bytebuffer and binding to OpenGL across classes -

javascript - addthis share facebook and google+ url -