1

Consider this function:

void useless() {
   char data[] = "aaa";
}

From what I learned here, the "aaa" literal lives to the end of the program. However, the data[] (initialized by the literal) is local, so it lives only to the end of the function.

The memory is copied, so the program needs 4B for the literal, 4B for the data and sizeof(size_t) bytes for the pointer to data and sizeof(size_t) for the pointer of the literal - is this true?

If the literal has static storage duration, no new memory is allocated for the local literal by the second call - is this true?

Community
  • 1
  • 1
Jan Turoň
  • 31,451
  • 23
  • 125
  • 169

2 Answers2

2
   char data[] = "aaa";

This is not a string literal but just an array. So there's no pointer there and memory is allocated only for the data.

If the literal has static storage duration, no new memory is allocated for the local literal by the second call

This is true for string literals like: char *s="aaa"; From the standard:

2.13. Sttring literals
[...]An ordinary string literal has type “array of n const char” and static storage duration (3.7)

P.P
  • 117,907
  • 20
  • 175
  • 238
  • So if we write `char data[] = "aaa"` no string literal `aaa` is created in memory? – Jan Turoň Sep 08 '12 at 08:54
  • Just an array is created which has auto storage which can be modified.But modifying string literals lead to undefined behavior. String literals may be stored in Read-only memory which is implementation defined. – P.P Sep 08 '12 at 08:55
  • @KingsIndian NOPE, not this way. If you initialize an auto array like this: `charr arr[] = "String literal";` then **you can safely modify the contents of `arr` afterwards.**. –  Sep 08 '12 at 09:05
  • @H2CO3 I didn't say anything different. If you were referring to my previous comment, I said you can modify array but can't modify string literal. – P.P Sep 08 '12 at 09:07
  • Nitpick: You're quoting from the C++ standard, but this question is about C. The difference: in C a string literal has type `char[N]`, not `const char[N]`. – Daniel Fischer Sep 08 '12 at 17:17
1

There is no pointer variable here. All there is an array, which is 4 bytes.

The compiler may or may not store the literal itself in memory; if it does, that is another 4 bytes.
Note that any memory taken up by anything other than the array itself is implementation-dependent.

I'm not sure what you mean by the "second call", but in general when you create an array, it takes up some amount of size... so if you create two arrays with the same literal, the compiler allocates space for two arrays (and perhaps -- or perhaps not -- for the literal also).

user541686
  • 205,094
  • 128
  • 528
  • 886
  • So how the compiler knows where is the literal and the array stored? Is there a way how to control if 4 or 2x4 bytes will be used? – Jan Turoň Sep 08 '12 at 08:51
  • If we call `useless` for the first time, memory for `data` is allocated on the stack and released after RET. I we call it for the second time: is new memory allocated, or is the static `aaa` used? Is there the literal created at all, or is it translated into `char data[] = {'a','a','a','\0'};`? – Jan Turoň Sep 08 '12 at 08:59
  • @JanTuroň: What I'm telling you is that it's implementation-dependent. I've seen *both* cases -- cases where the literal is global, and cases where the array's elements are set directly, via `mov , ` operations. It just depends on your compiler and optimization settings; sometimes optimizing for size gives you something different than optimizing for speed. There's no single correct answer. – user541686 Sep 08 '12 at 09:04