0
相關的代碼:鏈表值被改變
常量缺省值:
#define MAX_NAME_LEN 15 /* Maximum length of any attribute or relation is 15. */
STRUCT缺省值:
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
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 - > Insur)。我只想讓我讀的條目(Instr)在整個閱讀過程中保持不變。
我有一個問題。在你的實現中,'strcpy(head-> name,attr_name);'我不清楚誰爲你的數據結構的'name'字段分配空間。所以,當你'strcpy'時,我覺得有可能發生內存損壞。我在這裏錯過了一些觀點嗎?你能否確認你是否面臨同樣的問題,將'name'作爲'array'而不是'pointer'? – Ganesh 2013-03-23 03:13:35
nvm你是個天才。非常感謝你。 – ICantNameMe 2013-03-23 03:22:47
是否有必要在最小的,可編譯的測試用例中使用*所有這些代碼?這個想法是爲我們提供足夠的代碼,以便讓問題像拇指一樣突出。複製你的項目,稱之爲你的測試用例並開始減少它。例如,用'char * foo = malloc(256)替換NextLine的主體; fgets(foo,256,stdin);返回foo;'。如果問題消失,那麼問題必須出現在您的NextLine函數中。給我們分析的功能。 ... – Sebivor 2013-03-23 03:29:25