how much memory is allocated to an uninitialized std::string variable?
up vote
-1
down vote
favorite
if i declare a variable of type std::string but do not initialize it, how much memory is allocated? I know that if I initialize it to "hello" for example, there will be a byte reserved for each character plus one for the null character, 6 total. Is there a default length defined somewhere in the string class?
(I've tried looking for the actually definition in the string header file but don't know where to find it)
c++ string memory
add a comment |
up vote
-1
down vote
favorite
if i declare a variable of type std::string but do not initialize it, how much memory is allocated? I know that if I initialize it to "hello" for example, there will be a byte reserved for each character plus one for the null character, 6 total. Is there a default length defined somewhere in the string class?
(I've tried looking for the actually definition in the string header file but don't know where to find it)
c++ string memory
Do we count the memory needed for the class itself or only for the data it is storing?
– Kamil Cuk
Nov 8 at 21:27
The place to look for an implementation is its default constructor and where the data members are declared, but it's by no means guaranteed to be consistent across implementations or versions.
– chris
Nov 8 at 21:28
1
It's likely 0 bytes, but it's not defined (as far as I can tell). And don't confuse memory allocated for the data and memory allocated for thestd::string(which is constant). Are you asking how much memory is used or how much is allocated? And somestd::stringimplementations use a small string optimization, so it can use thestd::string's memory to store short strings directly without allocating anything else. There are just so many ways this question can go it's hard to know what exactly you are asking about.
– François Andrieux
Nov 8 at 21:28
Are you asking about what happens on some particular platform, what typically happens, or what is guaranteed to happen?
– David Schwartz
Nov 8 at 21:29
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
if i declare a variable of type std::string but do not initialize it, how much memory is allocated? I know that if I initialize it to "hello" for example, there will be a byte reserved for each character plus one for the null character, 6 total. Is there a default length defined somewhere in the string class?
(I've tried looking for the actually definition in the string header file but don't know where to find it)
c++ string memory
if i declare a variable of type std::string but do not initialize it, how much memory is allocated? I know that if I initialize it to "hello" for example, there will be a byte reserved for each character plus one for the null character, 6 total. Is there a default length defined somewhere in the string class?
(I've tried looking for the actually definition in the string header file but don't know where to find it)
c++ string memory
c++ string memory
asked Nov 8 at 21:25
AndoKay
13
13
Do we count the memory needed for the class itself or only for the data it is storing?
– Kamil Cuk
Nov 8 at 21:27
The place to look for an implementation is its default constructor and where the data members are declared, but it's by no means guaranteed to be consistent across implementations or versions.
– chris
Nov 8 at 21:28
1
It's likely 0 bytes, but it's not defined (as far as I can tell). And don't confuse memory allocated for the data and memory allocated for thestd::string(which is constant). Are you asking how much memory is used or how much is allocated? And somestd::stringimplementations use a small string optimization, so it can use thestd::string's memory to store short strings directly without allocating anything else. There are just so many ways this question can go it's hard to know what exactly you are asking about.
– François Andrieux
Nov 8 at 21:28
Are you asking about what happens on some particular platform, what typically happens, or what is guaranteed to happen?
– David Schwartz
Nov 8 at 21:29
add a comment |
Do we count the memory needed for the class itself or only for the data it is storing?
– Kamil Cuk
Nov 8 at 21:27
The place to look for an implementation is its default constructor and where the data members are declared, but it's by no means guaranteed to be consistent across implementations or versions.
– chris
Nov 8 at 21:28
1
It's likely 0 bytes, but it's not defined (as far as I can tell). And don't confuse memory allocated for the data and memory allocated for thestd::string(which is constant). Are you asking how much memory is used or how much is allocated? And somestd::stringimplementations use a small string optimization, so it can use thestd::string's memory to store short strings directly without allocating anything else. There are just so many ways this question can go it's hard to know what exactly you are asking about.
– François Andrieux
Nov 8 at 21:28
Are you asking about what happens on some particular platform, what typically happens, or what is guaranteed to happen?
– David Schwartz
Nov 8 at 21:29
Do we count the memory needed for the class itself or only for the data it is storing?
– Kamil Cuk
Nov 8 at 21:27
Do we count the memory needed for the class itself or only for the data it is storing?
– Kamil Cuk
Nov 8 at 21:27
The place to look for an implementation is its default constructor and where the data members are declared, but it's by no means guaranteed to be consistent across implementations or versions.
– chris
Nov 8 at 21:28
The place to look for an implementation is its default constructor and where the data members are declared, but it's by no means guaranteed to be consistent across implementations or versions.
– chris
Nov 8 at 21:28
1
1
It's likely 0 bytes, but it's not defined (as far as I can tell). And don't confuse memory allocated for the data and memory allocated for the
std::string (which is constant). Are you asking how much memory is used or how much is allocated? And some std::string implementations use a small string optimization, so it can use the std::string's memory to store short strings directly without allocating anything else. There are just so many ways this question can go it's hard to know what exactly you are asking about.– François Andrieux
Nov 8 at 21:28
It's likely 0 bytes, but it's not defined (as far as I can tell). And don't confuse memory allocated for the data and memory allocated for the
std::string (which is constant). Are you asking how much memory is used or how much is allocated? And some std::string implementations use a small string optimization, so it can use the std::string's memory to store short strings directly without allocating anything else. There are just so many ways this question can go it's hard to know what exactly you are asking about.– François Andrieux
Nov 8 at 21:28
Are you asking about what happens on some particular platform, what typically happens, or what is guaranteed to happen?
– David Schwartz
Nov 8 at 21:29
Are you asking about what happens on some particular platform, what typically happens, or what is guaranteed to happen?
– David Schwartz
Nov 8 at 21:29
add a comment |
3 Answers
3
active
oldest
votes
up vote
3
down vote
accepted
It's unspecified. Different implementations may allocate different amounts of memory upon default-construction, and an implementation is not required to tell you how much memory that is. However, I believe that right now it's most common for std::string to employ a short string optimization, under which a default-constructed std::string need not allocate any memory at all besides the size of the std::string class itself. See Meaning of acronym SSO in the context of std::string for details. Note that sizeof(std::string) is also unspecified.
add a comment |
up vote
3
down vote
While that is unspecified, there is something worth mentioning: In practice, implementations avoid allocating memory for uninitialized strings.
I have performed a few tests for usual sizeof(std::string) using Compiler Explorer (link to the test). Here are the results, though you can of course experiment with more:
- gcc / Linux / x86-64 / libstdc++ : 32 bytes
- gcc / Linux / x86 / libstdc++ : 24 bytes
- clang / Linux / x86-64 / libc++ (NB: not libstdc++!) : 24 bytes
- clang / Linux / x86 / libc++ : 12 bytes
- msvc / Windows / x86-64 / VC runtime : 32 bytes
- msvc / Windows / x86 / VC runtime : 24 bytes
- gcc / Linux / ARM64 / libstdc++ : 32 bytes
- gcc / Linux / ARM (32-bit) / libstdc++ : 24 bytes
- msvc / Windows / ARM64 / VC runtime : 32 bytes (NB: output is in hex on CE)
Roughly speaking, in practice, a std::string object is going to be 12 to 32 bytes big, excluding the dynamically allocated memory.
These results are depending mostly on the standard library implementation and CPU architecture (because of whatever standard library feels like doing).
Note that these sizes do include SSO, which @Brian discussed in his answer. I believe this is the motive for libstdc++ and MS' implementation to use 32 bytes rather than 24 (since I suspect that there are usually 3 pointers involved: data begin, data end and capacity end), though I don't know the specifics.
add a comment |
up vote
0
down vote
You can always check youself. It depends on the computer you are at, to check datatype sizes you can always use size:of.
In your case it would look like this
#include <iostream>
#include <string>
int main()
std::string a = "";
std::cout << sizeof(a) << 'n';
system("PAUSE");
return 0;
I get 28 bytes.
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
It's unspecified. Different implementations may allocate different amounts of memory upon default-construction, and an implementation is not required to tell you how much memory that is. However, I believe that right now it's most common for std::string to employ a short string optimization, under which a default-constructed std::string need not allocate any memory at all besides the size of the std::string class itself. See Meaning of acronym SSO in the context of std::string for details. Note that sizeof(std::string) is also unspecified.
add a comment |
up vote
3
down vote
accepted
It's unspecified. Different implementations may allocate different amounts of memory upon default-construction, and an implementation is not required to tell you how much memory that is. However, I believe that right now it's most common for std::string to employ a short string optimization, under which a default-constructed std::string need not allocate any memory at all besides the size of the std::string class itself. See Meaning of acronym SSO in the context of std::string for details. Note that sizeof(std::string) is also unspecified.
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
It's unspecified. Different implementations may allocate different amounts of memory upon default-construction, and an implementation is not required to tell you how much memory that is. However, I believe that right now it's most common for std::string to employ a short string optimization, under which a default-constructed std::string need not allocate any memory at all besides the size of the std::string class itself. See Meaning of acronym SSO in the context of std::string for details. Note that sizeof(std::string) is also unspecified.
It's unspecified. Different implementations may allocate different amounts of memory upon default-construction, and an implementation is not required to tell you how much memory that is. However, I believe that right now it's most common for std::string to employ a short string optimization, under which a default-constructed std::string need not allocate any memory at all besides the size of the std::string class itself. See Meaning of acronym SSO in the context of std::string for details. Note that sizeof(std::string) is also unspecified.
answered Nov 8 at 21:29
Brian
63.1k793177
63.1k793177
add a comment |
add a comment |
up vote
3
down vote
While that is unspecified, there is something worth mentioning: In practice, implementations avoid allocating memory for uninitialized strings.
I have performed a few tests for usual sizeof(std::string) using Compiler Explorer (link to the test). Here are the results, though you can of course experiment with more:
- gcc / Linux / x86-64 / libstdc++ : 32 bytes
- gcc / Linux / x86 / libstdc++ : 24 bytes
- clang / Linux / x86-64 / libc++ (NB: not libstdc++!) : 24 bytes
- clang / Linux / x86 / libc++ : 12 bytes
- msvc / Windows / x86-64 / VC runtime : 32 bytes
- msvc / Windows / x86 / VC runtime : 24 bytes
- gcc / Linux / ARM64 / libstdc++ : 32 bytes
- gcc / Linux / ARM (32-bit) / libstdc++ : 24 bytes
- msvc / Windows / ARM64 / VC runtime : 32 bytes (NB: output is in hex on CE)
Roughly speaking, in practice, a std::string object is going to be 12 to 32 bytes big, excluding the dynamically allocated memory.
These results are depending mostly on the standard library implementation and CPU architecture (because of whatever standard library feels like doing).
Note that these sizes do include SSO, which @Brian discussed in his answer. I believe this is the motive for libstdc++ and MS' implementation to use 32 bytes rather than 24 (since I suspect that there are usually 3 pointers involved: data begin, data end and capacity end), though I don't know the specifics.
add a comment |
up vote
3
down vote
While that is unspecified, there is something worth mentioning: In practice, implementations avoid allocating memory for uninitialized strings.
I have performed a few tests for usual sizeof(std::string) using Compiler Explorer (link to the test). Here are the results, though you can of course experiment with more:
- gcc / Linux / x86-64 / libstdc++ : 32 bytes
- gcc / Linux / x86 / libstdc++ : 24 bytes
- clang / Linux / x86-64 / libc++ (NB: not libstdc++!) : 24 bytes
- clang / Linux / x86 / libc++ : 12 bytes
- msvc / Windows / x86-64 / VC runtime : 32 bytes
- msvc / Windows / x86 / VC runtime : 24 bytes
- gcc / Linux / ARM64 / libstdc++ : 32 bytes
- gcc / Linux / ARM (32-bit) / libstdc++ : 24 bytes
- msvc / Windows / ARM64 / VC runtime : 32 bytes (NB: output is in hex on CE)
Roughly speaking, in practice, a std::string object is going to be 12 to 32 bytes big, excluding the dynamically allocated memory.
These results are depending mostly on the standard library implementation and CPU architecture (because of whatever standard library feels like doing).
Note that these sizes do include SSO, which @Brian discussed in his answer. I believe this is the motive for libstdc++ and MS' implementation to use 32 bytes rather than 24 (since I suspect that there are usually 3 pointers involved: data begin, data end and capacity end), though I don't know the specifics.
add a comment |
up vote
3
down vote
up vote
3
down vote
While that is unspecified, there is something worth mentioning: In practice, implementations avoid allocating memory for uninitialized strings.
I have performed a few tests for usual sizeof(std::string) using Compiler Explorer (link to the test). Here are the results, though you can of course experiment with more:
- gcc / Linux / x86-64 / libstdc++ : 32 bytes
- gcc / Linux / x86 / libstdc++ : 24 bytes
- clang / Linux / x86-64 / libc++ (NB: not libstdc++!) : 24 bytes
- clang / Linux / x86 / libc++ : 12 bytes
- msvc / Windows / x86-64 / VC runtime : 32 bytes
- msvc / Windows / x86 / VC runtime : 24 bytes
- gcc / Linux / ARM64 / libstdc++ : 32 bytes
- gcc / Linux / ARM (32-bit) / libstdc++ : 24 bytes
- msvc / Windows / ARM64 / VC runtime : 32 bytes (NB: output is in hex on CE)
Roughly speaking, in practice, a std::string object is going to be 12 to 32 bytes big, excluding the dynamically allocated memory.
These results are depending mostly on the standard library implementation and CPU architecture (because of whatever standard library feels like doing).
Note that these sizes do include SSO, which @Brian discussed in his answer. I believe this is the motive for libstdc++ and MS' implementation to use 32 bytes rather than 24 (since I suspect that there are usually 3 pointers involved: data begin, data end and capacity end), though I don't know the specifics.
While that is unspecified, there is something worth mentioning: In practice, implementations avoid allocating memory for uninitialized strings.
I have performed a few tests for usual sizeof(std::string) using Compiler Explorer (link to the test). Here are the results, though you can of course experiment with more:
- gcc / Linux / x86-64 / libstdc++ : 32 bytes
- gcc / Linux / x86 / libstdc++ : 24 bytes
- clang / Linux / x86-64 / libc++ (NB: not libstdc++!) : 24 bytes
- clang / Linux / x86 / libc++ : 12 bytes
- msvc / Windows / x86-64 / VC runtime : 32 bytes
- msvc / Windows / x86 / VC runtime : 24 bytes
- gcc / Linux / ARM64 / libstdc++ : 32 bytes
- gcc / Linux / ARM (32-bit) / libstdc++ : 24 bytes
- msvc / Windows / ARM64 / VC runtime : 32 bytes (NB: output is in hex on CE)
Roughly speaking, in practice, a std::string object is going to be 12 to 32 bytes big, excluding the dynamically allocated memory.
These results are depending mostly on the standard library implementation and CPU architecture (because of whatever standard library feels like doing).
Note that these sizes do include SSO, which @Brian discussed in his answer. I believe this is the motive for libstdc++ and MS' implementation to use 32 bytes rather than 24 (since I suspect that there are usually 3 pointers involved: data begin, data end and capacity end), though I don't know the specifics.
answered Nov 8 at 21:52
Asu
1,3511021
1,3511021
add a comment |
add a comment |
up vote
0
down vote
You can always check youself. It depends on the computer you are at, to check datatype sizes you can always use size:of.
In your case it would look like this
#include <iostream>
#include <string>
int main()
std::string a = "";
std::cout << sizeof(a) << 'n';
system("PAUSE");
return 0;
I get 28 bytes.
add a comment |
up vote
0
down vote
You can always check youself. It depends on the computer you are at, to check datatype sizes you can always use size:of.
In your case it would look like this
#include <iostream>
#include <string>
int main()
std::string a = "";
std::cout << sizeof(a) << 'n';
system("PAUSE");
return 0;
I get 28 bytes.
add a comment |
up vote
0
down vote
up vote
0
down vote
You can always check youself. It depends on the computer you are at, to check datatype sizes you can always use size:of.
In your case it would look like this
#include <iostream>
#include <string>
int main()
std::string a = "";
std::cout << sizeof(a) << 'n';
system("PAUSE");
return 0;
I get 28 bytes.
You can always check youself. It depends on the computer you are at, to check datatype sizes you can always use size:of.
In your case it would look like this
#include <iostream>
#include <string>
int main()
std::string a = "";
std::cout << sizeof(a) << 'n';
system("PAUSE");
return 0;
I get 28 bytes.
answered Nov 9 at 5:28
Allamo Olsson
849
849
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53216377%2fhow-much-memory-is-allocated-to-an-uninitialized-stdstring-variable%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Do we count the memory needed for the class itself or only for the data it is storing?
– Kamil Cuk
Nov 8 at 21:27
The place to look for an implementation is its default constructor and where the data members are declared, but it's by no means guaranteed to be consistent across implementations or versions.
– chris
Nov 8 at 21:28
1
It's likely 0 bytes, but it's not defined (as far as I can tell). And don't confuse memory allocated for the data and memory allocated for the
std::string(which is constant). Are you asking how much memory is used or how much is allocated? And somestd::stringimplementations use a small string optimization, so it can use thestd::string's memory to store short strings directly without allocating anything else. There are just so many ways this question can go it's hard to know what exactly you are asking about.– François Andrieux
Nov 8 at 21:28
Are you asking about what happens on some particular platform, what typically happens, or what is guaranteed to happen?
– David Schwartz
Nov 8 at 21:29