我正在使用推送和弹出功能创建一个简单的堆栈。我正在尝试将一系列字符串推入一个充当内存堆栈的数组中。但是,GDB一直告诉我,我没有正确地将字符串复制到数组中。任何人都有关于如何解决这个问题的想法?
/*************************************************************************
* stack.c
*
* Implements a simple stack structure for char* s.
************************************************************************/
// for strdup() in the testing code
#define _XOPEN_SOURCE 500
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// the capacity of the stack
#define CAPACITY 10
//global variable used to keep track of pop and push
typedef struct
{
// storage for the elements in the stack
char* strings[CAPACITY];
// the number of elements currently in the stack
int size;
} stack;
// declare a stack (as a global variable)
stack s;
/**
* Puts a new element into the stack onto the "top" of the data structure
* so that it will be retrived prior to the elements already in the stack.
*/
bool push(char* str)
{
int i = 0;
s.strings[i++] = strdup(str);
++s.size;
return false;
}
/**
* Retrieves ("pops") the last ("top") element off of the stack, following
* the "last-in, first-out" (LIFO) ordering of the data structure. Reduces
* the size of the stack.
*/
char* pop(void)
{
int i = CAPACITY-1;
return s.strings[i--];
}
/**
* Implements some simple test code for our stack
*/
int main(void)
{
// initialize the stack
s.size = 0;
printf("Pushing %d strings onto the stack...", CAPACITY);
for (int i = 0; i < CAPACITY; i++)
{
char str[12];
sprintf(str, "%d", i);
push(strdup(str));
}
printf("done!\n");
printf("Making sure that the stack size is indeed %d...", CAPACITY);
assert(s.size == CAPACITY);
printf("good!\n");
printf("Making sure that push() now returns false...");
assert(!push("too much!"));
printf("good!\n");
printf("Popping everything off of the stack...");
char* str_array[CAPACITY];
for (int i = 0; i < CAPACITY; i++)
{
str_array[i] = pop();
}
printf("done!\n");
printf("Making sure that pop() returned values in LIFO order...");
for (int i = 0; i < CAPACITY; i++)
{
char str[12];
sprintf(str, "%d", CAPACITY - i - 1);
assert(strcmp(str_array[i], str) == 0);
free(str_array[i]);
}
printf("good!\n");
printf("Making sure that the stack is now empty...");
assert(s.size == 0);
printf("good!\n");
printf("Making sure that pop() now returns NULL...");
assert(pop() == NULL);
printf("good!\n");
printf("\n********\nSuccess!\n********\n");
return 0;
}
答案 0 :(得分:1)
pop()函数总是返回相同的字符串:
char* pop(void)
{
int i = CAPACITY-1; // i is the same!
return s.strings[i--]; // the same pointer
}
我建议将其更改为以下内容:
char* pop(void)
{
if (s.size == 0) return NULL;
char *str = s.strings[--s.size];
s.strings[s.size] = NULL;
return str;
}
它可以避免您将过时的指针存储在堆栈中。
答案 1 :(得分:1)
您的推送功能始终将字符串插入位置0。
改变如下:
bool push(char* str)
{
if (s.size == CAPACITY) return true; // Stack is full!
s.strings[s.size++] = strdup(str);
return false;
}
有了pop(我从Michaels那里偷了这个回答并为空堆添加了警卫):
char* pop(void)
{
if (s.size == 0) return NULL; // Stack is empty!
char *str = s.strings[--s.size];
s.strings[s.size] = NULL;
return str;
}