【问题标题】:Linked list value being changed [closed]链接列表值正在更改[关闭]
【发布时间】:2013-03-23 03:00:01
【问题描述】:

相关代码:

常量定义:

#define MAX_NAME_LEN 15 /* Maximum length of any attribute or relation is 15. */

结构定义:

typedef struct schnode {
char name[MAX_NAME_LEN + 1]; /* The name of the attribute. */
char type; /* The type of the attribute ('S' or 'I'). */
int len; /* The length of the attribute. */
struct schnode *next; /* A pointer to the next node. */
} SCHNODE;

变量定义:

FILE *schemafile; /* A text file. */
SCHNODE *head = NULL; /* Head of the linked list. */
SCHNODE *ptrnode = NULL; /* A variable node in the linked list. */
SCHNODE *p = NULL; /* A variable node in the linked list. */
SCHNODE *tail = NULL; /* Tail of the linked list. */
char *attr_name; /* Holds the name of the current attribute. */
char *attr_type; /* Holds the current attribute type (S or I). */
int attr_len; /* Holds the current attribute length. */
int attr_loc = 0; /* Attribute location in respect to the tuple. */
int i; /* Index for loops. */
char attr_match = 0; /* 1 if the attribute is valid, 0 otherwise. */

常量定义:

#define BUFFER_START_SIZE 20
#define BUFFER_INC_SIZE 10

下一行功能:

char* NextLine (FILE *f) {
int size = BUFFER_START_SIZE;
char *buf; //buffer storing current line as a char array
int i = 0; //index for array
int c; //a character

if (f == NULL){ //check if the file pointer is NULL
    printf("Unable to find the file.\n"); fflush(stdout);
    return NULL; //nothing to read, so return NULL
}

if((c = getc(f)) == EOF) //check if the first 'char' in the line is EOF
    return NULL; //if so, line is empty, so return NULL

if((buf = (char *) malloc(size)) == NULL){ //make room for the buffer
    printf("Failed to create buffer.\n"); fflush(stdout); }

buf[0] = (char) c; //put the first char into the buffer string
i = 1; //and increment the index

while(1){
    while(i < size){ //loop through the buffer char array
        if((c = getc(f)) == '\n'){ //if end of line
            buf[i] = '\0'; //instead put '\0' to terminate string
            return buf; //and return the completed string
        }
        buf[i] = (char) c; //otherwise, just keep copying the characters
        i++; //increment the index
    }

    //IF REACHED THIS POINT, LINE IS TOO LONG TO FIT IN BUFFER -> REALLOC
    size += BUFFER_INC_SIZE;
    if((buf = (char*) realloc(buf,size)) == NULL){ //make buffer bigger
        printf("Unable to realloc memory.\n"); fflush(stdout); }
    //loop and continue adding chars from where you left off
}
}

执行代码:

for (i = 0; i < num_attr; i++) {
    if ((i > 0) && !attr_match) attr_loc += attr_len;
    attr_name = NextLine(schemafile);
    attr_name = strtok(attr_name, " \t\n");
    attr_type = strtok(NULL, " \t\n");
    attr_len =  atoi(strtok(NULL, " \t\n"));

    /* Construct linked list of schema file data. */
    /* If empty: */
    if (head == NULL) {
        if ((head = (SCHNODE*) malloc(sizeof(SCHNODE))) == NULL) {
            fprintf(stderr, "Unable to Malloc space.\n");
            return;
        }
        strcpy(head->name, attr_name);
        head->type = attr_type[0];
        head->len = attr_len;
        head->next = NULL;
    }

    /* If the list already has a head defined: */
    else {
        /* Start from the head if tail is NULL (only 1 element) otherwise start from tail. */
        if(tail == NULL) ptrnode = head;
        else ptrnode = tail;
        /* Malloc space for the tail node. */
        if ((tail = (SCHNODE*) malloc(sizeof(SCHNODE))) == NULL) {
            fprintf(stderr, "Unable to Malloc space.\n");
            return;
        }
        strcpy(tail->name, attr_name);
        tail->type = attr_type[0];
        tail->len = attr_len;
        tail->next = NULL;
        /* Insert tail node at the end. */
        ptrnode->next = tail;
        if (strcmp(tail->name, "Instr") == 0)
            p = tail;
        if (p != NULL) printf("%d: %s\n", i, p->name);
    }

输入:

CName  S  25
CId    S  8
Instr  S  10
Credits I  4

输出:

2: Instr
3: Insur

没有其他值被改变(据我所知)。有人可以解释为什么这个特定的值总是被改变吗? (Instr -> 保险)。我只是希望我阅读的条目(Instr)在整个阅读过程中保持不变。

【问题讨论】:

  • 我有一个问题。在您的实现中,strcpy(head-&gt;name, attr_name); 我不清楚是谁在为您的数据结构的name 字段分配空间。所以,当你strcpy 时,我觉得有潜在的内存损坏。我在这里错过了什么吗?您能否确认您是否面临同样的问题,namearray 而不是 pointer
  • nvm 你是个天才。非常感谢。
  • 是否有必要在您的最小可编译测试用例中使用所有这些代码?这个想法是为我们提供足够的代码,以便问题像拇指一样突出。复制您的项目,将其称为您的测试用例并开始减少它。例如,将 NextLine 的正文替换为 char *foo = malloc(256); fgets(foo, 256, stdin); return foo;。如果问题消失了,那么问题一定出在您的 NextLine 函数中。给我们这个函数来分析。 ...
  • @ICantNameMe .. 你能尝试一个小实验,其中第 4 个元素 不是 Credits I 4,而是像这样的PId I 4。您是否还看到此输入数据的问题?我怀疑你的 realloc 逻辑,因此,这个请求。
  • 这个问题似乎离题了,因为它不太可能在将来帮助 Stack Overflow 的其他访问者。

标签: c linked-list singly-linked-list


【解决方案1】:

在您的程序中,数据结构的name 字段被定义为pointer。因此,当您strcpy 时,会出现内存损坏,因为没有为pointer 分配空间。因此,对于每个节点,mallocname 提供一个空格,或者将 name 定义为 n 元素的数组。

【讨论】:

    猜你喜欢
    • 2011-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-17
    • 1970-01-01
    • 2014-10-06
    相关资源
    最近更新 更多