I wrote the following piece of code and got the error mentioned above
Can someone tell me where i am going wrong
Thanks.
#include "iostream"
#include "sstream"
#include <string.h>
int main()
{
std::string temp1 = "454552354772";
char arr[16];
memcpy(arr , &temp1 , temp1.size());
std::string temp2;
memcpy(&temp2 , arr , temp1.size());
std::cout<<temp2;
}
the error i got was
*** Error in `./a.out': double free or corruption (fasttop): 0x00000000016c5010 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x80a46)[0x7fa91d359a46]
/usr/lib/x86_64-linux-gnu/libstdc++.so.6(_ZNSsD1Ev+0x20)[0x7fa91dc78290]
./a.out[0x400bc9]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf5)[0x7fa91d2faea5]
./a.out[0x400a09]
std::string
isn't possible. Not without invoking all kinds of undefined behavior. – jogojapanmemcpy
statement. You copy from an array straight into astd::string
object. Don't do that. – jogojapan