Answer the question
In order to leave comments, you need to log in
What is the difference between delete and nullptr?
Why if you create a pointer to char in c++, then copy this pointer, then if you delete it, the value of the copied pointer will be undefined, and if you make the initial pointer nullptr, then the copied pointer will have its original value?
Here is an example:
char* g = new char[10];
strcpy_s(g, 6, "hi");
char* gg = g;
g = nullptr; // Здесь всё нормально, но если сделать delete[] g, то будет неопределенное поведение
std::cout << gg;
Answer the question
In order to leave comments, you need to log in
g = nullptr does not delete memory, it simply writes a value to the variable g with the meaning - "pointer to nowhere". The memory remains intact and can be accessed, which you do by storing the pointer in another variable.
delete[] releases the memory, which means that all subsequent calls to it, either with g or with gg, will cause UB
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question