如何检查指针是否已被释放

时间:2014-06-20 03:55:44

标签: c pointers malloc free

我是C的初学者。下面是我的场景 - 我在main函数中创建了一个指针变量,它已经传递给了几个函数(在这个例子中是2个级别)。其中一个功能可以释放它。现在我需要检查Main以查看指针是否被释放,这意味着我需要在main()中设置& str的值以指向NULL。不确定我的做法是否正确。任何帮助将不胜感激

void func2(char *str)   
{
    free(str); 
}

void func1(char *str)
{
   func2(str); 
}

int main()
{ 
   char *str;
   str=(char *) malloc(10);
   func1(str);
   if(str){ do something; }  // if condition to check whether str is freed 
}

5 个答案:

答案 0 :(得分:1)

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

func2(char **str)
{
        free(*str);                //free
        *str = NULL;               //Set to NULL
}

func1(char **str)                   //func1 receives as **
{
        func2(str);                //Pass pointer to func2()
}

int main()
{
        char *str = NULL;
        str=(char *) malloc(10);
        func1(&str);                //Pass Address of pointer to func1()

        if(str)                     //Check for NULL
        {
                printf("\n Not - Freed...\n");
        }
        else
        {
                printf("\n Freed...\n");
        }                                             
        return 0;
}

在C中,所有都是按值传递。我建议学习http://www.cs.fsu.edu/~myers/cgs4406/notes/pointers.html来理解这一点。

答案 1 :(得分:0)

void func1(char** str) {
    free(*str);
    *str = NULL;
}

void func2(char** str) {
    free(*str);
    *str = NULL;
}

int main() {
    char *str;

    str = (char*) malloc(10);
    func1(&str);
    if (str) {
        do something;
    }
}

答案 2 :(得分:0)

void func2(char **str)   
{
    free(*str); 
    *str = 0;
}

void func1(char **str)
{
   func2(str); 
}

int main()
{ 
   char *str;
   // I'd recommend using sizeof(type_you_want) * amount_of_elements instead of
   // a constant number: ->  malloc(sizeof(char) * 10);

   str=(char *) malloc(10);

   func1(&str); // You must pass the address of the pointer, because you want
               // to change "WHAT IT POINTS TO", not "WHAT IS POINTED BY IT"

   if(str){ do something; }  // if condition to check whether str is freed 
}

当您在C中调用函数时,您传递了这些参数的副本,因此您传递了该指针的副本(该副本仍指向同一位置,因此您可以更改它所指向的位置)但是你想改变指针值,所以你需要传递它的地址。

我已经解释了here

中如何使用函数内部的指针

答案 3 :(得分:0)

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

void func2(char **str)
{
    printf("%d %s\n",__LINE__,__func__);
    free(*str);
    *str = NULL;
}

void func1(char **str)
{
    printf("%d %s\n",__LINE__,__func__);
    func2(str);
}

char * allocaMem(char **ptr)
{
    *ptr=(char *) malloc(sizeof(char)* 10);
    if(!*ptr)
    {
        perror("");
    }
    else
    {
        return *ptr;
    }
}

int main()
{
    char *str = allocaMem(&str);
    if (!str) {
        printf("Error in malloc()\n");
        return -1;
    }

    func1(&str);

    if (str) {
        printf("Memory Not freed\n");
    } else {
        printf("Memory freed\n");
    }
}

答案 4 :(得分:0)

您可以尝试这样的方法 - 首先重新定义malloc和free(track.h)

#ifndef track_h
#define track_h
extern void* trackmalloc(size_t size);
extern void trackfree(void* array);
extern void trackismalloc(void* array);
#define malloc trackmalloc
#define free trackfree

#endif

然后对于使用malloc和free的每段代码,将#include替换为#include&#34; track.h&#34;

#include <stdlib.h>
#include <stdio.h>
#include "track.h" /* was <malloc.h> */

// A function which has a 20% chance of freeing the pointer
void twentypercent(char* array)
{
    if (rand() < (RAND_MAX / 5))
        free(array);
}


int main(int argc, char* argv[])
{
    char* list = malloc(256);
    int ii;

    for (ii = 0; ii < 10; ++ii)
        twentypercent(list);

    if (trackismalloc(list)
        printf("Not freed yet");

    return 0;
}

现在定义track.c。这将只释放由trackmalloc分配的内存。如果它没有被trackmalloc分配,那么它将报告内存已被释放。

#include <stdio.h>
#include <malloc.h>
#define TRACKER_MAX 2048
static void* tracker[TRACKER_MAX] = { 0 };
static int track_last = -1;
void* trackmalloc(size_t size)
{
    // For simplicity, tracker will not be reused
    tracker[++track_last] = malloc(size);
    return tracker[track_last];
}

void trackfree(void* array)
{
    // This will slow down as the list gets filled up.
    // You will need a more efficient way of searching lists (possibly bsearch)
    int tt;
    for (tt = 0; tt < track_last; ++tt)
    {
        if (array == tracker[tt])
        {
            free(tracker[tt]);
            tracker[tt] = 0;
            break;
        }
    }

    if (tt == track_last)
        printf("%p already freed\n", array);
}

int trackismalloc(void* array)
{
    // This will slow down as the list gets filled up.
    // You will need a more efficient way of searching lists (possibly bsearch)
    int tt, result = 0;
    for (tt = 0; tt < track_last; ++tt)
    {
        if (array == tracker[tt])
        {
            result = 1;
            break;
        }
    }
    return result;
}