访问冲突读取位置 0x000000xx - c struct 属性 - Visual Studio
Access Violation Reading Location 0x000000xx - c struct property - Visual Studio
正如标题所说的那样。下面粘贴的是一段代码和所有依赖项。这个错误对我来说很奇怪,因为我能够在 Watch window 中访问相同的变量,而且我找不到在运行时出现此错误的正当理由。我能够找到的大多数具有相同标题的问题都与正在访问的空地址有关,但这里不是这种情况。 0X00000069 是无法访问的位置。我还在下面附上了 Watch window 截图。
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) = '[=12=]';
return str;
}
wsprintf(temp, "%s=%ld",*(char *)variable->signature, *(long *)variable->variable);
signature
字段是 char *
。取消引用它会得到 char
。但是 %s
格式说明符需要 char *
。解决方法是通过删除取消引用来更改该行(并且也不需要强制转换)。
wsprintf(temp, "%s=%ld", variable->signature, *(long *)variable->variable);
正如标题所说的那样。下面粘贴的是一段代码和所有依赖项。这个错误对我来说很奇怪,因为我能够在 Watch window 中访问相同的变量,而且我找不到在运行时出现此错误的正当理由。我能够找到的大多数具有相同标题的问题都与正在访问的空地址有关,但这里不是这种情况。 0X00000069 是无法访问的位置。我还在下面附上了 Watch window 截图。
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) = '[=12=]';
return str;
}
wsprintf(temp, "%s=%ld",*(char *)variable->signature, *(long *)variable->variable);
signature
字段是 char *
。取消引用它会得到 char
。但是 %s
格式说明符需要 char *
。解决方法是通过删除取消引用来更改该行(并且也不需要强制转换)。
wsprintf(temp, "%s=%ld", variable->signature, *(long *)variable->variable);