need some info about new/delete

closed account (28poGNh0)
Hiii everyone

/// Is this fragment of code fine

int main()
{
char *ptr;

ptr = new char[5];

ptr = new char[10];

delete []ptr;

return 0;
}

/// and if It is should I delete ptr twice
Is this fragment of code fine
No, it leaks memory.

You should delete the memory the first time before you reallocate it (i.e. before ptr = new char[10];)
Last edited on
closed account (28poGNh0)
Thanks @coder777
Topic archived. No new replies allowed.