0

I am going through Kernighan and Richie's text on C . And I came across a statement which I could not exactly reason out .

char *pmessage;
pmessage = "jhjhjh";

The book states that only pointers are involved here . That's fine . Now it states that editing the contents of the string will have undefined behavior . I can understand that the pointer holds the starting address of where the "j" in the string is stored in the memory .

But editing the string will only change the values that are stored in the addresses which are stored in pmessage and its subsequent memory locations /

Why will result in undefined behavior ?

Ry-
  • 218,210
  • 55
  • 464
  • 476
  • 1) Most compilers will use the same constant (memory space) if it is used more than once, e.g. if you have later in this program `if ( !strcmp( abc, "jhjhjh" ) ) do_this();`. If you change the contents for one pointer variable, it will change all strings for the program; 2) In some cases the string can be stored in read-only memory, e.g. program flash. – i486 Apr 28 '18 at 20:21
  • @i486 : But i guess in constant folding technique in compilers , at the stage where the AST is generated , we use the constant string itself in the AST and don't allocate any memory for it . So isn't it a bad idea to store constant strings in some memory location and be prone to problems like changing a constant string everywhere ? – Kanishk Viman Apr 28 '18 at 20:33
  • @KanishkViman be aware and don't do it. C is raw, although modern compilers are a big help. – Weather Vane Apr 28 '18 at 20:35
  • @WeatherVane : Ok but why does that compiler allocate memory for constant strings ? This book is from the creators of C so I feel there must be some logic behind designing in this way . – Kanishk Viman Apr 29 '18 at 11:12
  • Where else can constant strings go but in run time memory? `pmessage` is pointer to that memory, and that is all. `pmessage` contains no other information. – Weather Vane Apr 29 '18 at 11:17
  • @WeatherVane : Suppose there is an if condition comparing a particular string with the constant "ru" . So the assembly instruction that would be generated for this would be to compare the value stored in the first memory location of str with "r" and the one in the second one with "u" . Why would I need to store "ru" in a memory location ? – Kanishk Viman Apr 29 '18 at 16:39
  • Perhaps in that case the code would be optimised but "r" and "u" are still in memory (and the `0` terminator checked too, or it could be a substring). Suppose the comparison was "floccinaucinihilipilification", would you think each character will be compared with inline code, or will `strcmp` be called? – Weather Vane Apr 29 '18 at 17:17
  • @WeatherVane : Sorry . But I could not figure out how "r" and "u" will be in memory , just the instructions for comparing with "r" and "u" will be in memory . – Kanishk Viman Apr 29 '18 at 17:26
  • "r" and "u" will be a part of the instruction, therefore in memory. – Weather Vane Apr 29 '18 at 17:26
  • @WeatherVane : I am not sure but the case that you presented there also I feel it would be optimized to generate instructions to compare individual characters . Even if the optimization is by two or three machine cycles. – Kanishk Viman Apr 29 '18 at 17:27
  • @WeatherVane : But not allocating any memory locations for them right ? – Kanishk Viman Apr 29 '18 at 17:28
  • But your string literal was "jhjhjh" not "ru". I suggest you look at the compiler output if you are convinced that a string literal will not be allocated some memory, but you have gone off topic. – Weather Vane Apr 29 '18 at 17:30

0 Answers0