Pointer Assignment Problem

When I run the above program in gcc complier (www.codepad.org), I get the output as a Forbidden system call: SYS_socketcall Can anyone understand why this error / exit is coming?

int main() {
    int i=8;
    int *p=&i;
    printf("\n%d",*p);
    *++p=2;
    printf("\n%d",i);
    printf("\n%d",*p);
    printf("\n%d",*(&i+1));
    return 0;
}

what i noticed, i become inaccessible after execution i ++ p = 2, WHY?

+3
source share
6 answers

When you perform *p = &i, you do pfor the whole i. ++pincreases pby pointing to the "next" integer, but since iit is not an array, the result is undefined.

+5
source

, , - undefined. , p *++p=2 , i . , , , &i + sizeof(int).

+2

undefined undefined . codepad.org , , undefined, , .

, , , - (, ).

+1

*++p p 1 int (.. ), 2, .

, *p = 2 (*p)++.

+1

, , undefined.

, , - sizeof(int) &i, - sizeof(int*) &p.

, , .

*++p=2;

printf("\n%d",*p);
printf("\n%d",*(&i+1));
0

The operator ++changes its argument, so the line *++p=2;assigns a 2place on the stack, which probably defines the frame of the call and increases the pointer p. Once you have mixed up the call frame - all bets are off - you are in a damaged state.

0
source

Source: https://habr.com/ru/post/1777974/


All Articles