In the following example:
int *i;
*i=1;
it produces a program to hang, because I know that I am putting a value directly to a memory position.
The question that I have is why the following:
int *i=1;
only produces a warning related to a casting of an integer and does not hang the program?
and why this instruction does not produce not an error nor a warning?
char *s="acd";
if I am using something similar to the example before
Thanks
Let’s talk about the 3 cases individually:
The first line allocates memory for a pointer, but does not initialize it, leaving i with a random garbage value. The second line tries to write to the memory address spelled out by the random garbage, causing undefined behaviour at runtime.
This allocates memory for a pointer and assigns it the value 1 (i.e. the memory address
0x1). Since this implicitly casts the integer value 1 to a pointer, you get a warning, since it’s extremely rare to ever initialize a pointer to a non-null memory address.As an aside, if you were to do:
it would try to write the value 1 to the memory address
0x1, causing the same undefined behaviour as in your first case.This creates a null-terminated string on the stack and makes s point to it. String literals are the natural thing to assign to a char *, so no warning.