I have recently made changes in some code that makes a char name field dynamic.
So it was originally like
struct boo
{
char name[100];
...
}
and i have changed it to
struct boo
{
char *name;
...
}
so this make name dynamically allocate the amount of memory actually needed to store the names.
Anyway.. the result of this change will require me to add if(boo->name) null pointer check in about 1000 places in the code.
So just wondering is there any smart or efficient way (reduce programmer development time) of doing this null pointer check.
It will be far easier to ensure that the buffer is allocated when the structure is created rather than checking it wherever the structure is used. Don’t ever let it be NULL in the first place!
If you need a pointer value to place in the structure before you have the relevant data, you can keep a global empty string to use specifically for this task. Compare to this pointer before trying to
freethe memory.If this is C++ and not C, seriously consider using a
std::stringinstead of a pointer.