How memory is allocated for vector in C++?
Clash Royale CLAN TAG#URR8PPP
up vote
6
down vote
favorite
#include <vector>
#include <string>
#include <iostream>
using namespace std;
int main()
vector<char> vChar;
vChar.push_back('a');
vChar.push_back('b');
vChar.push_back('c');
vChar.push_back('d');
vector<int> vInt;
vInt.push_back(1);
vInt.push_back(2);
vInt.push_back(3);
vInt.push_back(4);
cout << "For char vector Size:" << vChar.size() << " Capacity:" << vChar.capacity() << "n";
for(int i=0; i < vChar.size(); i++)
cout << "Data: " << vChar[i] << " Address:" << &vChar[i] << "n";
cout << "nFor int vector Size:" << vInt.size() << " Capacity:" << vInt.capacity() << "n";
for (int i = 0; i < vInt.size(); i++)
cout << "Data: " << vInt[i] << " Address:" << &vInt[i] << "n";
return 0;
Sample output for the code above is:
For char vector Size:4 Capacity:4
Data: a Address:abcdòòòòñPâ¿âÂÂâ¬
Data: b Address:bcdòòòòñPâ¿âÂÂâ¬
Data: c Address:cdòòòòñPâ¿âÂÂâ¬
Data: d Address:dòòòòñPâ¿âÂÂâ¬
For int vector Size:4 Capacity:4
Data: 1 Address:000001F020F80420
Data: 2 Address:000001F020F80424
Data: 3 Address:000001F020F80428
Data: 4 Address:000001F020F8042C
For every primitive data types memory locations are contiguous, except for char. It prints some garbage value on screen.
I tried addidng v.reserve(4) but the output was same.
c++ vector c++17
add a comment |Â
up vote
6
down vote
favorite
#include <vector>
#include <string>
#include <iostream>
using namespace std;
int main()
vector<char> vChar;
vChar.push_back('a');
vChar.push_back('b');
vChar.push_back('c');
vChar.push_back('d');
vector<int> vInt;
vInt.push_back(1);
vInt.push_back(2);
vInt.push_back(3);
vInt.push_back(4);
cout << "For char vector Size:" << vChar.size() << " Capacity:" << vChar.capacity() << "n";
for(int i=0; i < vChar.size(); i++)
cout << "Data: " << vChar[i] << " Address:" << &vChar[i] << "n";
cout << "nFor int vector Size:" << vInt.size() << " Capacity:" << vInt.capacity() << "n";
for (int i = 0; i < vInt.size(); i++)
cout << "Data: " << vInt[i] << " Address:" << &vInt[i] << "n";
return 0;
Sample output for the code above is:
For char vector Size:4 Capacity:4
Data: a Address:abcdòòòòñPâ¿âÂÂâ¬
Data: b Address:bcdòòòòñPâ¿âÂÂâ¬
Data: c Address:cdòòòòñPâ¿âÂÂâ¬
Data: d Address:dòòòòñPâ¿âÂÂâ¬
For int vector Size:4 Capacity:4
Data: 1 Address:000001F020F80420
Data: 2 Address:000001F020F80424
Data: 3 Address:000001F020F80428
Data: 4 Address:000001F020F8042C
For every primitive data types memory locations are contiguous, except for char. It prints some garbage value on screen.
I tried addidng v.reserve(4) but the output was same.
c++ vector c++17
6
The title doesn't have anything to do with the text of the question (which seems to be asking why garbage is printed). Please clarify what your question actually is.
â M.M
55 mins ago
@M.M The two very obviously have a direct correlation. The way it's stored is, in part, the cause of the garbage being printed. Whether that means allocating the memory for it without zeroing it out, etc. Resulting in garbage occupying that spot in memory. I would think he actually went a step further and found part of the problem and in fact asked a more specific question. +1
â Edward Severinsen
37 mins ago
1
Hard to say exactly what happens memory-wise. The C++ Standard doesn't specify low-level details like where the memory comes from and how much you get so long as it's at least the amount you asked for. The storage could be a billion gigabyte block in an RNA storage device on Pluto and accessed via quantum entanglement for all the program knows.
â user4581301
37 mins ago
add a comment |Â
up vote
6
down vote
favorite
up vote
6
down vote
favorite
#include <vector>
#include <string>
#include <iostream>
using namespace std;
int main()
vector<char> vChar;
vChar.push_back('a');
vChar.push_back('b');
vChar.push_back('c');
vChar.push_back('d');
vector<int> vInt;
vInt.push_back(1);
vInt.push_back(2);
vInt.push_back(3);
vInt.push_back(4);
cout << "For char vector Size:" << vChar.size() << " Capacity:" << vChar.capacity() << "n";
for(int i=0; i < vChar.size(); i++)
cout << "Data: " << vChar[i] << " Address:" << &vChar[i] << "n";
cout << "nFor int vector Size:" << vInt.size() << " Capacity:" << vInt.capacity() << "n";
for (int i = 0; i < vInt.size(); i++)
cout << "Data: " << vInt[i] << " Address:" << &vInt[i] << "n";
return 0;
Sample output for the code above is:
For char vector Size:4 Capacity:4
Data: a Address:abcdòòòòñPâ¿âÂÂâ¬
Data: b Address:bcdòòòòñPâ¿âÂÂâ¬
Data: c Address:cdòòòòñPâ¿âÂÂâ¬
Data: d Address:dòòòòñPâ¿âÂÂâ¬
For int vector Size:4 Capacity:4
Data: 1 Address:000001F020F80420
Data: 2 Address:000001F020F80424
Data: 3 Address:000001F020F80428
Data: 4 Address:000001F020F8042C
For every primitive data types memory locations are contiguous, except for char. It prints some garbage value on screen.
I tried addidng v.reserve(4) but the output was same.
c++ vector c++17
#include <vector>
#include <string>
#include <iostream>
using namespace std;
int main()
vector<char> vChar;
vChar.push_back('a');
vChar.push_back('b');
vChar.push_back('c');
vChar.push_back('d');
vector<int> vInt;
vInt.push_back(1);
vInt.push_back(2);
vInt.push_back(3);
vInt.push_back(4);
cout << "For char vector Size:" << vChar.size() << " Capacity:" << vChar.capacity() << "n";
for(int i=0; i < vChar.size(); i++)
cout << "Data: " << vChar[i] << " Address:" << &vChar[i] << "n";
cout << "nFor int vector Size:" << vInt.size() << " Capacity:" << vInt.capacity() << "n";
for (int i = 0; i < vInt.size(); i++)
cout << "Data: " << vInt[i] << " Address:" << &vInt[i] << "n";
return 0;
Sample output for the code above is:
For char vector Size:4 Capacity:4
Data: a Address:abcdòòòòñPâ¿âÂÂâ¬
Data: b Address:bcdòòòòñPâ¿âÂÂâ¬
Data: c Address:cdòòòòñPâ¿âÂÂâ¬
Data: d Address:dòòòòñPâ¿âÂÂâ¬
For int vector Size:4 Capacity:4
Data: 1 Address:000001F020F80420
Data: 2 Address:000001F020F80424
Data: 3 Address:000001F020F80428
Data: 4 Address:000001F020F8042C
For every primitive data types memory locations are contiguous, except for char. It prints some garbage value on screen.
I tried addidng v.reserve(4) but the output was same.
c++ vector c++17
c++ vector c++17
edited 55 mins ago
eyllanesc
57.9k82449
57.9k82449
asked 57 mins ago
Palash Tichkule
445
445
6
The title doesn't have anything to do with the text of the question (which seems to be asking why garbage is printed). Please clarify what your question actually is.
â M.M
55 mins ago
@M.M The two very obviously have a direct correlation. The way it's stored is, in part, the cause of the garbage being printed. Whether that means allocating the memory for it without zeroing it out, etc. Resulting in garbage occupying that spot in memory. I would think he actually went a step further and found part of the problem and in fact asked a more specific question. +1
â Edward Severinsen
37 mins ago
1
Hard to say exactly what happens memory-wise. The C++ Standard doesn't specify low-level details like where the memory comes from and how much you get so long as it's at least the amount you asked for. The storage could be a billion gigabyte block in an RNA storage device on Pluto and accessed via quantum entanglement for all the program knows.
â user4581301
37 mins ago
add a comment |Â
6
The title doesn't have anything to do with the text of the question (which seems to be asking why garbage is printed). Please clarify what your question actually is.
â M.M
55 mins ago
@M.M The two very obviously have a direct correlation. The way it's stored is, in part, the cause of the garbage being printed. Whether that means allocating the memory for it without zeroing it out, etc. Resulting in garbage occupying that spot in memory. I would think he actually went a step further and found part of the problem and in fact asked a more specific question. +1
â Edward Severinsen
37 mins ago
1
Hard to say exactly what happens memory-wise. The C++ Standard doesn't specify low-level details like where the memory comes from and how much you get so long as it's at least the amount you asked for. The storage could be a billion gigabyte block in an RNA storage device on Pluto and accessed via quantum entanglement for all the program knows.
â user4581301
37 mins ago
6
6
The title doesn't have anything to do with the text of the question (which seems to be asking why garbage is printed). Please clarify what your question actually is.
â M.M
55 mins ago
The title doesn't have anything to do with the text of the question (which seems to be asking why garbage is printed). Please clarify what your question actually is.
â M.M
55 mins ago
@M.M The two very obviously have a direct correlation. The way it's stored is, in part, the cause of the garbage being printed. Whether that means allocating the memory for it without zeroing it out, etc. Resulting in garbage occupying that spot in memory. I would think he actually went a step further and found part of the problem and in fact asked a more specific question. +1
â Edward Severinsen
37 mins ago
@M.M The two very obviously have a direct correlation. The way it's stored is, in part, the cause of the garbage being printed. Whether that means allocating the memory for it without zeroing it out, etc. Resulting in garbage occupying that spot in memory. I would think he actually went a step further and found part of the problem and in fact asked a more specific question. +1
â Edward Severinsen
37 mins ago
1
1
Hard to say exactly what happens memory-wise. The C++ Standard doesn't specify low-level details like where the memory comes from and how much you get so long as it's at least the amount you asked for. The storage could be a billion gigabyte block in an RNA storage device on Pluto and accessed via quantum entanglement for all the program knows.
â user4581301
37 mins ago
Hard to say exactly what happens memory-wise. The C++ Standard doesn't specify low-level details like where the memory comes from and how much you get so long as it's at least the amount you asked for. The storage could be a billion gigabyte block in an RNA storage device on Pluto and accessed via quantum entanglement for all the program knows.
â user4581301
37 mins ago
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
11
down vote
accepted
For every primitive data types memory locations are contiguous, except
for char. It prints some garbage value on screen.
The "memory locations" are contiguous in the exact same way for both cases. The only difference is how you're displaying your results. When you do:
cout << "Data: " << vChar[i] << " Address:" << &vChar[i] << "n";
you're giving std::operator<<(std::basic_ostream)
a char*
, as your applying &
(address of) on a single char
from the vector-- that makes it treat it as a C-style string, meaning it looks for a terminating null. In your case, this null is right after some garbage indeed. But you're bound to have some garbage after the vector<int>
just as well, only you're not printing it.
If you want to get the same printout as you're getting for the vector<int>
then you could explicitly cast to a void
pointer, so std::cout
would treat it as an address to be printed (overload (7) here), not a string:
cout << "Data: " << vChar[i] << " Address:" << static_cast<void*>(&vChar[i]) << "n";
In which case the output is:
For char vector Size:4 Capacity:4
Data: a Address:0x1c39810
Data: b Address:0x1c39811
Data: c Address:0x1c39812
Data: d Address:0x1c39813
For int vector Size:4 Capacity:4
Data: 1 Address:0x1c39960
Data: 2 Address:0x1c39964
Data: 3 Address:0x1c39968
Data: 4 Address:0x1c3996c
std::cout::operator<<()
doesn't have aconst char*
overload.
â Dev Null
30 mins ago
@DevNull Never said it does...
â SkepticalEmpiricist
29 mins ago
"you're giving cout's operator<< a char*", pointing to en.cppreference.com/w/cpp/io/basic_ostream/operator_ltlt.
â Dev Null
27 mins ago
Well that's pretty accurate so far, where is the specific overload being mentioned?
â SkepticalEmpiricist
26 mins ago
You can't givestd::basic_ostream::operator<<()
achar*
as it doesn't acceptchar*
because it lacks an overload. "Character and character string arguments (e.g., of type char or const char*) are handled by the non-member overloads of operator<<."
â Dev Null
22 mins ago
 |Â
show 1 more comment
up vote
2
down vote
std::vector<T>::operator()
returns T&
, address of which in case of char
will be formatted by overload (2) of operator<<(std::basic_ostream)
as if it was a null terminated C-style string (that is a string that begins at &vChar[i]
and stops at the first found).
To make it work use std::cout << static_cast<const void*>(&vChar[i])
to pick up overload (7) of std::basic_ostream::operator<<()
.
@user4581301 is it better this way?
â Dev Null
51 mins ago
That's groovy. Should explain to just about anyone what's just happened. If they don't know what a C-style string is and can't be bothered to look it up, well, that's kind-of their problem.
â user4581301
47 mins ago
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
11
down vote
accepted
For every primitive data types memory locations are contiguous, except
for char. It prints some garbage value on screen.
The "memory locations" are contiguous in the exact same way for both cases. The only difference is how you're displaying your results. When you do:
cout << "Data: " << vChar[i] << " Address:" << &vChar[i] << "n";
you're giving std::operator<<(std::basic_ostream)
a char*
, as your applying &
(address of) on a single char
from the vector-- that makes it treat it as a C-style string, meaning it looks for a terminating null. In your case, this null is right after some garbage indeed. But you're bound to have some garbage after the vector<int>
just as well, only you're not printing it.
If you want to get the same printout as you're getting for the vector<int>
then you could explicitly cast to a void
pointer, so std::cout
would treat it as an address to be printed (overload (7) here), not a string:
cout << "Data: " << vChar[i] << " Address:" << static_cast<void*>(&vChar[i]) << "n";
In which case the output is:
For char vector Size:4 Capacity:4
Data: a Address:0x1c39810
Data: b Address:0x1c39811
Data: c Address:0x1c39812
Data: d Address:0x1c39813
For int vector Size:4 Capacity:4
Data: 1 Address:0x1c39960
Data: 2 Address:0x1c39964
Data: 3 Address:0x1c39968
Data: 4 Address:0x1c3996c
std::cout::operator<<()
doesn't have aconst char*
overload.
â Dev Null
30 mins ago
@DevNull Never said it does...
â SkepticalEmpiricist
29 mins ago
"you're giving cout's operator<< a char*", pointing to en.cppreference.com/w/cpp/io/basic_ostream/operator_ltlt.
â Dev Null
27 mins ago
Well that's pretty accurate so far, where is the specific overload being mentioned?
â SkepticalEmpiricist
26 mins ago
You can't givestd::basic_ostream::operator<<()
achar*
as it doesn't acceptchar*
because it lacks an overload. "Character and character string arguments (e.g., of type char or const char*) are handled by the non-member overloads of operator<<."
â Dev Null
22 mins ago
 |Â
show 1 more comment
up vote
11
down vote
accepted
For every primitive data types memory locations are contiguous, except
for char. It prints some garbage value on screen.
The "memory locations" are contiguous in the exact same way for both cases. The only difference is how you're displaying your results. When you do:
cout << "Data: " << vChar[i] << " Address:" << &vChar[i] << "n";
you're giving std::operator<<(std::basic_ostream)
a char*
, as your applying &
(address of) on a single char
from the vector-- that makes it treat it as a C-style string, meaning it looks for a terminating null. In your case, this null is right after some garbage indeed. But you're bound to have some garbage after the vector<int>
just as well, only you're not printing it.
If you want to get the same printout as you're getting for the vector<int>
then you could explicitly cast to a void
pointer, so std::cout
would treat it as an address to be printed (overload (7) here), not a string:
cout << "Data: " << vChar[i] << " Address:" << static_cast<void*>(&vChar[i]) << "n";
In which case the output is:
For char vector Size:4 Capacity:4
Data: a Address:0x1c39810
Data: b Address:0x1c39811
Data: c Address:0x1c39812
Data: d Address:0x1c39813
For int vector Size:4 Capacity:4
Data: 1 Address:0x1c39960
Data: 2 Address:0x1c39964
Data: 3 Address:0x1c39968
Data: 4 Address:0x1c3996c
std::cout::operator<<()
doesn't have aconst char*
overload.
â Dev Null
30 mins ago
@DevNull Never said it does...
â SkepticalEmpiricist
29 mins ago
"you're giving cout's operator<< a char*", pointing to en.cppreference.com/w/cpp/io/basic_ostream/operator_ltlt.
â Dev Null
27 mins ago
Well that's pretty accurate so far, where is the specific overload being mentioned?
â SkepticalEmpiricist
26 mins ago
You can't givestd::basic_ostream::operator<<()
achar*
as it doesn't acceptchar*
because it lacks an overload. "Character and character string arguments (e.g., of type char or const char*) are handled by the non-member overloads of operator<<."
â Dev Null
22 mins ago
 |Â
show 1 more comment
up vote
11
down vote
accepted
up vote
11
down vote
accepted
For every primitive data types memory locations are contiguous, except
for char. It prints some garbage value on screen.
The "memory locations" are contiguous in the exact same way for both cases. The only difference is how you're displaying your results. When you do:
cout << "Data: " << vChar[i] << " Address:" << &vChar[i] << "n";
you're giving std::operator<<(std::basic_ostream)
a char*
, as your applying &
(address of) on a single char
from the vector-- that makes it treat it as a C-style string, meaning it looks for a terminating null. In your case, this null is right after some garbage indeed. But you're bound to have some garbage after the vector<int>
just as well, only you're not printing it.
If you want to get the same printout as you're getting for the vector<int>
then you could explicitly cast to a void
pointer, so std::cout
would treat it as an address to be printed (overload (7) here), not a string:
cout << "Data: " << vChar[i] << " Address:" << static_cast<void*>(&vChar[i]) << "n";
In which case the output is:
For char vector Size:4 Capacity:4
Data: a Address:0x1c39810
Data: b Address:0x1c39811
Data: c Address:0x1c39812
Data: d Address:0x1c39813
For int vector Size:4 Capacity:4
Data: 1 Address:0x1c39960
Data: 2 Address:0x1c39964
Data: 3 Address:0x1c39968
Data: 4 Address:0x1c3996c
For every primitive data types memory locations are contiguous, except
for char. It prints some garbage value on screen.
The "memory locations" are contiguous in the exact same way for both cases. The only difference is how you're displaying your results. When you do:
cout << "Data: " << vChar[i] << " Address:" << &vChar[i] << "n";
you're giving std::operator<<(std::basic_ostream)
a char*
, as your applying &
(address of) on a single char
from the vector-- that makes it treat it as a C-style string, meaning it looks for a terminating null. In your case, this null is right after some garbage indeed. But you're bound to have some garbage after the vector<int>
just as well, only you're not printing it.
If you want to get the same printout as you're getting for the vector<int>
then you could explicitly cast to a void
pointer, so std::cout
would treat it as an address to be printed (overload (7) here), not a string:
cout << "Data: " << vChar[i] << " Address:" << static_cast<void*>(&vChar[i]) << "n";
In which case the output is:
For char vector Size:4 Capacity:4
Data: a Address:0x1c39810
Data: b Address:0x1c39811
Data: c Address:0x1c39812
Data: d Address:0x1c39813
For int vector Size:4 Capacity:4
Data: 1 Address:0x1c39960
Data: 2 Address:0x1c39964
Data: 3 Address:0x1c39968
Data: 4 Address:0x1c3996c
edited 21 mins ago
answered 51 mins ago
SkepticalEmpiricist
3,792823
3,792823
std::cout::operator<<()
doesn't have aconst char*
overload.
â Dev Null
30 mins ago
@DevNull Never said it does...
â SkepticalEmpiricist
29 mins ago
"you're giving cout's operator<< a char*", pointing to en.cppreference.com/w/cpp/io/basic_ostream/operator_ltlt.
â Dev Null
27 mins ago
Well that's pretty accurate so far, where is the specific overload being mentioned?
â SkepticalEmpiricist
26 mins ago
You can't givestd::basic_ostream::operator<<()
achar*
as it doesn't acceptchar*
because it lacks an overload. "Character and character string arguments (e.g., of type char or const char*) are handled by the non-member overloads of operator<<."
â Dev Null
22 mins ago
 |Â
show 1 more comment
std::cout::operator<<()
doesn't have aconst char*
overload.
â Dev Null
30 mins ago
@DevNull Never said it does...
â SkepticalEmpiricist
29 mins ago
"you're giving cout's operator<< a char*", pointing to en.cppreference.com/w/cpp/io/basic_ostream/operator_ltlt.
â Dev Null
27 mins ago
Well that's pretty accurate so far, where is the specific overload being mentioned?
â SkepticalEmpiricist
26 mins ago
You can't givestd::basic_ostream::operator<<()
achar*
as it doesn't acceptchar*
because it lacks an overload. "Character and character string arguments (e.g., of type char or const char*) are handled by the non-member overloads of operator<<."
â Dev Null
22 mins ago
std::cout::operator<<()
doesn't have a const char*
overload.â Dev Null
30 mins ago
std::cout::operator<<()
doesn't have a const char*
overload.â Dev Null
30 mins ago
@DevNull Never said it does...
â SkepticalEmpiricist
29 mins ago
@DevNull Never said it does...
â SkepticalEmpiricist
29 mins ago
"you're giving cout's operator<< a char*", pointing to en.cppreference.com/w/cpp/io/basic_ostream/operator_ltlt.
â Dev Null
27 mins ago
"you're giving cout's operator<< a char*", pointing to en.cppreference.com/w/cpp/io/basic_ostream/operator_ltlt.
â Dev Null
27 mins ago
Well that's pretty accurate so far, where is the specific overload being mentioned?
â SkepticalEmpiricist
26 mins ago
Well that's pretty accurate so far, where is the specific overload being mentioned?
â SkepticalEmpiricist
26 mins ago
You can't give
std::basic_ostream::operator<<()
a char*
as it doesn't accept char*
because it lacks an overload. "Character and character string arguments (e.g., of type char or const char*) are handled by the non-member overloads of operator<<."â Dev Null
22 mins ago
You can't give
std::basic_ostream::operator<<()
a char*
as it doesn't accept char*
because it lacks an overload. "Character and character string arguments (e.g., of type char or const char*) are handled by the non-member overloads of operator<<."â Dev Null
22 mins ago
 |Â
show 1 more comment
up vote
2
down vote
std::vector<T>::operator()
returns T&
, address of which in case of char
will be formatted by overload (2) of operator<<(std::basic_ostream)
as if it was a null terminated C-style string (that is a string that begins at &vChar[i]
and stops at the first found).
To make it work use std::cout << static_cast<const void*>(&vChar[i])
to pick up overload (7) of std::basic_ostream::operator<<()
.
@user4581301 is it better this way?
â Dev Null
51 mins ago
That's groovy. Should explain to just about anyone what's just happened. If they don't know what a C-style string is and can't be bothered to look it up, well, that's kind-of their problem.
â user4581301
47 mins ago
add a comment |Â
up vote
2
down vote
std::vector<T>::operator()
returns T&
, address of which in case of char
will be formatted by overload (2) of operator<<(std::basic_ostream)
as if it was a null terminated C-style string (that is a string that begins at &vChar[i]
and stops at the first found).
To make it work use std::cout << static_cast<const void*>(&vChar[i])
to pick up overload (7) of std::basic_ostream::operator<<()
.
@user4581301 is it better this way?
â Dev Null
51 mins ago
That's groovy. Should explain to just about anyone what's just happened. If they don't know what a C-style string is and can't be bothered to look it up, well, that's kind-of their problem.
â user4581301
47 mins ago
add a comment |Â
up vote
2
down vote
up vote
2
down vote
std::vector<T>::operator()
returns T&
, address of which in case of char
will be formatted by overload (2) of operator<<(std::basic_ostream)
as if it was a null terminated C-style string (that is a string that begins at &vChar[i]
and stops at the first found).
To make it work use std::cout << static_cast<const void*>(&vChar[i])
to pick up overload (7) of std::basic_ostream::operator<<()
.
std::vector<T>::operator()
returns T&
, address of which in case of char
will be formatted by overload (2) of operator<<(std::basic_ostream)
as if it was a null terminated C-style string (that is a string that begins at &vChar[i]
and stops at the first found).
To make it work use std::cout << static_cast<const void*>(&vChar[i])
to pick up overload (7) of std::basic_ostream::operator<<()
.
edited 29 mins ago
answered 55 mins ago
Dev Null
1,518721
1,518721
@user4581301 is it better this way?
â Dev Null
51 mins ago
That's groovy. Should explain to just about anyone what's just happened. If they don't know what a C-style string is and can't be bothered to look it up, well, that's kind-of their problem.
â user4581301
47 mins ago
add a comment |Â
@user4581301 is it better this way?
â Dev Null
51 mins ago
That's groovy. Should explain to just about anyone what's just happened. If they don't know what a C-style string is and can't be bothered to look it up, well, that's kind-of their problem.
â user4581301
47 mins ago
@user4581301 is it better this way?
â Dev Null
51 mins ago
@user4581301 is it better this way?
â Dev Null
51 mins ago
That's groovy. Should explain to just about anyone what's just happened. If they don't know what a C-style string is and can't be bothered to look it up, well, that's kind-of their problem.
â user4581301
47 mins ago
That's groovy. Should explain to just about anyone what's just happened. If they don't know what a C-style string is and can't be bothered to look it up, well, that's kind-of their problem.
â user4581301
47 mins ago
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52398502%2fhow-memory-is-allocated-for-vectorchar-in-c%23new-answer', 'question_page');
);
Post as a guest
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
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
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
6
The title doesn't have anything to do with the text of the question (which seems to be asking why garbage is printed). Please clarify what your question actually is.
â M.M
55 mins ago
@M.M The two very obviously have a direct correlation. The way it's stored is, in part, the cause of the garbage being printed. Whether that means allocating the memory for it without zeroing it out, etc. Resulting in garbage occupying that spot in memory. I would think he actually went a step further and found part of the problem and in fact asked a more specific question. +1
â Edward Severinsen
37 mins ago
1
Hard to say exactly what happens memory-wise. The C++ Standard doesn't specify low-level details like where the memory comes from and how much you get so long as it's at least the amount you asked for. The storage could be a billion gigabyte block in an RNA storage device on Pluto and accessed via quantum entanglement for all the program knows.
â user4581301
37 mins ago