-1

I have been doing this exercise of exploring strings in c programming. I found that we can assign a string literal to a pointer also (Like in the code mentioned here!)

#include <stdio.h>

int main(void)
{
    char msg1[] = "Hello";
    char *msg2 = ", Welt";

    printf("the string at msg1 is :%s\n", msg1);
    printf("the string at msg2 is :%s\n", msg2);


    printf("Address of the msg1 is: %p\n",&msg1);
    printf("Address of the msg2 is: %p\n",&msg2);

    printf("Value of the msg1 is: %p\n",msg1);
    printf("Value of the msg2 is: %p\n",msg2);
    
    msg1[1] = 'G';
    msg2[2] = 'X';

    printf("Value of the msg1 is: %p\n",msg1);
    printf("Value of the msg2 is: %p\n",msg2);
    return 0;
}

now when I execute this I get:

C:\Users\ADMIN\Desktop>test
the string at msg1 is :Hello
the string at msg2 is :, Rohit
Address of the msg1 is: 0061FF1A
Address of the msg2 is: 0061FF14
Value of the msg1 is: 0061FF1A
Value of the msg2 is: 00405064

i.e. The last two lines are not even printed! (same results in any platform)

My understanding and my question: I understand that I am assigning a ROM address to the pointer variable called: char *msg2, and am assigning a String Literal: ", Welt" to it. I also know that the FLASH controller at ROM will not allow this Write operation unless there is like a "special permission".(I'm still learning about these kind of processes).

But the thing is that: why are the print messages after that "illegal ROM-write process" are not being executed? is it because the the compiler ignores the instructions after that "illegal ROM-write process" ?

I know that we should NOT play around with ROM access like this in a program, rather use a:

char const *msg2 = ", Welt"; //which will give us an error incase we change something.

SORRY, I've just started Stack Overflow - I will learn to be more professional in the upcoming days

  • 2
    `msg2[2] = 'X';` is _undefined behavior_ (UB) as it attempts to change a _string literal_. Anything may happen including "messages after that "illegal ROM-write process" are not being executed". – chux - Reinstate Monica Apr 14 '22 at 08:16
  • In C all literal strings are really arrays of characters, including the null-terminator. And they are not allowed to be modified, any attempt to modify one leads to *undefined behavior*. They are, in essence, read-only, and is the reason it's recommended to use `const char *` to point to them. – Some programmer dude Apr 14 '22 at 08:17
  • And while there are different type of ROM, some which could be written to (for example EEPROM), a plain ROM is not writable, no matter permissions. – Some programmer dude Apr 14 '22 at 08:19
  • 1
    @Someprogrammerdude "they are not modifiable." over simplifies. _String literals_ are not _not modifiable_. They may change, they may not, bad things may happen. it is UB. `const char *` is a good idea. – chux - Reinstate Monica Apr 14 '22 at 08:19
  • 3
    Your program simply crashes. The compiler produced code, look into the generated assembly. – the busybee Apr 14 '22 at 08:20
  • @chux-ReinstateMonica You're right, rephrased. – Some programmer dude Apr 14 '22 at 08:21
  • 1
    Note: ROM means a specific type of physical memory (chips), and has nothing to do with read-only memory area of a process (except maybe on some low-level embedded devices, probably not the case here). So everything about ROM and FLASH in the question is just misunderstanding, the steing literal here is in RAM when it is in memory, and operating system just disallows writing there (if it is a modern OS with memory protection). – hyde Apr 14 '22 at 08:47

1 Answers1

3

msg2[2] = 'X'; is undefined behavior (UB) as it attempts to change a string literal.

Anything may happen including <messages after that "illegal ROM-write process" are not being executed>.


is it because the the compiler ignores the instructions after that "illegal ROM-write process" ?

If this was true, then that would be defined behavior. It has the possibility of being true today and not tomorrow. Anything may happen as that what UB is all about - uncertainty.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256