- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 Flex 文件的定义部分中声明全局作用域指针时遇到问题,然后我在主文件的开头 malloc
它,但是 < strong>一旦我的程序运行到yylex()
,指针的值就会设置为NULL
。
我在整个程序中都需要这个指向struct
(这是struct Modele * model
)的指针,它基本上是一个指向我存储所有结果的结构的指针文件,所以我实际上不能没有它,至少不能没有一个指向在 main()
和 yylex()
中都可以正常工作的结构的指针。
执行时,程序遇到段错误,尝试在地址 0x4 处写入;在 valgrind 下运行程序,并打印 model
的值,让我了解到内存已正确分配,但是一旦调用 yylex,model
的值就变成了NULL
(打印(nil)
)。我在这里没有使用任何 header ,但我尝试使用一个 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();
如果那段代码不足以捕获问题的根源,我会粘贴更多的代码,我只是想选择相关的部分。
最佳答案
My question is : what did I do wrong to face such a behavior ?
您从未设置过文件范围变量。您的 main()
函数而是声明并初始化一个具有相同名称和类型的本地变量。本地声明“隐藏”其范围内的文件范围声明。
要修复它,只需更改此...
modele * model = malloc(sizeof model);
...对此:
model = malloc(sizeof model);
如果变量名前面没有类型,那么您引用的是在其他地方声明的变量(在本例中是在文件范围内)。
关于c - 调用 yylex() 后全局指针设置为 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42518896/
本文整理了Java中jxl.biff.formula.Yylex.yylex()方法的一些代码示例,展示了Yylex.yylex()的具体用法。这些代码示例主要来源于Github/Stackoverf
本文整理了Java中org.json.simple.parser.Yylex.yylex()方法的一些代码示例,展示了Yylex.yylex()的具体用法。这些代码示例主要来源于Github/Stac
本文整理了Java中org.apache.dubbo.common.json.Yylex.yylex()方法的一些代码示例,展示了Yylex.yylex()的具体用法。这些代码示例主要来源于Githu
本文整理了Java中de.odysseus.staxon.json.stream.impl.Yylex.yylex()方法的一些代码示例,展示了Yylex.yylex()的具体用法。这些代码示例主要来
本文整理了Java中com.taobao.middleware.logger.json.parser.Yylex.yylex()方法的一些代码示例,展示了Yylex.yylex()的具体用法。这些代码
我正在尝试将位置信息添加到flex和bison中。但是我的野牛仍然使用int yylex(void),但是找不到int yylex(YYSTYPE *yylval_param, YYLTYPE *yy
所以我的问题很简单,我想之前已经有人回答了,但我无法找到关于它的好线索:我可以解析一次文件,收集一些必要的信息来初始化我的结构,然后解析第二次是“真的”吗? 当我两次调用 yylex() 时,它似乎什
我在 Flex 文件的定义部分中声明全局作用域指针时遇到问题,然后我在主文件的开头 malloc 它,但是 一旦我的程序运行到yylex(),指针的值就会设置为NULL。 我在整个程序中都需要这个指向
我正在尝试学习如何构建基于 C++ 的词法分析器/解析器,我正在关注多个在线教程和指南,我的代码主要是根据这两个指南构建的:1 , 2尽管我添加和调整了我认为合适的内容。 首先,我有一个词法分析器的头
我有一个简单的 flex 源代码,它跳过 /* */ 中的注释,应该得到找到的注释数: %{ int in_comment = 0; int count = 0; %} %% \/\* { i
我正在使用词法分析。为此,我使用 Flex然后我获取以下问题。 工作.l int cnt = 0,num_lines=0,num_chars=0; // Problem here. %% [" "]+
本文整理了Java中jxl.biff.formula.Yylex.yytext()方法的一些代码示例,展示了Yylex.yytext()的具体用法。这些代码示例主要来源于Github/Stackove
本文整理了Java中jxl.biff.formula.Yylex.zzUnpackAttribute()方法的一些代码示例,展示了Yylex.zzUnpackAttribute()的具体用法。这些代码
本文整理了Java中jxl.biff.formula.Yylex.zzUnpackTrans()方法的一些代码示例,展示了Yylex.zzUnpackTrans()的具体用法。这些代码示例主要来源于G
本文整理了Java中jxl.biff.formula.Yylex.zzScanError()方法的一些代码示例,展示了Yylex.zzScanError()的具体用法。这些代码示例主要来源于Githu
本文整理了Java中jxl.biff.formula.Yylex.zzUnpackRowMap()方法的一些代码示例,展示了Yylex.zzUnpackRowMap()的具体用法。这些代码示例主要来源
本文整理了Java中jxl.biff.formula.Yylex.zzUnpackAction()方法的一些代码示例,展示了Yylex.zzUnpackAction()的具体用法。这些代码示例主要来源
本文整理了Java中jxl.biff.formula.Yylex.getPos()方法的一些代码示例,展示了Yylex.getPos()的具体用法。这些代码示例主要来源于Github/Stackove
本文整理了Java中jxl.biff.formula.Yylex.zzRefill()方法的一些代码示例,展示了Yylex.zzRefill()的具体用法。这些代码示例主要来源于Github/Stac
本文整理了Java中jxl.biff.formula.Yylex.yybegin()方法的一些代码示例,展示了Yylex.yybegin()的具体用法。这些代码示例主要来源于Github/Stacko
我是一名优秀的程序员,十分优秀!