-8
unsigned char * numbers = {1,1,1};
unsigned short * ptr = (unsigned short*) numbers;
*(++ptr)=2;

printf("%d %d %d %d", numbers[0], numbers[1], numbers[2], numbers[3]);

The above can result in undefined behavior, right? Also, what will be printed in the screen?

The result was 1 1 2 0, but could 1 1 0 2 be printed sometime?

TartanLlama
  • 63,752
  • 13
  • 157
  • 193
We're All Mad Here
  • 1,544
  • 3
  • 18
  • 46
  • 6
    since it is an undefined behavior, it is impossible to say what will be printed... – Felice Pollano Aug 31 '15 at 14:37
  • 1
    Did you try to run it ? – Hacketo Aug 31 '15 at 14:37
  • 1
    @Hacketo: of course, the result was "1 1 2 0" – We're All Mad Here Aug 31 '15 at 14:38
  • 1
    @Hacketo: the big question is, will this in some computers print "1 1 0 2"? – We're All Mad Here Aug 31 '15 at 14:38
  • @Felice Pollano: We don't know for sure what the last character will be but the 1st 3 will be printed okay. – We're All Mad Here Aug 31 '15 at 14:39
  • Sure, why not? Or maybe print something else completely? Or give you nasal demons? Who knows, it's undefined behavior, *anything* could happen. – Some programmer dude Aug 31 '15 at 14:40
  • Yes, on some computers it would print 1 1 0 2. On some computers it should crash before printing anything (such computers might be harder to find, but have existed). – JSF Aug 31 '15 at 14:41
  • 3
    Older versions of GCC were known to start up a fun little computer game whenever encountering undefined behavior. – Lundin Aug 31 '15 at 14:43
  • @Lundin Was that game "Find the segfault" by any chance? – awksp Aug 31 '15 at 14:44
  • Are you reviewing someone else's code, and asking for confirmation that it's flawed? – donjuedo Aug 31 '15 at 14:44
  • 1
    @awksp http://feross.org/gcc-ownage/ – Lundin Aug 31 '15 at 14:45
  • Just guessing on the reasons for downvotes, but this sounds an awful lot like someone trying to get someone else to do his homework. – NoelC Aug 31 '15 at 14:50
  • I am looking at the comments, and I don't see why I was asked to. I downvoted because you're well aware that this is undefined behavior yet you still ask us what will happen. What part of *undefined behavior* is unclear here? – Borgleader Aug 31 '15 at 14:51
  • @ Borgleader: I am asking because I am not sure if the output of the 1st 3 characters will be the same on all computers (if they don't crash). – We're All Mad Here Aug 31 '15 at 14:52
  • 3
    @We'reAllMadHere Trying to predict the outcome of UB is generally pointless practice and likely where all down votes come from. Essentially you are asking "what is the behavior of undefined behavior, which doesn't make any sense. It is better to focus your energy on understanding _why_ this code is bad. – Lundin Aug 31 '15 at 14:56
  • 5
    One possible effect of undefined behavior is unexpected downvotes on a web site. The C++ standard allows that. – Bo Persson Aug 31 '15 at 15:02

2 Answers2

3

It will result in undefined behavior. First because you let a char pointer point to an integer array: note the difference between unsigned char * numbers = {1,1,1}; and unsigned char numbers[] = {1,1,1}; unsigned char* ptr = numbers;.

But also because the program breaks the strict aliasing rule. Anything might happen: the program might print some sort of result, or rubbish, or crash.

Furthermore, your cast to unsigned short assumes a certain CPU endianess. So in case your program happens to go for the undefined behavior "print some sort of result", that result will depend on CPU endianess.

Community
  • 1
  • 1
Lundin
  • 195,001
  • 40
  • 254
  • 396
3

The above can result in undefined behavior, right?

(1) Not only the behaviour can be undefined, but it will always be undefined.

Also, what will be printed in the screen?

It is undefined as per (1)

will this in some computers print "1 1 0 2"

It might. Or it might not. It's possible because any outcome is possible. See (1).

We don't know for sure what the last character will be but the 1st 3 will be printed okay.

They might not be printed okay. See (1).

This particular piece of code:

unsigned char * numbers = {1,1,1};

will not compile.

eerorika
  • 232,697
  • 12
  • 197
  • 326