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[]. The std::string still owns it and will delete[] it later.
Second: arrays are deleted with delete[], not delete. So you're using the wrong operator anyway.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With