I’m beginning and there is something that I don’t understand with pointers.
I have the following code returning an error I don’t know why:
std::string key = "myKey";
const unsigned char* aesKey = reinterpret_cast<const unsigned char *> (key.c_str());
// Executing some stuffs
delete aesKey;
the first time the code executes everything works fine, but the second time i get an error which I don’t get if I don’t have the delete line (Instead I have a memory leak, quite worst).
Someone could explain me why this is not working?
The sample below is working fine in the same context (same method, …)
unsigned char* test = new char;
//doing some stuff
delete test;
First: you don’t own that C string, so it’s not yours to
delete[]. Thestd::stringstill owns it and willdelete[]it later.Second: arrays are deleted with
delete[], notdelete. So you’re using the wrong operator anyway.