Quick question regarding memory management in C++
If I do the following operation:
pointer = new char [strlen(someinput_input)+1];
And then perform it again, with perhaps a different result being returned from strlen(someinput_input).
Does this result in memory being left allocated from the previous “new” statement? As in, is each new statement receiving another block of HEAP memory from the OS, or is it simply reallocating?
Assuming I do a final delete pointer[]; will that deallocate any and all memory that I ever allocated via new to that pointer?
Every call to
newmust be matched with a corresponding call todelete.As an aside, you should probably consider using
std::stringor evenstd::vector<char>(depending on the exact situation), rather than trying to allocate char arrays yourself. Then you don’t ever need to worry.