调用 yylex() 后全局指针设置为 NULL
Global pointer set to NULL after calling yylex()
我在我的 flex 文件的定义部分声明一个 全局范围指针 时遇到问题,然后我 malloc
它在我的 main 文件的开头,但是 一旦我的程序运行到 yylex()
, 指针的值就设置为 NULL
。
我的程序中一直需要这个指向 struct
(这是 struct Modele * model
)的指针,它基本上是一个指向我存储文件中所有结果的结构的指针,所以我实际上不能没有它,至少不能没有指向在 main()
和 yylex()
.
中都可以正常工作的结构的指针
执行时,程序遇到段错误,试图在地址 0x4 处写入; 运行 valgrind下的程序,打印出model
的值,让我明白了内存是正确分配的,但是一调用yylex,model
的值就变成了NULL
(打印 (nil)
)。我在这里没有使用任何 header,但我尝试使用一个来存储我所有的结构,以及我的全局范围变量的声明,但没有成功。
我的问题是:面对这样的行为,我做错了什么?一般情况下,避免此问题的最佳方法是什么?我不确定我是否使用过全局范围指针,所以可能是这个,或者可能是 flex-lex 特定问题....我有点迷路了!
这是我的代码示例:
%{
#include <stdlib.h>
#include <stdio.h>
//some more includes and #defines
typedef struct Doc {
int classe;
uint32_t * words;
int current_index;
int current_size;
} doc;
typedef struct Modele {
int nb_classes;
int nb_docs;
int nb_docs_base;
int nb_docs_test;
int base_or_test;
int voc_size;
int M_test_size;
liste ** M_theta;
row_info * M_calc;
doc * M_test;
} modele;
//some more typedefs
modele * model; // <--- this is the pointer i talk about
//some more functions bodies .....
%}
couple_entiers [0-9]+:[0-9]+
// .......
%%
{couple_entiers} { model->nb_docs ++}
//.....
%%
int main (int argc, char ** argv)
{
// .....
modele * model = malloc(sizeof model); // <---- here is the malloc
model->nb_classes = 0;
model->nb_docs = 0;
model->nb_docs_base = 0;
model->nb_docs_test = 0;
model->voc_size = 0;
model->M_test = malloc (TAB_SIZE * sizeof(doc));
//....
if ((yyin = fopen(argv[1],"r")) == NULL){
printf("Impossible d'ouvrir %s !\n",argv[1]);
exit(0);
}
yylex();
如果那段代码不足以抓住问题的根源,我会粘贴更多,我只是想 select 相关部分。
My question is : what did I do wrong to face such a behavior ?
您从未设置文件范围变量。您的 main()
函数改为声明并初始化一个具有相同名称和类型的 local 变量。本地声明 "shadows" 文件范围内的一个在其范围内。
要修复它,只需更改此...
modele * model = malloc(sizeof model);
...为此:
model = malloc(sizeof model);
如果您没有在变量名前面加上类型,那么您指的是在别处(在本例中为文件范围)声明的变量。
我在我的 flex 文件的定义部分声明一个 全局范围指针 时遇到问题,然后我 malloc
它在我的 main 文件的开头,但是 一旦我的程序运行到 yylex()
, 指针的值就设置为 NULL
。
我的程序中一直需要这个指向 struct
(这是 struct Modele * model
)的指针,它基本上是一个指向我存储文件中所有结果的结构的指针,所以我实际上不能没有它,至少不能没有指向在 main()
和 yylex()
.
执行时,程序遇到段错误,试图在地址 0x4 处写入; 运行 valgrind下的程序,打印出model
的值,让我明白了内存是正确分配的,但是一调用yylex,model
的值就变成了NULL
(打印 (nil)
)。我在这里没有使用任何 header,但我尝试使用一个来存储我所有的结构,以及我的全局范围变量的声明,但没有成功。
我的问题是:面对这样的行为,我做错了什么?一般情况下,避免此问题的最佳方法是什么?我不确定我是否使用过全局范围指针,所以可能是这个,或者可能是 flex-lex 特定问题....我有点迷路了!
这是我的代码示例:
%{
#include <stdlib.h>
#include <stdio.h>
//some more includes and #defines
typedef struct Doc {
int classe;
uint32_t * words;
int current_index;
int current_size;
} doc;
typedef struct Modele {
int nb_classes;
int nb_docs;
int nb_docs_base;
int nb_docs_test;
int base_or_test;
int voc_size;
int M_test_size;
liste ** M_theta;
row_info * M_calc;
doc * M_test;
} modele;
//some more typedefs
modele * model; // <--- this is the pointer i talk about
//some more functions bodies .....
%}
couple_entiers [0-9]+:[0-9]+
// .......
%%
{couple_entiers} { model->nb_docs ++}
//.....
%%
int main (int argc, char ** argv)
{
// .....
modele * model = malloc(sizeof model); // <---- here is the malloc
model->nb_classes = 0;
model->nb_docs = 0;
model->nb_docs_base = 0;
model->nb_docs_test = 0;
model->voc_size = 0;
model->M_test = malloc (TAB_SIZE * sizeof(doc));
//....
if ((yyin = fopen(argv[1],"r")) == NULL){
printf("Impossible d'ouvrir %s !\n",argv[1]);
exit(0);
}
yylex();
如果那段代码不足以抓住问题的根源,我会粘贴更多,我只是想 select 相关部分。
My question is : what did I do wrong to face such a behavior ?
您从未设置文件范围变量。您的 main()
函数改为声明并初始化一个具有相同名称和类型的 local 变量。本地声明 "shadows" 文件范围内的一个在其范围内。
要修复它,只需更改此...
modele * model = malloc(sizeof model);
...为此:
model = malloc(sizeof model);
如果您没有在变量名前面加上类型,那么您指的是在别处(在本例中为文件范围)声明的变量。