0

I coded something like the following code,and I was able to assign a value to the new address after increasing it but was not able to print this value run time error, Also after assigning a value to the location this pointer pointing to, pointer value changed to be 14. Anyone has an idea of what's going on ?

Why the pointer value itself changed to 14 after assigning value to the location itself ?

I did not get any error after increasing the pointer value too !

#include <stdio.h>
int main()
{
    int x = 10;
    int *ptr = &x;
    printf("%x\n",ptr);             // ptr value
    ptr++;                          //No ERROR !!
    printf("%x\n",ptr);             //ptr value +4 bytes no error!!!
    *ptr = 20;
    printf("%x\n",ptr);             //ptr=14
    printf("%x\n",*ptr); // run time error happens here only
    return 0;
}
2
  • 1
    Once you moved the pointer it is not pointing to any valid location anymore. UB. Commented Dec 21, 2017 at 18:50
  • There is no error, there is just undefined behaviour.. your code has 6 locations of undefined behaviour Commented Dec 21, 2017 at 20:02

2 Answers 2

2

This is undefined behavior. When you incremented the pointer variable then it was pointing to one past the variable x (4 Bytes past in your system). But then you dereference it. First of all the memory you made change to is not allocated by you. And also it is not a location that is already allocated (like part of an array etc). It is Undefined behavior to access it.

And again you can assign it to any possible address. But dereferencing it would be undefined behavior in case the memory address it points to is invalid.

From standard 6.3.2.3

The unary * operator denotes indirection. If the operand points to a function, the result is a function designator; if it points to an object, the result is an lvalue designating the object. If the operand has type ''pointer to type'', the result has type type. If an invalid value has been assigned to the pointer, the behavior of the unary * operator is undefined

Sign up to request clarification or add additional context in comments.

6 Comments

What about the pointer "address value "itself, why did changed after assigning value to it ?
Because you assigned a new value to it--that's what assignment means. C is a low-level language that does not generally prevent you from doing illiegal/stupid things.
@LeeDanielCrocker I believe I did not change the address value itself , I added value to the location it is pointing to,I thought this value "20" won't affect the pointer value itself, 0x14 is not the right value of pointer.
I guess I don't understand what you think you're seeing. But it doesn't matter anyway. Why broken code does what it does is not the programmer's concern. His job is to fix it.
@IDEN.: Yes that is it. I am sure somehow my comments notification didn't work. That's why late reply. The thing is you should have gotten some idea seeing the values -0x14 means 20. Same as the address value you assigned. Pointing to pointer and then changed i to some value - here 0x14.
|
0

When you do ptr++, it points "one element" past x. This is allowed, because x in this case is treated as an array of size 1, and a pointer is allowed to point one element past the end of an array. You can also subsequently print the value of that pointer with no problem.

What you can't do however is dereference a pointer to one element past the end. That invokes undefined behavior. In this case that behavior manifested as the pointer having an unexpected value and a subsequent crash.

That being said, here's what probably happened.

ptr was most likely placed right after x in memory, so after doing ptr++, ptr was pointing to itself. So *ptr = 20; had the effect of of setting ptr to 20. The value 14 that was printed is in hex, which is the same as 20 decimal. This explains the value that was printed.

Then you tried to print *ptr, which in this case says "print the int value at address 0x14". That is most likely not a valid address, so attempting to read it caused a crash.

You can't however depend on this behavior. You could add an extra printf or compile with different optimization settings and the observed behavior would change.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.