结构在功能后仍未编辑

时间:2014-04-26 11:56:30

标签: c struct

我已经定义了一些结构,我创建了一个从XML读取数据并将其插入DialogueRules结构的函数。

一切都应该有效,但在执行之后,一些价值观仍然保持不变,即使他们应该这样做。我已经调试过,我发现它们设置正确,但是当我退出 dialog_load 功能时,它们会丢失它们的值。 (我跑了Valgrind并没有发现任何奇怪的事情)

经过更多的调试,我发现一些指针,比如dr-> l_topic [1] .name,指向main中的一个方向,指向 dialog_load 中的另一个指针,然后他们当我回到主要时,指向原始功能。

我没有一个关于它为什么失败的线索,原因是什么?

代码如下:

DialogueRules * dr=create_dialog();  //Reserve memory for the pointer
printf("t0: %d \n",dr->l_topic[1].name); //The pointer has the original value
dialogue_load("rules.xml",dr);  //Loads the xml
printf("t2: %d \n",dr->l_topic[1].name); //The pointer has the original value

Dialogue_load是

STATUS dialogue_load(char * file_name,DialogueRules *dr){
printf("t1: %d \n",dr->l_topic[1].name);//The pointer has another value!

//Load the xml
 xmlDocPtr doc;
xmlNodePtr node;

if (!file_name) {
    return ERROR;
}

if ((doc = xmlParseFile(file_name))== NULL) {
    fprintf(stderr,"File %s has not XML format.\n", file_name);

    return ERROR;
}

node = xmlDocGetRootElement(doc);
if (node == NULL) {
    fprintf(stderr, "Empty document\n");
    xmlFreeDoc(doc);
    return ERROR;
}

if (xmlStrcmp(node->name, (const xmlChar*) ROOT)) {
    fprintf(stderr,"Expected <%s>, instead of %s\n", ROOT, node->name);
    xmlFreeDoc(doc);
    return ERROR;
}



node = node->xmlChildrenNode;
while(node != NULL) {

    if (!xmlStrcmp(node->name, (const xmlChar*) RULES)) {
        dreader_process_rules(doc, node->xmlChildrenNode, dr);
    }
    else if (!xmlStrcmp(node->name, (const xmlChar*) TOPICS)) {
        dreader_process_topics(doc, node->xmlChildrenNode, dr);
    }

    node = node->next;
}
xmlFreeDoc(doc);
return OK;

}

Create_dialog是

DialogueRules *create_dialog() {
 DialogueRules * dialog = (DialogueRules*)malloc(sizeof(DialogueRules));
 dialog->num_rules=0;
 dialog->num_topic=0;
 return dialog;
}

结构如下:

typedef struct _Topic
{
Id id;
char name[WORD_SIZE+1]; /* topic name */
Set * topic_rules; /* set of rule indexes associated with a topic */
} Topic;


typedef struct _Rule
{
Id id; /* rule identifier */
char * pattern [MAX_PATTERNS];/* If the string matches any of these input patterns,    then executes this rule */
int num_patterns;/* number of patterns */
char * template [MAX_PATTERNS]; /* List of pos sible templates that can be used as a    response */
int num_templates; /* number of possible answers */
int last; /* Last used template */
} Rule;

typedef struct _DialogueRules
{
Rule l_rule[MAX_RULES]; /* vector to store rules */
int num_rules; /* number of rules stored in l_rule */
Topic l_topic[MAX_TOPICS]; /* vector to store topics */
 int num_topic; /* number of topicsstored in l_topic */
} DialogueRules;

编辑:

使用%p时的输出是:

t0:0x7f57d20afe98

t1:0x7f57d20afe80

t2:0x7f57d20afe98

编辑2:

main的makefile规则是:

main.o: main.c 
 $(CC) ${CFLAGS} ${CXML2} -c main.c -o main.o

和dialoguerrulesreader(包含dialog_rule)是

 DialogueRulesReader.o: DialogueRulesReader.c DialogueRulesReader.h 
   ${CC}  ${CXML2} -c  $< ${LXML2}

(考虑到

CC=gcc  -ggdb
CFLAGS=-Wall -pedantic -ansi
CXML2=`xml2-config --cflags`
LXML2=`xml2-config --libs`

1 个答案:

答案 0 :(得分:1)

在评论中发现;你应该使用相同的CFLAGS来构建所有单位。事实证明,在一个文件上使用不同的标志(特别是-ansi,而在另一个文件上没有标准规范)导致结构在两个不同的单元中具有不同的大小。

相关问题