Because C does not require you to allocate memory. The language can access all of your address space insecurely. C is powerful, performative and flexible, at the cost of robustness. If the programmer says he wants to access an area of memory, the language leaves. Segment failure will only occur if the memory segment is protected for access (writing), which is not the case.
What was written is working, but what was there in the memory will no longer work. In this example it will work because it is something very simple, in a more complex application it would probably compromise the memory.
Could have picked up an address outside the area accessible by the application, then it would be wrong.
Without allocating memory, what is the value of s1
? It is what was in memory before. In C the memory is not cleared in the declaration of its allocation. C just reserves a space for use, in this case in the application stack. This "dirt" is giving "luck" to be something that runs. You might try later and something else will happen.
In all languages it is not enough to test to know if it is right. Yes, the test would be sufficient if it were really possible to test all possible situations. But it’s virtually impossible for anyone to do this. That’s why you always have to understand how everything works and know what you’re doing. Testing is never the solution, testing just proves that it works in a certain situation, doesn’t prove it doesn’t work. I would like every programmer to understand at least this.
In C this is even more valid.
The conclusion is that this code is wrong, even if it "passes the test".
Simple, it doesn’t give Segmentation fault because you didn’t do anything wrong: S1 is a char pointer and the only thing you did was change the memory location to where it points. It was pointing to a constant (literal string). This is valid in C. If you use the variable ONLY as a reading variable, you will never have any problem. The problem will only arise if you try to change the string value, for example: S1[1] = 'X'. See for example this question in the OS: http://stackoverflow.com/questions/1963780/when-should-i-use-malloc-in-c-and-when-dont-i
– bruno