Can this C code cause a segmentation error?

int *i;
*i=123;
+3
source share
6 answers

He can, but there he can do something again - he shows that C Standard causes " undefined behavior ".

+29
source

Yes. There is no allocated memory to accommodate the value 123.

As for initialization, a type variable is intnot guaranteed 0, it is int*not guaranteed to be a secure address.

It can also potentially lead to data corruption.

+30
source

 

+3

32- :

  • : 99.9995%
  • : 0.0005%
  • : 1 2

. Release. , , , 100%. 100% Debug. .

+2

, , , . , ( - ), -. "-"? , .

. :

int target = 0;
int *i = ⌖ 

printf("target=%d\n", target);
*i=123; 
printf("target=%d\n", target);

, .

+2

int *i;This allocates memory for the pointer, but this variable is not initialized, so the value is completely arbitrary. Then you play it out so that you have a random address and write to this place in memory, but 1) you don’t know where the place is 2) this place is probably not yours. You can fix it with initialization likeint * i = ( int* ) malloc( sizeof(int) )

+1
source

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


All Articles