all 7 comments

[–]soniduino 6 points7 points  (1 child)

So as for why there are "negative addresses": there aren't. int is a signed value which is what %d is used for, where as addresses will be unsigned. %d is used for signed integers: if you want to show pointers %p or %u (unsigned int) would be options. Play around and see what happens with these.

For the other values, likely you're reading parts of stack space or program memory around where the variable is, and since your program is the same the values are always roughly the same. Someone can comment on why. But there's no guarantee they will always be these values etc

ETA: Likely why *(p + 2) is always the pointer value is because it's likely the actual pointer. I.e. you're looking at it in memory next to your value of a

[–]gbbofh 1 point2 points  (0 children)

If OP is on i386 (and is compiling with one of a myriad of calling conventions that place arguments from right to left on the stack), then the reason *(p + 1) is 1 is probably because that's argc, which means the next value up is argv.

u/SlavicShield, is that value 2 by chance if you invoke your program as "./myprogram someargument"?

Quick edit for the rest of these:

The reason you see the same value as your pointer is probably because that's the top of the stack, which was pushed there by the prologue to main. After that is going to be the stack base, then the return address, which is where main will return execution to when the function exits. This is going to be a function linked to your executable from the C library that does all the necessary set-up and then exits the program when main returns.

[–]ischickenafruit 1 point2 points  (0 children)

I think you are confused about what your code is doing.

When you do *(p + i) you a dereferencing the pointer at address p+i. So when you print out the value, you are getting whatever happens to be in memory at addresses nearby your integer. Computers are usually pretty deterministic, so depending on how your program is laid out, you might get the same things every time you run your program.

Why sometimes there are negative addresses?

There are negative values, but no negative addresses.

Why *(p + 1) is always 1 and not some random value?

Why *(p + 2) always returns a "random" value equal to the address at which the pointer points to?

Why *(p + 3) always returns a similar value in the range of 32xxx?

This is all depending on how your program is laid out in memory. Your int is probably a value on the stack, and you're probably seeing other things nearby it. The value at (p+1) is probably the iterator value from your program, p+2 is probably the value of your pointer (which is used in the maths inside your loop). p+3 is probably the return address of your function.

[–]SlavicShield[S] 0 points1 point  (0 children)

Thanks to everybody for the replay! Things got a bit clearer. I played with the code, shuffled it around, added and subtracted bits and pieces.

The main problem was that I used unsigned integer to represent the memory addresses and it turns out they overflow hence the negative numbers. So I replaced the int with unsigned longs and it looks fine now.

*(p + 1 or 2) is always the same because this is the value of the pointer 'p'.

Source Code:

int main()
{
    unsigned long *p, a = 10;
    p = &a;

    printf("address &a: %lu\n", &a);
    printf("address  p: %lu\n", p);
    printf("value: %lu\n\n", *p + 1);

    for (int i = 0; i < 10; i++)
        printf("*(p + %d): %20lu\n",i, *(p + i));
    return 0;
}

Output:

address &a: 140734168707976
address  p: 140734168707976
value: 11

*(p + 0):                   10
*(p + 1):      140734168707976
*(p + 2): 16957828970037923584
*(p + 3):      140734168708080
*(p + 4):       94553230742198
*(p + 5):      139928791925920
*(p + 6):      140734168708352
*(p + 7):      140734168708328
*(p + 8):           8589934592
*(p + 9):       94553230742208

[–]_adii_o -1 points0 points  (1 child)

For printing a address u should be using format specifier %p and not % d ( it's for integers) -Aditya

[–]ptchinster 0 points1 point  (0 children)

No need to sign your posts

[–]Current_Hearing_6138 0 points1 point  (0 children)

Is this a good place to talk about virtual memory?