I am using C++ Builder XE4 on Windows7 Professional (32bit).
I have a question on TStringList;
What I would like to do is to know whether the TStringList variable was newed or not, to prevent Add() method to non-newed TStringList;
I thought checking NULL might work, but it didn't.
TStringList *list;
// list = new TStringList(); // someone commented out by mistakes
if (list == NULL) {
ShowMessage(L"NULL");
} else {
ShowMessage(L"not empty");
}
return;
The above code shows "not empty" dialog.
What is a standard way to check the newed or non-newed TStringList?
It is very commonplace is c/c++ to initialize pointers to NULL, and have them be null whenever they aren't pointing at something valid.
As such, your if statement is a very commonplace and appropriate piece of code.
However, this does require initializing your variable.
Unless declared at global/file scope, the line: TStringList *list; does not initialize the variable to any specific value - this leaves it pointing at random crap.
You need to change it to TStringList *list = NULL; (or similar, some people dont like "NULL")