-
delete[] frees whatever memory was allocated with new[], whatever was allocated.
If you used new to allocate, use delete to free.
If you used new[] to allocate, use delete[] to free.
Even better, don't use new/delete at all. Use containers and make_unique/make_shared.
-
freddie, any exception can be replaced with a bool wasThereAnError member of a class and then checked later. I've done this refactoring myself in some cases when C++ exceptions are not supported by the runtime environment. However, keep in mind that it adds an extra field to every single String object in your program. This additional space can be prohibitive (or at least not well utilized) especially when you have lots of small strings... and we know that runtimes already go to a lot of effort to optimize for short strings c++ - What are the mechanics of short string optimization in libc++? - Stack Overflow.
It's true that sometimes C++ exceptions go against the coding style of a codebase or may require additional runtime/buildtime cost. For instance, when compiling shared libraries that may want to throw exceptions, g++ may need the flag -shared-libgcc because the static libgcc.a doesn't support this. But even when you forget to link with this flag, or you're messing with the linker and you forget to have a .eh_frame exception header section in your executable (I may have done this recently...), an exception will simply cause a SIGABRT. Which will kill your program, but you will see a useful stack trace in a debugger. I think that's preferable to wasting space in every String object, getting NULL pointers, and potentially only getting a crash later when you try to use a large String object -- assuming you don't expect to commonly check for string allocation errors. Keep in mind also that if you configure new to throw exceptions you'll get the same behaviour as well.
Well actually, I think it's best to stick with the standard std::string unless you really have a need to do otherwise; clearly you like to use Windows allocation functions and so our needs are different. Just some food for thought.