Questions about C++ strings...

Hey, thanks for coming here. Currently I'm learning C++ and beyond. While I'm using C++ strings and come up with some inconsistencies.

Do we really need include string.h? Because sometimes it works sometimes not.

1
2
3
4
5
6
#include <iostream>

int main ()
{
    std::string myString {"This is a test"};
}


What's the size of string variable? It says 32 bytes using with sizeof() function (idk it's a method or function). What's the borders of a string? Because whether we use empty string and not empty string, it shows 32 bytes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <string>

int main ()
{
    std::string myString1 {};
    std::string myString2 {"Lorem ipsum dolor 
        sit amet, consectetur adipiscing elit. 
        Nam luctus hendrerit sagittis. 
        Curabitur sit amet porttitor nisi. 
        Aliquam mollis eros et ipsum 
        consectetur, nec sagittis libero 
        dignissim. Morbi sed ex eu elit 
        consequat eleifend. In dapibus, felis 
        vel tincidunt vehicula, purus mauris 
        tincidunt enim, eget rhoncus ligula 
        turpis eget neque. Phasellus in metus 
        molestie, feugiat ante in, sagittis 
        enim. Sed malesuada pretium nisi, a 
        fringilla est ornare nec. Integer 
        suscipit interdum metus, ut vulputate 
        sem commodo in. Quisque aliquam justo 
        libero, nec aliquet sapien lacinia sit 
        amet. Aenean sit amet dolor nunc. Duis 
        lobortis justo quis nisl ultrices 
        mattis. Proin consectetur sed elit sit 
        amet vestibulum. Ut vel ligula pulvinar 
        lectus viverra efficitur. Nunc dapibus 
        lacus non nisi tristique pretium."};

    std::cout << sizeof(myString1) << 
        std::endl;
    std::cout << sizeof(myString2) << 
        std::endl;

    return 0;
}
Last edited on
string.h is a C include file. For C++ you'd include cstring. It's used for the C string functions (strlen, strcmp, strcpy etc etc)

For C++ for std::string you'd include string. These shouldn't be confused.

If you use std::string, then you should always use #include <string> Depending upon other includes, it may not be strictly necessary as those other includes may also include string - but this shouldn't be relied upon.

std::string uses dynamic memory to store the data (there's also a Small String Optimisation in some implementations). This means that the content of the std::string is not stored within the std::string (ignoring SSO) but in memory external to the object. What this means is that sizeof() shows the size of the memory used by the std::string class itself - and not the size of the stored string. To get the size of the stored data then use .size().

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <string>

int main() {
    std::string myString1 {};
    std::string myString2 {"Lorem ipsum dolor\
        sit amet, consectetur adipiscing elit.\
        Nam luctus hendrerit sagittis.\
        Curabitur sit amet porttitor nisi.\
        Aliquam mollis eros et ipsum\
        consectetur, nec sagittis libero\
        dignissim.Morbi sed ex eu elit\
        consequat eleifend.In dapibus, felis\
        vel tincidunt vehicula, purus mauris\
        tincidunt enim, eget rhoncus ligula\
        turpis eget neque.Phasellus in metus\
        molestie, feugiat ante in, sagittis\
        enim.Sed malesuada pretium nisi, a\
        fringilla est ornare nec.Integer\
        suscipit interdum metus, ut vulputate\
        sem commodo in.Quisque aliquam justo\
        libero, nec aliquet sapien lacinia sit\
        amet.Aenean sit amet dolor nunc.Duis\
        lobortis justo quis nisl ultrices\
        mattis.Proin consectetur sed elit sit\
        amet vestibulum.Ut vel ligula pulvinar\
        lectus viverra efficitur.Nunc dapibus\
        lacus non nisi tristique pretium."};

    std::cout << myString1.size() << std::endl;
    std::cout << myString2.size() << std::endl;
}



0
954


Note that \ is a line continuation char as the string is split over multiple lines.

Last edited on
<string.h> is a C header, that contains functions to work with C-style strings (i.e. null-terminated char arrays). It only exists for backwards compatibility. <cstring> is the same as <string.h> but properly puts those functions inside the std namespace. But either way, those functions act on char arrays, not std::strings.

If you use a std::string object, you must #include <string>. Even if it works without it, by including the proper header, you make your code the most portable.

The sizeof a std::string object is implementation-defined. It does not depend on the actual content of the string. Many implementations will give extra space in std::strings to allow small-string optimization, but the actual sizeof(std::string) will always be the same regardless of the content of the string text itself.

sizeof(Type) is purely a compile-time constant. [Not including non-standard extensions.]
Last edited on
string.h is for C style strings, character arrays and tools to use those.
<string> is for C++ strings, which you are using here.

the standard string is rather small: it will use dynamic memory behind the scenes for the actual text data if the data is more than a few bytes in length. You can't see the amount allocated by the pointer, it is hidden from sizeof.

do you understand dynamic memory, smart pointers or anything similar? If not, that is what you need to know to understand what is happening. In a real short explanation, the string class has a pointer (probably 8 bytes) that refers to a location in memory where the text is stored. Because of this link between where it is stored and the object, sizeof is returning the size of the pointer (8 bytes) not the size of the text that it points to (it can't see that, sizeof isnt designed to dig into that). The sizeof is correct: it is how much space you need for a *string* variable. The string itself will take care of the extra memory; it does that for you by working with the operating system.


But it isn't important: string has a length() method that tells you how big the string data is. Is that all you need?
> Do we really need include string.h?

#include <string>


> Because sometimes it works sometimes not.

The sane thing to do is: if std::string is to be used, #include <string>

For more information, see http://www.cplusplus.com/forum/general/221668/#msg1017744


> What's the size of string variable?

To determine the number of characters in the string, use the size() member funtion.
For example: std::cout << myString2.size() << '\n'
What's the size of string variable? It says 32 bytes using with sizeof()

Implementations of C++ Standard Library have some liberties. The standard does not dictate how to write "class string" or which (other) headers to include in library headers.

The <iostream> in your compiler (compiler provides Standard Library implementation) on your system apparently includes (directly or indirectly) the <string>. Other compilers -- or other versions of your compiler -- might not.

On your system, with your compiler, the implementation of std::string uses 32 bytes. That is not true with all implementations and all systems.

Portable code compiles with all compilers on all systems and does not assume that size is X bytes.
Really thank you, guys. I learned that c++ header files are not having that .h extension. At least the MinGW compiler says that. And yes I learned again about how does a c++ program works. Thanks again.
Topic archived. No new replies allowed.