0

First of all i would like to know why exactly case 1 gives me core dump error. secondly I want to understand if my mindset is correct.

So i created 2 cases, in the first case when i execute, i get a core dumped message and the program exits.

The other case runs and i see the result.

    //case 1
    //'word' is not initialized properly,or by using malloc or calloc
    //as a result 'word' content will not change when you i use strcpy to copy over it
    char * word = "hello";
    printf("%s\n",word);
    strcpy(word,"world");
    printf("%s\n",word);

    //case 2
    //this case works because I create a static variable word with size 20
    //and everytime I change it, The compiler knows where to find it in the stack.
    char  word[20] = "";
    char * word2 = "yolo";
    strcpy(word,"hello");

    printf("%s\n",word); //prints hello as expected
    strcpy(word,word2); //copies yolo to word
    printf("%s\n",word); // prints yolo
   //hence strcpy does not change the reference
MohsenFM
  • 129
  • 1
  • 13

2 Answers2

1

//case 1

'word' is not initialized properly,or by using malloc or calloc

The word pointer is properly initialized with the address of the first character of a string literal "helllo".

as a result 'word' content will not change when you i use strcpy to copy over it

As a result, word pointer points to not modifiable memory and that memory can't be modified. Modifying the content of the memory where word points to results in undefined behavior of your program.

strcpy(word,"world")

Modifies the memory where the pointer word points to. This results in undefined behavior according to C. strcpy tries to write to a "read-only" memory or to a memory the system is not allowing the program to write to - on most systems this results in a segmentation fault, which is literally a failure condition associated with accessing a restricted memory area.

//case 2

this case works because I create a static variable word with size 20

The static keyword is (most commonly) associated in C with creating variables with static storage duration - this is not important here. The definition char word[20] tells the compiler to allocate storage for 20 chars. Such definition creates storage that can be modified. It does not matter if such declaration is used with the keyword static or not, the storage is accessible via word is modifiable.

and everytime I change it, The compiler knows where to find it in the stack.

Hm... The compiler will most probably always know where to find it on the stack - the address of word array - ie. &word - does not change. In strcpy(word,"hello"); you change the content of the array, the memory allocated with word array, not the array location.

Community
  • 1
  • 1
KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • So what i understand is, in the second case: "hello" is written somewhere in the stack and the first cell of the array word will point to the first cell of array "hello". is this correct? – MohsenFM Nov 27 '19 at 15:57
1

With char * word = "hello"; you have defined a string as a pointer to a char, initialised by a string literal. String literals are stored in a read only section of memory and are effectively constant.

The value is stored in a read-only section in the binary file and cannot be modified. If you compile to an assembly file (use the -S compiler option in gcc), you can see the string literals in the .rodata section. In this context, rodata means “read-only data”.

Though the string literal is read-only, you can modify your char * by pointing it to a different string literal:

char *word = "hello";
// Redefine the char pointer
word = "world";

This re-assignment is OK - you will see both string literals in your assembly file. In this case you can't use strcpy() to copy into word because it is immutable.

In case 2, you declare your string as a character array char word[20]. You are declaring an empty "string" (really a char array) with space for 20 characters. Copying word2 (a string literal) into word works because in this case word was declared as a character array and is therefore mutable.

csknk
  • 1,899
  • 1
  • 16
  • 27