GDB告訴我52行正在導致分段錯誤。我不明白爲什麼。我正在實現一個簡單的堆棧。它有兩個功能:彈出和推送。它出現流行不工作。 pop的目的是檢索堆棧中最高的值。但是,當它嘗試這樣做時,我會遇到分段錯誤。有誰知道原因?如何解決C中的函數傳遞指針的分段錯誤?
/*************************************************************************
* 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)
{
s.strings[s.size++] = strdup(str);
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)
{
char *ptr = s.strings[--s.size];
s.strings[s.size] = NULL;
return ptr;
}
/**
* 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;
}
哪一條是52號線? – Sinkingpoint
char * ptr = s.strings [ - s.size]; – Adam
你應該使用[valgrind](http://valgrind.org/)來了解爲什麼分段錯誤的答案。 –