链接列表值正在更改

时间:2013-03-23 03:00:01

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

相关代码:

Constant Def:

#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. */

Constant Def:

#define BUFFER_START_SIZE 20
#define BUFFER_INC_SIZE 10

NextLine功能:

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 - &gt; Insur)。我只是希望我读到的条目(Instr)在整个阅读过程中保持不变。

1 个答案:

答案 0 :(得分:0)

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