简体   繁体   English

将指针传递给struct并将int传递给C中的函数以实现堆栈

[英]Pass a pointer to struct and a int to a function in C to implement a stack

I'm trying to implement a stack in C. I have only implemented the struct that will contain an array and that currently only contains the size of the array and the position of the last item added to the stack 我正在尝试在C中实现堆栈。我仅实现了将包含一个数组的结构,该结构当前仅包含该数组的大小以及添加到堆栈中的最后一项的位置

This is a partial implementation that is giving me some trouble. 这是部分实现,给我带来了麻烦。

in stack.h 在stack.h中

#include <stdlib.h>
#include <stdbool.h>

typedef struct Stack
{
    int max_size;
    int top;
    // int *contents;
} Stack;

Stack *stack_create(int n);
bool stack_is_empty(Stack *stack);
bool stack_is_full(Stack *stack);
void stack_push(Stack *stack, int value);

in stack.c: 在stack.c中:

#include <stdio.h>
#ifndef STACK_H
#include "stack.h"
#endif

Stack *stack_create(int n)
{
    Stack stack;
    Stack *s = &stack;
    s->max_size = n;
    s->top = 0;
    // s->contents = (int *)malloc(sizeof(int) * n);
    return s;
}


bool stack_is_empty(Stack *stack)
{
    if (stack->top == 0)
    {
        return true;
    }
    return false;
}

bool stack_is_full(Stack *stack)
{
    if (stack->top == stack->max_size)
    {
         return true;
    }
    return false;
} 

void stack_push(Stack *stack, int value)
{

     if (!stack_is_full(stack))
     {
          printf("max_size: %d\n", stack->max_size);
          printf("top: %d (%p)\n", stack->top++, &stack->top);
          printf("value: %d (%p)\n", value, &value);
     }
     else
     {
          printf("Can't push. max_size==%d reached.\n", stack- >max_size);
          exit(EXIT_FAILURE);
     }
}

and in main.c: 在main.c中:

 #include <stdio.h>
 #include <stdlib.h>
 #include "stack.h"

 #define SIZE 3

 int main()
 {
     Stack *s = stack_create(SIZE);
     printf("stack_is_empty: %d\n", stack_is_empty(s));
     stack_push(s, 100);
     printf("stack_is_empty: %d\n", stack_is_empty(s));
     stack_push(s, 30);
     printf("stack_is_empty: %d\n", stack_is_empty(s));
     stack_push(s, 20);
     printf("stack_is_empty: %d\n", stack_is_empty(s));

     return 0;
 }

main produces the following output: main产生以下输出:

stack_is_empty: 1
max_size: 3
top: 100 (0x7ffd5430dfb4)
value: 101 (0x7ffd5430dfb4)
stack_is_empty: 0
max_size: 3
top: 30 (0x7ffd5430dfb4)
value: 31 (0x7ffd5430dfb4)
stack_is_empty: 0
max_size: 3
top: 20 (0x7ffd5430dfb4)
value: 21 (0x7ffd5430dfb4)
stack_is_empty: 0

Why is value 's address the same of stack->top ? 为什么value的地址与stack->top相同?

Problem 1 : You are allocating memory for the stack locally in stack_create function. 问题1:您正在stack_create函数中为堆栈本地分配内存。 As soon as the function goes out of scope memory will be freed. 一旦功能超出范围,内存将被释放。 Thus you will have a dangling pointer. 因此,您将有一个悬空的指针。

Problem 2 : You are allocating memory only for one instance regardless of value of 'n' 问题2:您只为一个实例分配内存,而不考虑'n'的值

typedef struct Stack
{
    int max_size;
    int *contents;
    int top;
    // int *contents;
} Stack;

Stack *stack_create(int n) {
    Stack *s;
    s = (Stack *)malloc(sizeof(Stack));
    s->contents = (int *)malloc(sizeof(int) * n);
    s->max_size = n;
    s->top = 0;
    return s;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM