C 大小为8的读取无效-堆栈ADT

C 大小为8的读取无效-堆栈ADT,c,C,在C代码中。这是我的代码(这是一个练习,我可以做更改,但不能更改结构和测试函数) 这些是堆栈节点和堆栈结构: /*structure of each node of the stack*/ struct stack_node_s { void *data; /*user provided data*/ struct stack_node_s *next; /*pointer to the next node in the stack*/ }; /*defines the typ

在C代码中。这是我的代码(这是一个练习,我可以做更改,但不能更改结构和测试函数)

这些是堆栈节点和堆栈结构:

/*structure of each node of the stack*/
struct stack_node_s {
    void *data; /*user provided data*/
    struct stack_node_s *next; /*pointer to the next node in the stack*/
};

/*defines the type of a stack node*/
typedef struct stack_node_s stack_node_t;

/*defines the structure of the stack*/
struct stack_s {
    stack_node_t *top; /*the top of the stack*/
    size_t size; /*size of the stack*/
};

/*defines the stack type*/
typedef struct stack_s* stack_t;
堆栈操作功能:

stack_t stack_create()
{
  stack_t stack = malloc(sizeof(struct stack_s));
  if (stack == NULL) {
    printf("Unable to create a stack\n");
    abort();
  }

  stack->top = NULL;
  stack->size = 0;

  return stack;
}

void stack_destroy(stack_t stack, int destroy_data)
{
  if (stack != NULL) {
    stack_clear(stack, destroy_data);
    free(stack);
  }
}

void stack_push(stack_t stack, void *data)
{
  if (stack != NULL) {
    stack_node_t *node = malloc(sizeof(stack_node_t));

    if (node == NULL) {
        perror("Unable to create a stack node\n");
        abort();
    }
    node->data = data;
    node->next = stack->top;
    stack->top = node;
    stack->size += 1;
  }
}

void stack_pop(stack_t stack, int destroy_data)
{
  if (!stack_is_empty(stack)) {
    stack_node_t *node = stack->top;
    stack->top = stack->top->next;
    stack->size -= 1;

    if (destroy_data) {
        free(node->data);
    }

    free(node);
  }
}

void* stack_top(const stack_t stack)
{
  if (stack_is_empty(stack)) {
    return NULL;
  }

  return stack->top->data;
}

int stack_is_empty(const stack_t stack)
{
  if (stack->top == NULL) {
    return 1;
  }

  return 0;
}

size_t stack_size(const stack_t stack)
{
  return stack->size;
}

void stack_clear(stack_t stack, int destroy_data)
{
  while (stack->top != NULL) {
    stack->size -= 1;
    stack_node_t *node = stack->top;
    stack->top = stack->top->next;

    if (destroy_data) {
        free(node->data);
    }
    free(node);
  }
}
测试功能(已编辑):

正确编译,但valgrind会显示以下消息:

Test case 'null'... ==4472== Invalid read of size 8
==4472==    at 0x10938F: stack_is_empty (in /home/francesco/Documents/stack/stack)
==4472==    by 0x10948F: test_null (in /home/francesco/Documents/stack/stack)
==4472==    by 0x1091D2: main (in /home/francesco/Documents/stack/stack)
==4472==  Address 0x0 is not stack'd, malloc'd or (recently) free'd
==4472== 
==4472== 
==4472== Process terminating with default action of signal 11 (SIGSEGV)
==4472==  Access not within mapped region at address 0x0
==4472==    at 0x10938F: stack_is_empty (in /home/francesco/Documents/stack/stack)
==4472==    by 0x10948F: test_null (in /home/francesco/Documents/stack/stack)
==4472==    by 0x1091D2: main (in /home/francesco/Documents/stack/stack)
==4472==  If you believe this happened as a result of a stack
==4472==  overflow in your program's main thread (unlikely but
==4472==  possible), you can try to increase the size of the
==4472==  main thread stack using the --main-stacksize= flag.
==4472==  The main thread stack size used in this run was 8388608.
这似乎是堆栈大小函数中的错误,但我不知道。。。 有人能帮我吗? 谢谢


编辑:我编辑代码以仅显示出现问题的测试函数。我在stack\u size函数中添加了一个控件(如果stack==NULL),现在问题似乎出在stack\u为空…

stack\u size
应该在开始时具有
如果(!stack)返回0
因为它需要处理传入
NULL
指针的情况-这显然是
test\u NULL
测试用例中正在做的事情。类似地,需要在其他函数中执行空处理,例如
stack\u is\u empty
(尽管该函数实际上应该只调用
stack\u size
)。请尝试将测试用例最小化,最好是复制不需要的行为的单个测试用例。然后进一步最小化该测试用例,直到您没有得到错误。重新添加最后一位(并重新引入错误)和您的问题以仅显示该位。代码已编辑。现在只有test函数存在问题,
stack\u size
应该在开始时设置
if(!stack)返回0
因为它需要处理传入
NULL
指针的情况-这显然是
test\u NULL
测试用例中正在做的事情。类似地,需要在其他函数中执行空处理,例如
stack\u is\u empty
(尽管该函数实际上应该只调用
stack\u size
)。请尝试将测试用例最小化,最好是复制不需要的行为的单个测试用例。然后进一步最小化该测试用例,直到您没有得到错误。重新添加最后一位(并重新引入错误)和您的问题以仅显示该位。代码已编辑。现在只有测试函数存在问题
Test case 'null'... ==4472== Invalid read of size 8
==4472==    at 0x10938F: stack_is_empty (in /home/francesco/Documents/stack/stack)
==4472==    by 0x10948F: test_null (in /home/francesco/Documents/stack/stack)
==4472==    by 0x1091D2: main (in /home/francesco/Documents/stack/stack)
==4472==  Address 0x0 is not stack'd, malloc'd or (recently) free'd
==4472== 
==4472== 
==4472== Process terminating with default action of signal 11 (SIGSEGV)
==4472==  Access not within mapped region at address 0x0
==4472==    at 0x10938F: stack_is_empty (in /home/francesco/Documents/stack/stack)
==4472==    by 0x10948F: test_null (in /home/francesco/Documents/stack/stack)
==4472==    by 0x1091D2: main (in /home/francesco/Documents/stack/stack)
==4472==  If you believe this happened as a result of a stack
==4472==  overflow in your program's main thread (unlikely but
==4472==  possible), you can try to increase the size of the
==4472==  main thread stack using the --main-stacksize= flag.
==4472==  The main thread stack size used in this run was 8388608.