0

In the below program,

#include<stdio.h>
#include<stdlib.h>
int main(){
  const char *str1 = "abc";
  char *str2 = (char *)malloc(sizeof(char)*4);
  str2= "def";
  str2[1]='s';
  printf("str2 is %s", str2);

}

Debugger :

(gdb) ptype str1                                                                                                                         
type = const char *                                                                                                                      
(gdb) ptype str2                                                                                                                         
type = char *                                                                                                                            
(gdb) n                                                                                                                                  
7         str2[1]='s';                                                                                                                   
(gdb) n               

Program received signal SIGSEGV, Segmentation fault.                                                                                     
0x00000000004005ab in main () at main.c:7                                                                                                
7         str2[1]='s';                                                                                                                   
(gdb)

SIGSEGV on str2[1] = 's';

As per my understanding, one cannot modify abc pointed by st1, due to declaration const char *st1 = "abc", where string literal is constant.

Why char * type str2 does not allow modifying the elements? Is stringliteral def also a constant string literal.

overexchange
  • 15,768
  • 30
  • 152
  • 347

5 Answers5

2

That's what happens:

  1. str2 is allocated with malloc() and points to the newly allocated buffer of 4 chars

  2. You change the value (address) of str2 to the address of "def", which is read-only (think of it as a part of your program)

  3. You try to change "def"

  4. You are not able to free the memory because you actually lost the address of it when you assigned str2 to the address of "def"

Robert Mutke
  • 2,757
  • 1
  • 16
  • 14
1

As per C standars

string literal ("def") will be stored in the data section, in a read-only page.

So you can not modify its memory.


You can modify the content of the str2 pointer. It means you can point str2 to something else memory. But memory allocared for string literal "def" can not be modified.


  str2= "def";

By doing this you are not copying "def" in your malloced memory. Instead "def" is getting allocated in read only region and you are assigning its address to str2.

If you want to store that "def" in to malloced space then use strcpy of memcpy.

Jeegar Patel
  • 26,264
  • 51
  • 149
  • 222
0

The str2 = "def"; statement changes the value of str2. str2 points to the string literal "def" after the assignment leading to the following crash. At least, it is so on my RHEL 7.1 with gcc 4.8.3

GMichael
  • 2,726
  • 1
  • 20
  • 30
0

If you want to store "abc" in the string buffer you have just allocated, you have to use strcpy(), as in strcpy(str2, "abc"). As pointed out by other users, using str2 = "abc" will change the memory address str2 is pointing to instead of doing that, and that new address refers to a read-only section that cannot be modified.

Havenard
  • 27,022
  • 5
  • 36
  • 62
-1

Issues:

  char *str2 = (char *)malloc(sizeof(char)*4);

This means that you have asked for 4 bytes of space in the memory for your personal use and you have been granted with the starting address of the space.

  str2= "def";

You got your personal address but now you are changing your address to point to the enchanting "def" which belongs to Read-Only Data section of the memory (String literals). This means that the original address allocated is dangling and cannot be traced back.

Issues:

  • Dangling pointer wherein the address would be untraceable.
  • you won't be able to free it.

str2[1]='s';

Ohh and now you are trying to be a rebel by trying to change the read only section's content.

Issues:

  • Trying to overwrite a read only segment which is not allowed.

  • SIGSEGV is causes i.e an error(signal) caused by an invalid memory reference or a segmentation fault).

What you might be actually be trying:

  • Allocating memory for your work (the 4 bytes)
  • Overwriting content to that memory strcpy(str2, "def");
  • str2[1] = 's' would also work then.
  • Please do free the str2 variable as you were the one who asked for it. (With great powers come great responsibility) ;)
thepace
  • 2,221
  • 1
  • 13
  • 21