Dreamer78692
Dreamer78692

Reputation: 215

code crashing at malloc() on windows XP

My simplified code looks like something below:

 char decrypted[64] = "P@ssw0rd                ";
 int realsize = 8;
  realloc(decrypted, realsize);

  char *dec2 = (char *) malloc(realsize+1); // Exe crashes at this point

I am guessing it has to do with char *dec2 , but this only crashes Win XP for some reason.

Upvotes: 0

Views: 194

Answers (2)

Marius Bancila
Marius Bancila

Reputation: 16318

decrypted is an array allocated on the stack. realloc can only re-allocate memory allocated on the heap with malloc or calloc.

Upvotes: 2

Alexey Frunze
Alexey Frunze

Reputation: 62048

You cannot realloc() what hasn't been allocated with malloc() or calloc() or realloc().

Upvotes: 7

Related Questions