0

I am learning pointers. I saw this code sample in a tutorial. I tried it but it gave different result from the tutorial.

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i = 5;
    int myInt = 7;
    int *pointer = &i;
    printf("%i\n", *(pointer + 1));

    return 0;

}
  • on a windows machine, the output is 2686740

  • on a linux machine, the output is 7.

What is the reason for this?

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
Hakan As
  • 23
  • 3
  • 3
    Lookup `undefined behavior`. There is nothing in particular guaranteed to exist at `*(pointer + 1)` since the code never allocates or reserves anything for it. Also, please kindly point out the "tutorial" so that others know to avoid it. – dxiv Dec 22 '15 at 05:41
  • Accessing invalid memory is Undefined Behaviour. More details: [Does “Undefined Behavior” really permit *anything* to happen?](https://stackoverflow.com/questions/32132574/does-undefined-behavior-really-permit-anything-to-happen) and [Undefined, unspecified and implementation-defined behavior](https://stackoverflow.com/questions/2397984/undefined-unspecified-and-implementation-defined-behavior). – kaylum Dec 22 '15 at 05:42
  • @dxiv I do not know whether it is a bad tutorial or not. In fact, I am an absolute beginner. [link] https://youtu.be/5jQ2ZiXzJjE?t=3m57s – Hakan As Dec 22 '15 at 05:54
  • Judging by just the example you posted (sorry, did not and will not watch the videos), that's either an awful tutorial, or maybe one aimed at a different audience about some special cases of compiler/OS behavior. In either case, that is the completely wrong source to be `learning pointers` from. Get yourself some other basic, established, reputable book or online tutorial. Once you understand why the question you just posted makes no sense in the context of standard C, please add your answer here, and I'll be the first one to upvote it. – dxiv Dec 22 '15 at 06:11
  • 1
    @HakanAs That's a crap tutorial, stop watching it. The author says in a comment "The compiler doesn't guarantee that the variables will be in order like that. I just assumed that since they were for me, they would be for the majority of people watching, but I guess I was wrong." So he doesn't have a clue about C basics yet he thought it would be a great idea to create a C tutorial series... – Lundin Dec 22 '15 at 07:25
  • @Lundin thank you for your interest and advice. I will take it into consideration. I have already started searching a good book on "C". – Hakan As Dec 22 '15 at 07:53

4 Answers4

1

To elaborate on the existing answers, I would like to add an explanation.

In your code, i is an int variable. You assign the address of i to pointer. Fine. Then, what you do is, increment the pointer (address) and then, attempt to dereference it.

Now, in comparison to the statement in your code,

printf("%i\n", *(pointer + 1));    

quoting the C11 standard, chapter §6.5.6, Additive operators

[....] If both the pointer operand and the result point to elements of the same array object, or one past the last element of the array object, the evaluation shall not produce an overflow; otherwise, the behavior is undefined. If the result points one past the last element of the array object, it shall not be used as the operand of a unary * operator that is evaluated.

Essentially, by doing this, you're trying to access some memory which is not allocated to your process, thereby invoking undefined behavior.

The output of UB, is, well, undefined.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
0

You're doing pointer arithmetic into a region that wasn't allocated for that pointer/reference, so it's undefined. It can be implemented however the compiler wishes to.

The actual reason is that on linux, the variable my int is located at the address &i + 1, whereas on windows... it's somewhere else

Joseph Young
  • 2,758
  • 12
  • 23
0

The memory (stack in this case) is like this

[ i ][ something else .... 
^    ^
^    pointer+1 
pointer

Doing

int *pointer = &i;
printf("%i\n", *(pointer + 1));

you display an int, ie the memory space from pointer+1 of sizeof(int) is read as an int, something that the compiler did not expect you to do, and that space is "unknown". So Windows may display X, Linux may also do that, display something else or even crash ...

This is Undefined Behavior.

Déjà vu
  • 28,223
  • 6
  • 72
  • 100
0

I think the code is making assumptions about the memory layout which might or might not hold. The assumptions are: stack is linear and variables are stored exactly as declared in source. This implies somewhat that you assume the compiler not to make any optimizations.

Try the following on windows: (i) switch off all optimization of the compiler, (ii) or run in debug mode. The behavior might or might not switch to the "expected" one on windows. The lesson is: do not write such code ;)

NeitherNor
  • 254
  • 2
  • 7