3

CONTEXT

I am studying the array definitions. My question sticks with char array for simplicity. Debian 64bit.

PROBLEM

When I declare those arrays, here is my understanding :

char d[10]; 

=> I get 10 * 1 byte memory usage.

char d[] = "hellofooo"; 

=> 9 bytes + '\0' => 10 * 1 byte memory usage

char * d = "hellofooo";

=> 9 bytes + '\0' somewhere in the memory + 8 bytes for the pointer (64 bits)

So, if my understanding is good, we should always prefer the second solution [] to minimise the memory consumption when we do not know upfront the size of the char?

There is no need for pointers in my code. Just trying to find the most ram efficient solution.

Community
  • 1
  • 1
Larry
  • 1,735
  • 1
  • 18
  • 46
  • Options 2 and 3 are completely different semantically. And 2 you get potentially the string literal somewhere in non-modifiable memory + a copy of that stored in `d`. – Mat May 24 '14 at 10:06
  • So 2 and 3 have the same effect in ram usage ? considering i don't need pointers; – Larry May 24 '14 at 10:07
  • No, 2) is potentially more costly (10+10), but that's absolutely not a reason to choose one over the other - they don't do the same thing at all. – Mat May 24 '14 at 10:09
  • 3
    All other things equal, I use `char const d[] = "hellofooo";`. You should find that the compiler just makes `d` designate the string directly in the string table. – M.M May 24 '14 at 10:09
  • And for completeness: The 3rd case does **not** define an array but a pointer. – alk May 24 '14 at 10:11
  • 1
    http://stackoverflow.com/questions/7903551/when-to-use-const-char-and-when-to-use-const-char – Mat May 24 '14 at 10:11
  • @MattMcNabb : Mat said that there is a copy in d anyway, which would tend to a higher memory usage in final. So ? What should I do ? to fulfill my goal ? – Larry May 24 '14 at 10:12
  • @alk : exact. I took a shortcut there. – Larry May 24 '14 at 10:13
  • @Larry Conceptually `d` is separate to the string literal, but in practice this gets optimized so that `d` designates the string literal. – M.M May 24 '14 at 10:14
  • Of course the same thing could happen if you went `char const *const d = "hellofooo";` , this is really an aesthetics issue – M.M May 24 '14 at 10:15
  • Thanks you all. In case I don't need pointers, I will use 2 and 3 in other cases. – Larry May 24 '14 at 10:35
  • @MattMcNabb isn't that `const char *const d = "hellofooo";`? – Peter Varo May 24 '14 at 10:50
  • @PeterVaro those have the same meaning but I think `char const` is better style . it's consistent with how `const` applies in more complicated declarations. Left-most `const` is a special case. – M.M May 24 '14 at 11:09
  • well, this is probably the question of personal preferences, but for me saying `const char` and then `*const d` is more powerful, as it is: *"declare d as const pointer to pointer to const char"* – Peter Varo May 24 '14 at 11:12
  • that isn't what it means.. there's only one level of pointing. – M.M May 24 '14 at 11:13
  • oh ofc, you are right.. it is *declare d as const pointer to const char* -- silly me, sorry.. – Peter Varo May 24 '14 at 11:15

0 Answers0