Printing addresses of vector's elements shows garbage [duplicate]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
21
down vote

favorite













This question already has an answer here:



  • Why does streaming a char pointer to cout not print an address?

    4 answers



Consider:



#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 type memory locations are contiguous, except for char. It prints some garbage value on the screen.



I tried adding v.reserve(4), but the output was the same.










share|improve this question















marked as duplicate by Sneftel, Killzone Kid, Konrad Rudolph c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Sep 19 at 13:06


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • @KonradRudolph, while the solution in code in the answer to this clearly applies almost verbatim to this one, I kinda feel like this question is explicitly about garbage in memory of a std::vector ("I tried adding v.reserve(4)"), while the older question is directly about printing.
    – SkepticalEmpiricist
    Sep 19 at 17:06











  • @KonradRudolph the original title, before edited not by the OP, was 'How memory is allocated for vector<char> in C++?'. My point is that a certain thought process has lead the OP to ask it this way (which he could be sharing with other people as well), and changing it and coupling it with a question regarding something else (from the perspective of the person asking) will just prevent other people going through the same thought process from finding the answer.
    – SkepticalEmpiricist
    Sep 19 at 18:14














up vote
21
down vote

favorite













This question already has an answer here:



  • Why does streaming a char pointer to cout not print an address?

    4 answers



Consider:



#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 type memory locations are contiguous, except for char. It prints some garbage value on the screen.



I tried adding v.reserve(4), but the output was the same.










share|improve this question















marked as duplicate by Sneftel, Killzone Kid, Konrad Rudolph c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Sep 19 at 13:06


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • @KonradRudolph, while the solution in code in the answer to this clearly applies almost verbatim to this one, I kinda feel like this question is explicitly about garbage in memory of a std::vector ("I tried adding v.reserve(4)"), while the older question is directly about printing.
    – SkepticalEmpiricist
    Sep 19 at 17:06











  • @KonradRudolph the original title, before edited not by the OP, was 'How memory is allocated for vector<char> in C++?'. My point is that a certain thought process has lead the OP to ask it this way (which he could be sharing with other people as well), and changing it and coupling it with a question regarding something else (from the perspective of the person asking) will just prevent other people going through the same thought process from finding the answer.
    – SkepticalEmpiricist
    Sep 19 at 18:14












up vote
21
down vote

favorite









up vote
21
down vote

favorite












This question already has an answer here:



  • Why does streaming a char pointer to cout not print an address?

    4 answers



Consider:



#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 type memory locations are contiguous, except for char. It prints some garbage value on the screen.



I tried adding v.reserve(4), but the output was the same.










share|improve this question
















This question already has an answer here:



  • Why does streaming a char pointer to cout not print an address?

    4 answers



Consider:



#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 type memory locations are contiguous, except for char. It prints some garbage value on the screen.



I tried adding v.reserve(4), but the output was the same.





This question already has an answer here:



  • Why does streaming a char pointer to cout not print an address?

    4 answers







c++ vector c++17






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 20 at 6:51









Cerbrus

48.1k888113




48.1k888113










asked Sep 19 at 5:10









Palash Tichkule

1258




1258




marked as duplicate by Sneftel, Killzone Kid, Konrad Rudolph c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Sep 19 at 13:06


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Sneftel, Killzone Kid, Konrad Rudolph c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Sep 19 at 13:06


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.













  • @KonradRudolph, while the solution in code in the answer to this clearly applies almost verbatim to this one, I kinda feel like this question is explicitly about garbage in memory of a std::vector ("I tried adding v.reserve(4)"), while the older question is directly about printing.
    – SkepticalEmpiricist
    Sep 19 at 17:06











  • @KonradRudolph the original title, before edited not by the OP, was 'How memory is allocated for vector<char> in C++?'. My point is that a certain thought process has lead the OP to ask it this way (which he could be sharing with other people as well), and changing it and coupling it with a question regarding something else (from the perspective of the person asking) will just prevent other people going through the same thought process from finding the answer.
    – SkepticalEmpiricist
    Sep 19 at 18:14
















  • @KonradRudolph, while the solution in code in the answer to this clearly applies almost verbatim to this one, I kinda feel like this question is explicitly about garbage in memory of a std::vector ("I tried adding v.reserve(4)"), while the older question is directly about printing.
    – SkepticalEmpiricist
    Sep 19 at 17:06











  • @KonradRudolph the original title, before edited not by the OP, was 'How memory is allocated for vector<char> in C++?'. My point is that a certain thought process has lead the OP to ask it this way (which he could be sharing with other people as well), and changing it and coupling it with a question regarding something else (from the perspective of the person asking) will just prevent other people going through the same thought process from finding the answer.
    – SkepticalEmpiricist
    Sep 19 at 18:14















@KonradRudolph, while the solution in code in the answer to this clearly applies almost verbatim to this one, I kinda feel like this question is explicitly about garbage in memory of a std::vector ("I tried adding v.reserve(4)"), while the older question is directly about printing.
– SkepticalEmpiricist
Sep 19 at 17:06





@KonradRudolph, while the solution in code in the answer to this clearly applies almost verbatim to this one, I kinda feel like this question is explicitly about garbage in memory of a std::vector ("I tried adding v.reserve(4)"), while the older question is directly about printing.
– SkepticalEmpiricist
Sep 19 at 17:06













@KonradRudolph the original title, before edited not by the OP, was 'How memory is allocated for vector<char> in C++?'. My point is that a certain thought process has lead the OP to ask it this way (which he could be sharing with other people as well), and changing it and coupling it with a question regarding something else (from the perspective of the person asking) will just prevent other people going through the same thought process from finding the answer.
– SkepticalEmpiricist
Sep 19 at 18:14




@KonradRudolph the original title, before edited not by the OP, was 'How memory is allocated for vector<char> in C++?'. My point is that a certain thought process has lead the OP to ask it this way (which he could be sharing with other people as well), and changing it and coupling it with a question regarding something else (from the perspective of the person asking) will just prevent other people going through the same thought process from finding the answer.
– SkepticalEmpiricist
Sep 19 at 18:14












2 Answers
2






active

oldest

votes

















up vote
38
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 in 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 you're applying & (address-of) on a single char1 from the vector, which 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.2 But you're bound to have some garbage after the vector<int> just as well, only you're not printing it.3



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 will 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



1char& to be precise, as std::vector<T>::operator returns a T&.



2 Note that looking for this terminating null that wasn't placed there by you constitutes undefined behavior, as it potentially makes you access memory that isn't intended to be accessed for this purpose.



3 Yo can try and see so for yourself if you perform the reverse casting to make std::cout treat the vector<int> elements as C-style strings:



cout << "Data: " << vInt[i] << " Address:" << reinterpret_cast<char*>(&vInt[i]) << "n";


Again, just remember this means undefined behavior as the printing code will look in memory for the terminating null while you definitely didn't have it there for it to find.






share|improve this answer





























    up vote
    8
    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





























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      38
      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 in 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 you're applying & (address-of) on a single char1 from the vector, which 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.2 But you're bound to have some garbage after the vector<int> just as well, only you're not printing it.3



      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 will 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



      1char& to be precise, as std::vector<T>::operator returns a T&.



      2 Note that looking for this terminating null that wasn't placed there by you constitutes undefined behavior, as it potentially makes you access memory that isn't intended to be accessed for this purpose.



      3 Yo can try and see so for yourself if you perform the reverse casting to make std::cout treat the vector<int> elements as C-style strings:



      cout << "Data: " << vInt[i] << " Address:" << reinterpret_cast<char*>(&vInt[i]) << "n";


      Again, just remember this means undefined behavior as the printing code will look in memory for the terminating null while you definitely didn't have it there for it to find.






      share|improve this answer


























        up vote
        38
        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 in 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 you're applying & (address-of) on a single char1 from the vector, which 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.2 But you're bound to have some garbage after the vector<int> just as well, only you're not printing it.3



        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 will 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



        1char& to be precise, as std::vector<T>::operator returns a T&.



        2 Note that looking for this terminating null that wasn't placed there by you constitutes undefined behavior, as it potentially makes you access memory that isn't intended to be accessed for this purpose.



        3 Yo can try and see so for yourself if you perform the reverse casting to make std::cout treat the vector<int> elements as C-style strings:



        cout << "Data: " << vInt[i] << " Address:" << reinterpret_cast<char*>(&vInt[i]) << "n";


        Again, just remember this means undefined behavior as the printing code will look in memory for the terminating null while you definitely didn't have it there for it to find.






        share|improve this answer
























          up vote
          38
          down vote



          accepted







          up vote
          38
          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 in 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 you're applying & (address-of) on a single char1 from the vector, which 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.2 But you're bound to have some garbage after the vector<int> just as well, only you're not printing it.3



          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 will 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



          1char& to be precise, as std::vector<T>::operator returns a T&.



          2 Note that looking for this terminating null that wasn't placed there by you constitutes undefined behavior, as it potentially makes you access memory that isn't intended to be accessed for this purpose.



          3 Yo can try and see so for yourself if you perform the reverse casting to make std::cout treat the vector<int> elements as C-style strings:



          cout << "Data: " << vInt[i] << " Address:" << reinterpret_cast<char*>(&vInt[i]) << "n";


          Again, just remember this means undefined behavior as the printing code will look in memory for the terminating null while you definitely didn't have it there for it to find.






          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 in 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 you're applying & (address-of) on a single char1 from the vector, which 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.2 But you're bound to have some garbage after the vector<int> just as well, only you're not printing it.3



          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 will 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



          1char& to be precise, as std::vector<T>::operator returns a T&.



          2 Note that looking for this terminating null that wasn't placed there by you constitutes undefined behavior, as it potentially makes you access memory that isn't intended to be accessed for this purpose.



          3 Yo can try and see so for yourself if you perform the reverse casting to make std::cout treat the vector<int> elements as C-style strings:



          cout << "Data: " << vInt[i] << " Address:" << reinterpret_cast<char*>(&vInt[i]) << "n";


          Again, just remember this means undefined behavior as the printing code will look in memory for the terminating null while you definitely didn't have it there for it to find.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Sep 20 at 13:59

























          answered Sep 19 at 5:17









          SkepticalEmpiricist

          4,668924




          4,668924






















              up vote
              8
              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


























                up vote
                8
                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
























                  up vote
                  8
                  down vote










                  up vote
                  8
                  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 Sep 19 at 5:38

























                  answered Sep 19 at 5:12









                  Dev Null

                  1,657721




                  1,657721












                      Popular posts from this blog

                      Peggy Mitchell

                      The Forum (Inglewood, California)

                      Palaiologos