In c/c++ some people use c-styled strings like:
char *str = "This is a c-styled string";
My question is is this safe? The way I see it is they created a char pointer that points to the first letter of a const array of chars, but can’t some other thing eg another variable overwrite a portion of the char array in the memory? Thus causing str to be logically invalid?
These days string constants are placed in read-only sections of your binary. If you attempt to write to an address in a read-only section the CPU’s memory management unit will cause a fault and your application will get an access violation or segmentation fault or some other operating system dependent behavior.
Also, compilers warn if you declare the type of the string constant to be non const.