C Issues with Stack of Strings
So I'm trying to create a Stack of Strings in C and I seem to be running into an issue. The goal is to read a file, and print it in reverse. I decided a stack would be the most appropriate way to do this (I realize there are easier ways to do this, but I wanted to challenge myself with the use of structures).
Here is my push / printStack code:
void push(struct LineStack * stack, char * line)
{
if(!stack->head)
{
stack->head = malloc(sizeof(struct entry *));
stack->head->data = line;
stack->head->next = NULL;
stack->top = stack->head->data;
stack->size++;
}
else
{
struct entry * entry = malloc(sizeof(struct entry *));
entry->data = line;
entry->next = stack->head;
stack->head = entry;
stack->top = stack->head->data;
stack->size++;
}
}
void printStack(struct LineStack * stack)
{
while(stack->head)
{
printf("%sn", stack->head->data);
stack->head = stack->head->next;
}
}
And here is main / tempFile.txt:
int main(void)
{
struct LineStack * stack = newStack();
char * fileName = "tempFile.txt"
char line[SIZE];
FILE * fp = fopen(fileName, "r");
while(fgets(line, 128, fp) != NULL)
push(stack, line);
printStack(stack);
free(stack);
return 0;
}
tempFile.txt:
Lets begin
We'll say 2 + 2 = 4
But then go ahead and prove that 1 + 2 + 3 + 4 + ... = -1/12
How can this be?
How can this be?
When I try to run the code, it prints out the correct number of lines in the file (5), but only prints out "How can this be". Now, I've tried using GDB to see what the issue is, and it seems that the push calls are working properly. Each call puts a different line in a different memory location so I have to assume the Linked List that makes up the stack is performing fine. Am I missing something stupid and small?
For reference, here is the entry / LineStack declarations:
struct entry
{
char * data;
struct entry * next;
};
struct LineStack
{
struct entry * head;
char * top;
int size;
};
Thanks in advance.
Your main()
reads each line into local array line
. It then passes (a pointer to) this array to function push()
. That function simply stores this pointer in the stack -- the data are not copied, just the pointer. As a result, all of your stack entries contain pointers to the same array; when you print them, this array contains the last line read from the file, and that's what you print, as many times as you read lines.
You need to copy the input strings. If you have strdup()
(a POSIX function but not a standard C one) then that's about the easiest way to make such a copy. Otherwise, [ strlen()
+] malloc()
+ strcpy()
would be the conventional way to make a copies of your strings. Either way, remember that you are responsible for freeing each piece of dynamically-allocated memory when you are done with it, before allowing the last pointer to it to be lost.
Apart from what @JohnBollinger already said, you have the following problem:
stack->head = malloc(sizeof(struct entry *));
Here, you allocate a chunk of memory the size of a pointer to struct entry
, not the size of struct entry
itself. It should be:
stack->head = malloc(sizeof(struct entry));
The same problem happens when you allocate memory for a stack->entry
.
上一篇: 基本使用堆栈
下一篇: C堆栈字符串的问题