-1

In my code, I increment address of the *p which address doesn't contain any value.so, why this p variable doesn't return me a garbage value.

#include <stdio.h>
int main()
{
    int a=4;
    int *p=&a;
    p++;
    printf("%d ",*p);

    return 0;
}
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
Ishan Ahmed
  • 105
  • 11
  • 13
    `0` is a garbage value (as is `1`, `42`, `-7621803`, ...) – pmg Mar 24 '21 at 09:50
  • 9
    what makes you think the number it outputs isn't garbage? – Raildex Mar 24 '21 at 09:51
  • 1
    You're going out of bounds of `a`, and that leads to *undefined behavior*. Once you have undefined behavior your whole program is invalid. What you're doing could print a seemingly random value (of which `0` is a valid value), it could cause a crash of your program, or it could set your computer on fire or strangle your cat. – Some programmer dude Mar 24 '21 at 09:52
  • 3
    why do you expect a "garbage value" ? What is a "garbage value" ? Your code invokes undefined behavior. Frankly, expecting output that looks a certain way is silly. Undefined behavior is undefined. If someone told you that you should get some garbage value that has certain properties that qualifies as "garbage" they were telling you nonsense – 463035818_is_not_an_ai Mar 24 '21 at 09:53
  • 1
    There are **no** garbage numbers. Suppose we have a list of garbage numbers and a list of non-garbage numbers. The smallest garbage number has the interesting property of being the smallest garbage number, so it cannot be garbage, and so it can be moved into the non-garbage list. Now there is a *new* 'lowest garbage number' and... – Weather Vane Mar 24 '21 at 10:10
  • [What is undefined behavior and how does it work?](https://software.codidact.com/posts/277486) – Lundin Mar 24 '21 at 11:49

5 Answers5

2

why this pointer does not give me a garbage value

It probably does. The behaviour of the program is undefined.

I recommend thinking why you thought that you didn't get a garbage value. That should lead you to a deeper understanding.

Note that technically getting a garbage value isn't guaranteed. It's entirely possible that you get no output at all, or that the program crashes or anything else. But if you do get output, it will be garbage.

eerorika
  • 232,697
  • 12
  • 197
  • 326
1

This is an undefined behavior so it may show you some values that may seem to be valid but is not. Here is an element of undefined behavior documentation and here

Pat. ANDRIA
  • 2,330
  • 1
  • 13
  • 27
1

There are no "garbage" values. According to the c++ standard, a program that contains undefined behavior has ... well ... undefined behavior. The output could be anything.

You are not guaranteed to get any meaningful output. Though, that does not imply that the output you get is guaranteed to be meaningless (or "garbage" or whatever you want to call it).

It could be 4 because a holds that value, it could be 4 because that value happens to be in some register. You have no way to tell why you get the output you get by looking only at the code and the output. Thats because the C++ standard does not specifiy what the program does at runtime when you dereference an invalid pointer, ie when your code invokes undefined behavior anything can happen.

If you want to know where the output actually comes from you can look at the assembly. For a simpler code:

int main() {
    int* p;
    return *p;
}

gcc -O3:

main:
        mov     eax, DWORD PTR ds:0
        ret

When running the code, it segfaults. Thats one possible outcome. Given that code, anything else is possible as well.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
-3

Because you increment the pointer, not the value it points to.

Then *p points into the memory after a. That place is on the stack and youfind some random(?) value there.

Define another variable b with some other value after a and you will most likel see that value.

U. W.
  • 414
  • 2
  • 10
-3

It's returning garbage value

 468024888
H Hasan
  • 55
  • 1
  • 8
  • 1
    Or maybe not? What qualifies `468024888` as "garbage"? You could put any number there. Because the behaviour is undefined, any value you receive is garbage. – Lukas-T Mar 24 '21 at 10:12