Access Violation Reading Location 0x000000xx - c struct property - Visual Studio

-3

As the title tells the story. Pasted below is the piece of code and all the dependencies. The error is strange to me because I am able to access the same variables in Watch window, and I can find no valid reason why I would get this error on runtime. Most of the questions with the same title I was able to find were related to null addresses being accessed, which is not the case here. 0X00000069 is the location which can not be accessed. I have also attached Watch window screenshot below.

typedef struct
{
    void  * variable;     // variable address
    char * signature;
    char name[30];
    char type[50];
}   GlobalVariable;

GlobalVariable VariableTable[] = 
{
    { &a, "int a = 7;", "a", "int"},
    { &ch, "char ch = 'a';", "ch", "char"},
    { NULL, NULL }
};

void PrintGlobalVariables()
{
    GlobalVariable * variable = VariableTable;
    int count = 0;
    while(variable->variable)
    {
        count++;
        variable++;
    }
    if(!count)
    {
        OutputDebugString("No global variables passed.\n");
        return;
    }
    int i = 0 ;
    variable = VariableTable;

    char       temp[MAX_PATH];
    strcpy(temp, "                                                             ");
    OutputDebugString("Global Variables = "); 
    while(variable->variable)
    {
        if(strcmp(trimwhitespace(variable->type),"int") == 0)
        {


            ////////THIS IS THE STATEMENT BELOW WHERE I AM GETTING ERROR
            wsprintf(temp, "%s=%ld",*(char *)variable->signature, *(long *)variable->variable);


        } else
        if(strcmp(trimwhitespace(variable->type),"char") == 0)
        {
            wsprintf(temp, "%s=%c", *(char *)variable->name, *(char *)variable->variable);
        }
        if((variable+1)->variable != NULL)
        {
            wsprintf(temp + strlen(temp), ",", "");
        }
        OutputDebugString(temp);
        variable++;

    }
    OutputDebugString("\n");
}
char *trimwhitespace(char *str)
{
    char *end;

    // Trim leading space
    while(isspace(*str)) str++;

    if(*str == 0)  // All spaces?
        return str;

    // Trim trailing space
    end = str + strlen(str) - 1;
    while(end > str && isspace(*end)) end--;

    // Write new null terminator
    *(end+1) = '\0';
    return str;
}

Watch Values

c
visual-studio-2008
struct
access-violation
asked on Stack Overflow Jun 20, 2016 by Taha Rehman Siddiqui • edited Jun 21, 2016 by Taha Rehman Siddiqui

1 Answer

3
wsprintf(temp, "%s=%ld",*(char *)variable->signature, *(long *)variable->variable);

The signature field is a char *. Dereferencing it will give a char. But the %s format specifier requires a char *. The fix is to change that line by removing the dereference (and the cast is unnecessary too).

wsprintf(temp, "%s=%ld", variable->signature, *(long *)variable->variable);
answered on Stack Overflow Jun 21, 2016 by kaylum

User contributions licensed under CC BY-SA 3.0