释放 C 堆栈和删除悬空指针

发布于 2024-10-26 06:47:11 字数 4920 浏览 5 评论 0原文

我已经用 C 语言实现了一个堆栈,使用 stackADT 结构和一组函数:

#ifndef _stack_h
#define _stack_h

// Macros
#define MaxStackSize 100
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

// The type of element that may 
// be stored in the stack
typedef int stackElementT;

// The stackADT represents the abstract type used to store
// the elements that have been pushed
typedef struct stackCDT
{
    // A pointer to an array of elements
    stackElementT* elements;
    // Number of elements on the stack
    int count;
    // Number of elements we can push onto
    // the stack before having to resize
    int size;
}* stackADT;

// This function allocates and returns a new stack, which is
// initially empty... AKA - The Constructor
stackADT NewStack(void)
{
    // The stack to return
    stackADT stack;

    // Instanitate a new stack
    stack = (stackCDT*)(malloc(sizeof(stackCDT)));

    // Start with 0 elements of course
    stack->count = 0;
    // Allocate memory for 50 integers
    stack->elements = (stackElementT*)(malloc(50*sizeof(stackElementT)));
    // Establish the size of the stack
    stack->size = 50;

    return stack;
}


/********* GETTER FUNCTIONS *********/
// Returns the number of elements currently pushed 
// onto the stack
int StackDepth(stackADT stack)
{
    return (stack->count);
}

// This function returns the element a the specified index in
// the stack, where the top is defined as index 0
stackElementT GetStackElement(stackADT stack, int index);

// Function to print contents of stack
void PrintStack(stackADT stack)
{
    int i = 0;
    printf("count = %d\nsize = %d\n",stack->count,stack->size);

    for(i = (stack->count - 1); i >= 0; i--)
    {
        if((i%10 == 0) && (i != 0))
            printf("\n");
        printf("%d\t",*(stack->elements + i));
    }
}


// Functions to determine if stack is empty or full
int StackIsEmpty(stackADT stack)
{
    if(stack->count == 0)
        return 1;
    else
        return 0;
}
int StackIsFull(stackADT stack)
{
    if(stack->count == stack->size)
        return 1;
    else
        return 0;
}


// This function pushes the specified element onto the stack
void Push(stackADT stack, stackElementT element)
{
    // A temporary array that we may use later on
    stackElementT* temp = NULL;
    int oldCount = stack->count;
    int i = 0;

    // If the stack if full we need to do a
    // a transfer, resize, and retransfer, then push
    if(StackIsFull(stack))
    {
        // temp will be the same size as the old stack
        temp = (stackElementT*)(malloc((oldCount)*sizeof(stackElementT)));

        // Now we perform the transfer
        for(i = 0; i < oldCount; i++)
        {
            *(temp + i) = *((stack->elements) + i);
        }

        // Free the old memory
        free(stack->elements);
        stack->elements = NULL;

        // Recreate the stack with a 50% increase in size/capacity
        stack->elements = (stackElementT*)(malloc((3*oldCount/2)*sizeof(stackElementT)));
        // Re-establish the size
        stack->size = 3*oldCount/2;

        // Now we perform the transfer back
        for(i = 0; i < oldCount; i++)
        {
            *((stack->elements) + i) = *(temp + i);
        }

        // Free the temp array and 
        // remove dangling pointer
        free(temp);
        temp = NULL;

        // Now we push the element onto the stack
        *((stack->elements) + oldCount) = element;
        // Increase the count
        stack->count = oldCount + 1;
    }
    // If the stack isn't full
    else
    {
        *((stack->elements) + oldCount) = element;
        stack->count = oldCount + 1;
    }

}

// This function pops the top element from the stack and returns
// that value
stackElementT Pop(stackADT stack);


// This function frees the storage associated with the stack
void FreeStack(stackADT stack)
{
    // Start by freeing the elements on the stack
    // and remove dangling pointers
    free(stack->elements);
    stack->elements = NULL;

    // Finally free the stack
    free(stack);
    stack = NULL;
}

#endif

显然我还没有完全完成(需要一个 pop 函数)。我关心的是底层函数(FreeStack)。我测试了下面的代码:

#include <stdio.h>
#include <stdlib.h>
#include "Stack.h"
#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>

int main(void)
{
    stackADT stack;
    int i = 0;

    stack = NewStack();
    PrintStack(stack);

    for(i = 0; i < 60; i++)
    {
        Push(stack,i);
    }

    PrintStack(stack);
    FreeStack(stack);

    _CrtDumpMemoryLeaks();

    return 0;
}

_CrtDumpMemoryLeaks() 函数适用于 Visual Studio,它指示是否存在内存泄漏。显然,我在调用 FreeStack(stackADT stack) 函数时已经封堵了任何泄漏。但是,堆栈指针仍然保留内存地址,这是问题所在,因为 FreeStack 函数应该释放堆栈变量指向的内存并将其设置为 NULL。这发生在函数内,但是当我在调试期间返回主函数时,我看到内存地址仍然存在。我在这里缺少什么?如果我能够释放内存,为什么我不能删除悬空指针?

I've implemented a stack in C, using a stackADT struct and a set of functions:

#ifndef _stack_h
#define _stack_h

// Macros
#define MaxStackSize 100
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

// The type of element that may 
// be stored in the stack
typedef int stackElementT;

// The stackADT represents the abstract type used to store
// the elements that have been pushed
typedef struct stackCDT
{
    // A pointer to an array of elements
    stackElementT* elements;
    // Number of elements on the stack
    int count;
    // Number of elements we can push onto
    // the stack before having to resize
    int size;
}* stackADT;

// This function allocates and returns a new stack, which is
// initially empty... AKA - The Constructor
stackADT NewStack(void)
{
    // The stack to return
    stackADT stack;

    // Instanitate a new stack
    stack = (stackCDT*)(malloc(sizeof(stackCDT)));

    // Start with 0 elements of course
    stack->count = 0;
    // Allocate memory for 50 integers
    stack->elements = (stackElementT*)(malloc(50*sizeof(stackElementT)));
    // Establish the size of the stack
    stack->size = 50;

    return stack;
}


/********* GETTER FUNCTIONS *********/
// Returns the number of elements currently pushed 
// onto the stack
int StackDepth(stackADT stack)
{
    return (stack->count);
}

// This function returns the element a the specified index in
// the stack, where the top is defined as index 0
stackElementT GetStackElement(stackADT stack, int index);

// Function to print contents of stack
void PrintStack(stackADT stack)
{
    int i = 0;
    printf("count = %d\nsize = %d\n",stack->count,stack->size);

    for(i = (stack->count - 1); i >= 0; i--)
    {
        if((i%10 == 0) && (i != 0))
            printf("\n");
        printf("%d\t",*(stack->elements + i));
    }
}


// Functions to determine if stack is empty or full
int StackIsEmpty(stackADT stack)
{
    if(stack->count == 0)
        return 1;
    else
        return 0;
}
int StackIsFull(stackADT stack)
{
    if(stack->count == stack->size)
        return 1;
    else
        return 0;
}


// This function pushes the specified element onto the stack
void Push(stackADT stack, stackElementT element)
{
    // A temporary array that we may use later on
    stackElementT* temp = NULL;
    int oldCount = stack->count;
    int i = 0;

    // If the stack if full we need to do a
    // a transfer, resize, and retransfer, then push
    if(StackIsFull(stack))
    {
        // temp will be the same size as the old stack
        temp = (stackElementT*)(malloc((oldCount)*sizeof(stackElementT)));

        // Now we perform the transfer
        for(i = 0; i < oldCount; i++)
        {
            *(temp + i) = *((stack->elements) + i);
        }

        // Free the old memory
        free(stack->elements);
        stack->elements = NULL;

        // Recreate the stack with a 50% increase in size/capacity
        stack->elements = (stackElementT*)(malloc((3*oldCount/2)*sizeof(stackElementT)));
        // Re-establish the size
        stack->size = 3*oldCount/2;

        // Now we perform the transfer back
        for(i = 0; i < oldCount; i++)
        {
            *((stack->elements) + i) = *(temp + i);
        }

        // Free the temp array and 
        // remove dangling pointer
        free(temp);
        temp = NULL;

        // Now we push the element onto the stack
        *((stack->elements) + oldCount) = element;
        // Increase the count
        stack->count = oldCount + 1;
    }
    // If the stack isn't full
    else
    {
        *((stack->elements) + oldCount) = element;
        stack->count = oldCount + 1;
    }

}

// This function pops the top element from the stack and returns
// that value
stackElementT Pop(stackADT stack);


// This function frees the storage associated with the stack
void FreeStack(stackADT stack)
{
    // Start by freeing the elements on the stack
    // and remove dangling pointers
    free(stack->elements);
    stack->elements = NULL;

    // Finally free the stack
    free(stack);
    stack = NULL;
}

#endif

Obviously I'm not completely finished (needs a pop function). My concern is with the bottom function (FreeStack). I tested the code below as such:

#include <stdio.h>
#include <stdlib.h>
#include "Stack.h"
#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>

int main(void)
{
    stackADT stack;
    int i = 0;

    stack = NewStack();
    PrintStack(stack);

    for(i = 0; i < 60; i++)
    {
        Push(stack,i);
    }

    PrintStack(stack);
    FreeStack(stack);

    _CrtDumpMemoryLeaks();

    return 0;
}

The _CrtDumpMemoryLeaks() function is for Visual Studio and it indicates if there is a memory leak. Apparently I've sealed off any leaks when calling the FreeStack(stackADT stack) function. However, the stack pointer still holds a memory address, which is the issue because the FreeStack function is supposed to free the memory pointed to by the stack variable and set it equal to NULL. This occurs within the function, but when I return to the main function during debugging, I see the memory address still there. What is it I'm missing here? If I'm able to release the memory, why can't I remove the dangling pointer?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不一样的天空 2024-11-02 06:47:11

您可以通过值(而不是地址)将堆栈传递给函数,修改函数以接收(stackADT *),然后就可以开始了。

澄清:正如 Christian 所评论的,函数调用和堆栈的使用当然也必须改变(因为现在它是指向指针的指针......)

You pass the stack to the function by value, instead of by address, modify the function to receive (stackADT *) and you'll be good to go.

Clarification: as Christian commented, the function call, and the use of stack will have to be changed as well of course (since now it's a pointer to pointer...)

轮廓§ 2024-11-02 06:47:11

您在 pop 方法中按值传递 stackADT 对象(指针):

void FreeStack(stackADT stack)

因此 stack 指的是该指针的本地副本。当您设置该指针 = NULL 时,您只能在 FreeStack 内修改它。 main 方法有它自己的所述指针的副本,不指向 NULL。

You're passing that stackADT object (pointer) by value in your pop method:

void FreeStack(stackADT stack)

So stack refers to the local copy of that pointer. When you set that pointer = NULL, you modify it only within FreeStack. The main method has its own copy of said pointer, not pointing to NULL.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文