Hi,

How do I check if a member of a struct is null?
I've tried:

if(ErrorInfo->TestName != NULL)

The above method returns true even when ErrorInfo->TestName contains nothing (debug shows it as 0xccccccc, "").

(!(strcspn(ErrorInfo->TestName, "") == 0))

The above method throws an access violation.
I've tried other ways too (strcmp etc but they are all throwing access violation).
ErrorInfo->TestName is of type char *.

The structure:

typedef struct 
{
    int ErrorCode;          // the error code which will correspond to an error message
    char *TestName;         // the name of the current test
    char *Command;          // the command that caused the error
    char ErrorText[0x50];   // an error message
    char *ExpectedData;     // the expected data
    char *ReturnedData;     // the returned data
} ErrorInformation;

Note: I can only use ANSI C library functions.
Any help appreciated.

Dani AI

Generated

What you are seeing is not a null string; it is an uninitialized pointer. In MSVC debug builds, uninitialized stack memory is filled with 0xCC, so a char * may appear as 0xCCCCCCCC and any C string function that dereferences it will crash. There is no way in C to test whether a non-NULL pointer is valid; you must ensure it is initialized and points to a NUL-terminated buffer before using it. See Microsoft’s fill patterns in CRT debug heap details.

A robust pattern is to zero-initialize the struct and always guard C library calls:

ErrorInformation info = {0};            /* all pointers NULL, arrays zeroed */
/* or: ErrorInformation *p = calloc(1, sizeof *p); */

if (info.TestName && info.TestName[0] != '\0') {
    /* non-empty string */
}

/* Safe length computation */
size_t name_len = info.TestName ? strlen(info.TestName) : 0;

If you know a maximum length, prefer fixed-size arrays in the struct to avoid dangling pointers:

typedef struct {
    int ErrorCode;
    char TestName[64];
    char Command[64];
    char ErrorText[0x50];
    char ExpectedData[128];
    char ReturnedData[128];
} ErrorInformation;

Then an "empty" value is simply TestName[0] == '\0'. If you keep char * members, allocate and free them consistently, and treat (ptr && *ptr) as the check for a non-empty string.

Recommended Answers

All 2 Replies

>> How do I check if a member of a struct is null?

Just like you are doing, i.e.

// Is it a NULL pointer?
if(ErrorInfo->TestName != NULL)
{
  // No it isn't ...
}

>> debug shows it as 0xccccccc, "")

This value 0xccccccc tells you that you are dealing with an automatic ErrorInfo variable, whose TestName member has NOT been initialized by you - it's an uninitialized variable. The value you see, is set by the Microsoft's debugging runtime library - a handy feature for spotting uninitialized variables.

So, what you need to do, is to initialize the variables. Either write a constructor which initializes the pointers to NULL, or if you are actually writing C instead of C++ ..

struct ErrorInfo errInfo;
errInfo.TestName = NULL;
// Initialize the rest of the members ..

P.S. An overview regarding various fill patterns and such Win32 Debug CRT Heap Internals.

Thank you, this works :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.