How memory is allocated for vector in C++?

The name of the pictureThe name of the pictureThe name of the pictureClash 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.










share|improve this question



















  • 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















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.










share|improve this question



















  • 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













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.










share|improve this question















#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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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













  • 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













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





share|improve this answer






















  • std::cout::operator<<() doesn't have a const 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 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

















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<<().






share|improve this answer






















  • @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










Your Answer





StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















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






























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





share|improve this answer






















  • std::cout::operator<<() doesn't have a const 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 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














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





share|improve this answer






















  • std::cout::operator<<() doesn't have a const 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 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












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





share|improve this answer















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






share|improve this answer














share|improve this answer



share|improve this answer








edited 21 mins ago

























answered 51 mins ago









SkepticalEmpiricist

3,792823




3,792823











  • std::cout::operator<<() doesn't have a const 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 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
















  • std::cout::operator<<() doesn't have a const 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 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















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












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<<().






share|improve this answer






















  • @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














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<<().






share|improve this answer






















  • @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












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<<().






share|improve this answer














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<<().







share|improve this answer














share|improve this answer



share|improve this answer








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
















  • @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

















 

draft saved


draft discarded















































 


draft saved


draft discarded














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













































































Comments

Popular posts from this blog

Long meetings (6-7 hours a day): Being “babysat” by supervisor

Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

Confectionery