-9

With this code:

#include <stdio.h>
#include <unistd.h>

int main()
{

  char buffer[64];
  int check;
  int i = 2;

  buffer[-2] = i;
  printf("%x\n",buffer[-2]);

  i=25;
  buffer[-2] = i;
  printf("%x\n",buffer[-2]);

  i=255; 
  buffer[-2] = i;
  printf("%x\n",buffer[-2]);   

  return 0;
}

I get this output (compilation with gcc and -m32 option).

2
19
ffffffff

So, I can understand the two first values (2 and 19), but I really don't understand the 3rd as 255 is equal to ff and not ffffffff.

Do you have any idea why it's displayed like this ?

Thank you

p.s: Please don't suggest to affect "chars" to a "char" array, this code is just for training, thank you.

p.s (bis): please don't say it's not valid, because if it is, why are the 1rst two output of the program in accordance with the code ??

I compiled this code with gcc -m32 -o code code.c

gfd78Fgi
  • 11
  • 2
  • 2
    Why are you using `-2` as index? – tkausl Jan 26 '17 at 09:14
  • 3
    `buffer[-2] = i;` wat – user657267 Jan 26 '17 at 09:14
  • 3
    valid indices for buffer are `0` through `63`. Your program causes undefined behaviour, therefore any output is possible – M.M Jan 26 '17 at 09:15
  • @M.M Please try youself, it work just find, see the 2 first outpu of buffer[-2], they are valid – gfd78Fgi Jan 26 '17 at 09:20
  • 1
    Apart from the undefined behavior: `char` is a *signed* integer on your platform, and printing it with `%x` does a sign extension to `-1` which is then printed in hex as `ffffffff`. So you would get the same output when using `buffer[+2]`. – Martin R Jan 26 '17 at 09:22
  • @gfd78Fgi, nops, a negative index is only allowed in cases like: `char buffer[64]; char *ptr = buffer + 2; ptr[-2] = i;` – David Ranieri Jan 26 '17 at 09:23
  • please see [what is undefined behaviour](http://stackoverflow.com/questions/2397984/undefined-unspecified-and-implementation-defined-behavior) before you attract even more downvotes – M.M Jan 26 '17 at 09:25
  • @MartinR using wrong format specifier is also undefined behaviour. On OP's system the "sign extension" is likely to come from default argument promotions, not from `%x` – M.M Jan 26 '17 at 09:26
  • 1
    @M.M: Well, that is what I meant (perhaps expressed badly): `char` is promoted to `int` with sign extension in the variable argument list, and that is printed with `%x` as `ffffffff`. – Martin R Jan 26 '17 at 09:28

3 Answers3

2

Do you have any idea why it's displayed like this ?

No, because your program has undefined behavior:

buffer[-2] = i; 
//     ^^

You're accessing buffer out of bounds, causing UB - therefore anything can happen.

Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
2
 buffer[-2] = i; //invalid; out of range

It is not valid, because you would be accessing memory outside the bounds of the array. Please see this stack overflow answer.

Community
  • 1
  • 1
msc
  • 33,420
  • 29
  • 119
  • 214
2

You seem to be asking about the behaviour of this code:

int i = 255;
char c = i;
printf("%x\n", c);

The reason it prints ffffffff is because printf is a vararg function so all integer-types smaller than int are promoted to int. So you could have written:

int i = 255;
char c = i;
printf("%x\n", (int)c);

Then the question comes, why is (int)c the same as ffffffff. It is because c is assigned ff, which is beyond its limits of -128 to 127. It is wrapped to -1. It is then converted to the int value -1, and the int value -1 is coded as ffffffff.

Note: As many others have written, your program has undefined behaviour. This means that your program may print other things, or crash, when run on a different compiler, a different computer, or even a different version of your compiler. It is considered bad practise to include undefined behaviour in a program.

Mats
  • 8,528
  • 1
  • 29
  • 35
  • 2
    For completeness it should be pointed out that on a system with unsigned `char` there will be no sign extension and the output will be `ff`. – Klas Lindbäck Jan 26 '17 at 09:31