c pointer deferencing and arithmetic

Aaron
int main(void)
{
    int *x = 0;
    int y = 0;

    x = &y
    *x = 1

    *(x+1) = 10
    return 0;
}

When I perform *(x+1) = 10 and try to print out the address of x, it points to 0x7fff0000000a and get this when I try to print out the value of x Cannot access memory at address 0x7fff0000000a

Before *(x+1) = 10, the address of x comes out to (int *) 0x7fffffffe4d4 and the value of x comes out to 1

The question I have is, what exactly is *(x+1) = 10 doing? The address of x and y are 4 bytes apart. Since x points to y, and x was added 1, it should point back to x itself

Address of x: (int **) 0x7fffffffe4d8 Address of y: (int *) 0x7fffffffe4d4

Sergey Kalinichenko

what exactly is *(x+1) = 10 doing?

It re-interprets x as a pointer to the initial element of an array, and tries to access that "array"'s second member (i.e. an item at index 1). Since x is neither an array nor a pointer into an array, the assignment is undefined behavior.

The address of x and y are 4 bytes apart.

This happens to be the case with your compiler. It may be different on other systems, depending on many factors, such as the compiler, the optimization flags, the size of a pointer, the size of an int, and so on. The important thing is that no matter where x+1 points, it is illegal to dereference it, because x points to a scalar local variable.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

C pointer arithmetic without object of structure

From Dev

Performing arithmetic on a void pointer in C++

From Dev

Compare structs in C using memcmp() and pointer arithmetic

From Dev

Is there a safe way to perform pointer arithmetic in C# prior to .NET 4.0?

From Dev

Most efficient pointer arithmetic type in c

From Dev

How to do C++ parameter array pointer arithmetic in Delphi

From Dev

Deferencing Void Pointer / Void Pointer Copy

From Dev

C++11: (recursive) pointer arithmetic in templates (TMP)

From Dev

Correct pointer arithmetic in C

From Dev

I don't understand C++ pointer arithmetic

From Dev

c pointer deferencing and arithmetic

From Dev

Pointer Deferencing in x86 Assembly Code

From Dev

c++ pointer arithmetic and classes

From Dev

Char pointer arithmetic C

From Dev

Pointer Arithmetic in C using Array Variables

From Dev

Pointer arithmetic and 2-D array in c?

From Dev

C++ Bus Error during Pointer arithmetic

From Dev

C Pointer Arithmetic Strange Behavior

From Dev

Error: deferencing pointer to incomplete type

From Dev

Void Pointer Arithmetic in C++11

From Dev

Why isn't this pointer arithmetic allowed in C?

From Dev

Pointer arithmetic

From Dev

Deferencing pointer to incomplete type error in C

From Dev

C:Pointer Arithmetic -How does it work?

From Dev

c++ pointer arithmetic and classes

From Dev

Pointer arithmetic and 2-D array in c?

From Dev

Error: deferencing pointer to incomplete type

From Dev

Pointer arithmetic Visual Studio C++

From Dev

Understanding Pointer Arithmetic of Array Declared in C