Hello Sina,This thread turned into whether or not std::string does a strcpy, and how a smart pointer or something similar would solve my problem. The code just showed that std::string -does- copy. Much easier to write a quick POC than to dig through the internals of the stl for something I already know happens.
Thanks, Ty----- Original Message ----- From: "Sina Bahram" <sbahram@xxxxxxxxx>
To: <programmingblind@xxxxxxxxxxxxx> Sent: Sunday, August 22, 2010 9:39 PM Subject: RE: generating sha256 hashes with openssl?
Why are you not using strncpy? Take care, Sina -----Original Message-----From: programmingblind-bounce@xxxxxxxxxxxxx [mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Littlefield, TylerSent: Sunday, August 22, 2010 11:36 PM To: programmingblind@xxxxxxxxxxxxx Subject: Re: generating sha256 hashes with openssl? 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 thatpointer, 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 smartpointer won't do any good as it's not used after the strcpy, thus deletingit 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 __________ 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