我的程序包括读取首字母缩写词及其相关定义的文件,然后允许用户根据他们输入的首字母缩写词找到定义。
例如用户输入FAT,程序将打印出“文件分配表”,并将其他定义映射到FAT。
打算存储定义的方式是使用链接列表。 首字母缩写为结构数组。 我遇到的问题是当我尝试为结构对象分配空间时。
没有编译器警告/错误,但是当我逐步执行代码时,总是在这一点上:
AcronymDB * entry_list = malloc( sizeof( AcronymDB ) );
在“变量”视图窗口中显示以下错误:
_entry_list AcronymDB * Error: Multiple errors reported.
Failed to execute MI command: -var-update 1 var3 Error message from debugger back end: Cannot access memory at address 0xbaadf00d
Failed to execute MI command: -var-update 1 var3 Error message from debugger back end: Cannot access memory at address 0xbaadf00d_
无论我运行程序或打开/关闭程序多少次,该内存地址0xbaadf00d
(第三行)似乎都是相同的。我不明白为什么会这样。
我正在使用Portable Eclipse
编辑: 行是先前计算的条目总数的全局变量, r 是找到首字母缩写词的当前行内。
这些是结构,问题似乎出在最后一个。
//associated definitions in a linked list
typedef struct defnList
{
char * defn;
struct defnList * next;
} Definition;
//acronym entry
typedef struct
{
char * acronym;
Definition * f_defn; //a pointer to the first definition
} Acronym;
//database of acronyms
typedef struct
{
unsigned int entries;
Acronym ** acronyms; //array of pointers to acronym entries
} AcronymDB;
这是发生问题的函数调用。
delimbuff 是strtok的输出,例如传递的首字母缩写字符串 r 是首字母缩写条目的当前行,在我的情况下,它是第一个首字母缩写,因此 r 为0。
我尚未实现任何错误处理。
void addAcronym( char * delimbuff, unsigned int r )
{
//this is where the problem occurs
AcronymDB * entry_list = malloc( sizeof( AcronymDB ) );
if ( entry_list == NULL )
{
}
entry_list -> acronyms = malloc( sizeof( Acronym * ) * rows );
if ( entry_list -> acronyms == NULL )
{
free( entry_list );
}
entry_list -> acronyms[ r ] = malloc( sizeof( Acronym ) );
entry_list -> acronyms[ r ] -> acronym = strdup( delimbuff );
}
我的期望是,即使存在内存分配问题,带有if语句的下一行也会捕获它,但是不会,并且代码将继续执行,直到函数结束,然后是更多。
>