0

I have to write a piece of code and deploy it in a system with limited memory space and was thinking about char arrays vs string . I wrote the following piece of code to get a better idea.

    string str="abcde";
    char carray[6]={'a','b','c','d','e','\0'};
    cout <<"size of char array:"<< sizeof(carray) << endl ;
    cout <<"size of string:"<< sizeof(str) << endl ;

The output I get is

size of char array:6
size of string:28

So, I have 2 questions

  1. Can I conclude that using the string will be a foolish idea given that it is taking much more space, given that memory space is tight and the max chars in my array wont be greater than 10?
  2. Why the hell is size of string showing as 28? on what basis?
Nostalgic
  • 300
  • 1
  • 4
  • 18
  • 4
    For `std::string`, use `str.size()` function – Inian Jun 28 '20 at 15:22
  • I compiled this on my laptop and it gave me: `size of char array:6 size of string:4`. This has me even more confused, towards why would a 5-character string occupy only 4 bytes... – H-005 Jun 28 '20 at 15:22
  • 2
    Does this answer your question? [Why is sizeof(std::string) only eight bytes?](https://stackoverflow.com/questions/34560502/why-is-sizeofstdstring-only-eight-bytes) – Fureeish Jun 28 '20 at 15:23
  • 4
    `sizeof(str)` is the size of `std::string` object - a compile-time constant. It's unrelated to the length of the character sequence this object manages (which could vary at run time). That length is reported by `str.size()` or `str.length()` member functions. – Igor Tandetnik Jun 28 '20 at 15:24
  • @Inian : what I think is that size() function returns the number of chars inside the string object, not the size it has taken in memory – Nostalgic Jun 28 '20 at 15:24
  • @Igor Tandetnik: I am concerned about memory usage and not the number of characters – Nostalgic Jun 28 '20 at 15:26
  • 2
    Consider: `char* p = new char[100];`. Here, `sizeof(p)` is the amount of memory the pointer itself occupies (usually 4 or 8), and is unrelated to the size of the block of memory it points to. So which value do you care about - 4/8, or 100? – Igor Tandetnik Jun 28 '20 at 15:26
  • @IgorTandetnik : I got your point. would you suggest the std::string or char[] for smaller char arrays .for approx maximum 10 chars – Nostalgic Jun 28 '20 at 15:33
  • Use `string` everywhere. – john Jun 28 '20 at 15:36
  • Rather depends on what you plan to do with those arrays. Are you going to have so many of them that the overhead of `std::string` would become an issue? What kinds of operations would you need to perform on them? – Igor Tandetnik Jun 28 '20 at 15:37
  • There will be thousands of them.. small ones but big in number.. they will act more like constants.. means once assigned some value.. they wont be changed – Nostalgic Jun 28 '20 at 15:39
  • 1
    Depends on what you're going to do with the string, how permanent it is, and just how tight the memory constraints are. I start with `std::string` and only migrate to `char` arrays when examination of the program tells me that have to. – user4581301 Jun 28 '20 at 15:39

1 Answers1

2

std::string is a class object in C++. The size of it is implementation-defined, but will generally require space for at least both a pointer to a heap-allocated string, and a size. The sizeof(std::string) you are seeing may be specific to your implementation -- but it could be partially due to a small-buffer optimization where small strings are stored directly in the string.

An array of chars is just an array of characters, so sizeof(arr) will always be the the number of characters.


As for whether you can use std::string: it depends on how constrained you are. std::string will often use heap memory for larger strings, which may be hard to justify on extremely constrained systems. However, most std::string implementations also have support for small-buffer-optimized strings, where these strings are stored directly in the std::string rather than indirectly via heap memory

If heap memory is not justifyable, std::string will not be a good solution for you.

However, if that's the case, I would recommend you look into either adopting an existing static_string implementation that encodes the size as a template argument, or at least writing your own. This would be better than using char arrays everywhere, since this becomes hard to manage long-term.

Human-Compiler
  • 11,022
  • 1
  • 32
  • 59
  • Sounds like this will be the accepted answer. Just a couple of points. There will be thousands of them and once assigned some value they wont be changed. small ones but big in number. so heap memory is a bit of a catch here. But that static implementation I am going to try. – Nostalgic Jun 28 '20 at 15:43
  • 1
    If it's thousands of them, then heap memory is definitely a tough pill to swallow in a constrained system... `static_string` is probably your best bet. There are many open-source implementations of similar concepts available out there -- so it's a good place to start – Human-Compiler Jun 28 '20 at 15:48