Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How C strings are allocated in memory?

Tags:

People also ask

How are C string stored in a memory?

The C language does not have a specific "String" data type, the way some other languages such as C++ and Java do. Instead C stores strings of characters as arrays of chars, terminated by a null byte.

Do you have to allocate memory for strings in C?

If your program needs to create a string of varying lengths then you'll have to allocate the memory yourself using malloc. In duplicating a string, s, for example we would need to find the length of that string: int len = strlen(s);

How does C allocate memory?

In C, dynamic memory is allocated from the heap using some standard library functions. The two key dynamic memory functions are malloc() and free(). The malloc() function takes a single parameter, which is the size of the requested memory area in bytes. It returns a pointer to the allocated memory.

How is memory allocated to string?

Thanks to the immutability of Strings in Java, the JVM can optimize the amount of memory allocated for them by storing only one copy of each literal String in the pool. This process is called interning. When we create a String variable and assign a value to it, the JVM searches the pool for a String of equal value.


Say I have a simple function that returns a C string this way:

const char * getString()
{
  const char * ptr = "blah blah";
  return ptr; 
}

and I call getString() from main() this way:

  const char * s = getString();

1) According to gdb, the variable ptr is stored on the stack, but the string pointed by ptr is not:

(gdb) p &ptr
$1 = (const char **) 0x7fffffffe688

(gdb) p ptr
$2 = 0x4009fc "blah blah"

Does this mean that "blah blah" is not a local variable inside getString()?

I guess that if it were a local variable, I would not be able to pass it to my main() function... But if it's not, where is it stored? On the heap? Is that a "kind of" dynamically memory allocation implemented by the OS every time it hits on a string, or what?

2) If I use an array instead of a pointer, this way:

const char *getString2()
{
  const char a[] = "blah blah blah";
  return a;
}

the compiler warns me that:

warning: address of local variable ‘a’ returned

(and of course the program compiles, but it doesn't work).

Actually, if I ask gdb, I get

(gdb) p &a
$2 = (const char (*)[15]) 0x7fffffffe690

But I thought that const char * ptr and const char a[] were basically the same thing. Looks like they're not.

Am I wrong? What is exactely the difference between the two versions?

Thank you!