c - realloc() in a 64bit iOS device -


when use c function realloc(p,size) in project, code runs in both simulator , on iphone 5.

however, when code running on iphone 6 plus, odd things happen. contents a points changed! what's worse, output different every time function runs!

here test code:

#define length (16)  - (void)viewdidload {      [super viewdidload];         char* = (char*)malloc(length+1);       a[0] = 33;      a[1] = -14;      a[2] = 76;      a[3] = -128;      a[4] = 25;      a[5] = 49;      a[6] = -45;      a[7] = 56;      a[8] = -36;      a[9] = 56;      a[10] = 125;      a[11] = 44;      a[12] = 26;      a[13] = 79;      a[14] = 29;      a[15] = 66;      //print binary contents pointed a, print each char in int can see differene      [self printinbyte:a];      realloc(a, length);      [self printinbyte:a]        free(a);  }  -(void) printinbyte: (char*)t{      for(int = 0;i < length ;++i){          for(int j = -1;j < 16;(t[i] >> ++j)& 1?printf("1"):printf("0"));          printf(" ");          if (i % 4 == 3) {              printf("\n");          }      }  } 

one more thing, when length not 16, runs assigning a[length-1]. however, if length 16, things go wrong.

any ideas appreciated.

from realloc man doc:

the realloc() function tries change size of allocation pointed ptr size, , returns ptr. if there not enough room enlarge memory allocation pointed ptr, realloc() creates new allocation, copies of old data pointed ptr fit new allocation, frees old allocation, , returns pointer allocated memory.

so, must write: a = realloc(a, length); because in case allocated memory block can't made larger, zone moved location, , so, adress of zone change. why realloc return memory pointer.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -