I seem to have forgotten a line:While this isn't good coding practice, it's a great example of what I'm saying...
#include <iostream> #include <string> #include <cstring> int main(void) { char* i = new char(10); memset(i, 0, 10); strcpy(i, "hello!"); std::string s=i; delete [] i; memset(i, 0, 10); std::cout << s << std::endl; return 0; }In short, we create an array of 10 bytes, fill it with zeros (so the string is NULL terminated), assign that to a std::string, delete the array and set the 10 bytes at that address back to zero. If the std::string held a reference or just the address of that pointer, we wouldn't see the "hello" being printed, because the memory where the array was was just filled with zeros. The fact that it is, tells us that std::string's = operator does a strcpy, or a similar copy operation.
----- Original Message ----- From: "Littlefield, Tyler" <tyler@xxxxxxxxxxxxx>
To: <programmingblind@xxxxxxxxxxxxx> Sent: Sunday, August 22, 2010 9:30 PM Subject: Re: generating sha256 hashes with openssl?
#include <iostream> #include <string> #include <cstring> int main(void) { char* i = new char(10); memset(i, 0, 10); strcpy(i, "hello!"); std::string s=i; delete [] i; std::cout << s << std::endl; return 0; } tyler@tangerine:~$ g++ poc.cpp tyler@tangerine:~$ ./a.out hello! tyler@tangerine:~$ so... It -is- copying, as I said earlier.the std::string manages it's own array, so it can expand and etc. A smart pointer won't do any good as it's not used after the strcpy, thus deleting it has no affect here.Thanks, Ty __________View the list's information and change your settings at //www.freelists.org/list/programmingblind
__________View the list's information and change your settings at //www.freelists.org/list/programmingblind