I want to know why this works without having to bind memory for a string

Hi guys, I recently chose C programming and I am stuck on understanding pointers. As I understand it, in order to store a value in a pointer, you have to bind the memory (using malloc) to the size of the value you want to store. With that in mind, the following code shouldn't work as I haven't allocated 11 bytes of memory to store my 11 bytes string, but for some reason other than my understanding it works fine.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(){

  char *str = NULL;

  str = "hello world\0";
  printf("filename = %s\n", str);
  return 0;
}

      

+3


source to share


5 answers


When you declare a string literal in a C program, it is stored in a read - only section of the program code. The form statement char *str = "hello";

assigns the address of this string to the pointer char*

. However, the string itself (ie, symbols h

, e

, l

, l

and o

, plus a terminator line \0

) are still in the non-volatile memory, so you do not change them.



Note that you do not need to explicitly add the null byte terminator to your string declarations. The C compiler will do this for you.

+1


source


In this case

 str = "hello world\0";

      

str

points to the address of the first element of the array chars

initialized with "hello world\0"

. In other words, it str

points to a "string literal".

By definition, an array is allocated, and the address of the first element must be "valid".

Quote C11

, chapter ยง6.4.5, String literals



In phase 7 translation, a byte or code of zero value is added to each multibyte the character sequence that is obtained from the string literal or literals. 78) Multibyte character sequence is used to initialize an array of static storage duration and length sufficient to contain the sequence. For character string literals, the array elements have a type char and are initialized with individual bytes of a multibyte character sequence. [....]

Allocating memory still , just not explicitly for you (via memory allocation functions).


However, "...\0"

at the end is repeated, as mentioned (in the first statement of the quote) above, by default the array will be null-terminated.

+3


source


Using a variable char

without malloc

means that the string you are assigning is read-only. This means that you are creating a pointer to a string constant. "hello world\0"

is somewhere in a read-only part of memory and you just point to it.

Now if you want to make changes to the line. Let's say changing the value h

to h

, it will be str[0]='H'

. Without malloc

, which will be impossible to do.

+2


source


Right. But in this case, you are simply pointing to a string literal that fits into the persistent memory area. Your pointer is created on the stack area. Thus, you are simply pointing to a different address. that is, at the starting address of the string literal.

Try using a string literal on a pointer variable. Then it will give an error because you did not allocate memory. Hope you can understand now.

+1


source


Storage for string literals is deferred at program startup and held until the program exits. This storage might be read-only, and trying to change the contents of a string literal results in undefined behavior (it might work, it might crash, it might do something in between).

0


source







All Articles