mercredi 22 juin 2016

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

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.

EDIT

Project can be downloaded from Google Drive Link and should be opened with Visual Studio 2008 as it uses a compiler option known as "Gh".

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) = '�';
    return str;
}

Watch Values

Aucun commentaire:

Enregistrer un commentaire