Why not give Segmentation fault when writing to a "char *" without allocating memory?

Asked

Viewed 96 times

4

My question is whether the compiler automatically allocates memory to the variable s1

char *s1;
char s2[20];

//s1=(char*)malloc (sizeof(char)*20);
s1="palavra1";

strcpy (s2,"palavra2");


printf("s1:%s\ns2:%s\n",s1,s2);

Apparently if I make the allocation that is in comment, has the same output, namely, the palavra1 is printed in the same.

The same situation happens when I do the following:

char *s1 = "teste";
char s2[20];

//s1=(char*)malloc (sizeof(char)*20);
//s1="palavra1";

strcpy (s2,"palavra2");


printf("s1:%s\ns2:%s\n",s1,s2);

Will print the word "test" and "word1"

What can I conclude from here?

  • 2

    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

1 answer

6


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.

Fiat 147 todo detonado andando pelas ruas

In C this is even more valid.

The conclusion is that this code is wrong, even if it "passes the test".

  • I don’t see anything wrong with the code.s1 is always initialized pointing to valid memory zone

Browser other questions tagged

You are not signed in. Login or sign up in order to post.