无法使用C中的free函数释放指针 [英] Pointer is not freed using free function in C

查看:88
本文介绍了无法使用C中的free函数释放指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


请建议我一种确认指针是否空闲的方法.
以下是我尝试修复的示例代码段.请帮忙

Hi ,
Please suggest me a way to confirm if a pointer is free or not.
Below is the sample code snippet i am trying to fix. Please help

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

int main()
{
        char cArr[] = "Hi Thomas";
        char * ptr;
        ptr = ( char * ) malloc( sizeof( char ) * strlen( cArr ) + 1 );
        if( ptr == NULL )
                printf("Ptr is not usable\n");
        else
                printf("Ptr is usable\n");
        strcpy( ptr, cArr );
        printf("Pointer contains: %s\n", ptr );
        free( ptr );
        if( ptr == NULL )
                printf("Ptr is freed now\n");
        else
                printf("Ptr is not freed\n");
        return 0;
}



执行以上代码的结果如下
------------------------------------------



Result on executing the above code is as below
------------------------------------------

Ptr is usable
Pointer contains: Hi Thomas
Ptr is not freed

推荐答案

否,free释放了ptr所引用的内存块,不是它使指针本身无效.该块将不再可访问-仍然不会导致问题-但仅使用free不会影响指针的内容;那只是一个数字.如果您这样做,该怎么办:

No, the free releases the memory block that ptr refers to, not that it nullifies the pointer itself. The block is no longer accessible - not without causing problems, anyway - but just using free does not affect the content of the pointer; that is just a number. What if you had done this:

ptr = ( char * ) malloc( sizeof( char ) * strlen( cArr ) + 1 );
otherptr = ptr;
free( ptr );

如果free清除了对内存块的引用,您是否还希望otherptr也受到影响?

If free cleared the reference to the memory block, would you expect otherptr to be affected as well?


您对free的期望有误. free函数并非旨在设置其参数(在您的情况下为ptr),因为您可能会从其签名推断出(函数参数始终在C中通过值传递).
常见的成语是:
You have wrong expectations on free.
the free function is NOT designed to set its argument (ptr in your case) value, as you may deduce from its signature (function arguments are always passed by value in C).
The common idiom is:
if (ptr) 
{// memory was allocated
  free(ptr); // release memory
  ptr = NULL;// invalidate the pointer
}


在执行ptr操作时,释放内存后它将变成悬空指针

悬空指针:-悬空指针是在删除或释放对象时发生的,而没有修改指针的值,因此指针仍指向已释放内存的存储位置.

因此,执行此类程序或防止悬空指针或停止崩溃程序的最佳方法是,在释放该指针之后,必须将其初始化为NULL.

as you are doing for ptr it will become a dangling pointer after freeing your memory

Dangling Pointer :- dangling pointers arise when an object is deleted or deallocated, without modifying the value of the pointer, so that the pointer still points to the memory location of the deallocated memory.

so best way to do this kind of program,or to preventing the dangling pointer or to stop crashing the program ,after freeing this pointer you have to initialize as NULL.

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

int main()
{
        char cArr[] = "Hi Thomas";
        char * ptr = NULL;
        ptr = ( char * ) malloc( sizeof( char ) * strlen( cArr ) + 1 );
        if( ptr == NULL )
                printf("Ptr is not usable\n");
        else
                printf("Ptr is usable\n");
        strcpy( ptr, cArr );
        printf("Pointer contains: %s\n", ptr );
        free( ptr );
        ptr = NULL;
        if( ptr == NULL )
                printf("Ptr is freed now\n");
        else
                printf("Ptr is not freed\n");
        return 0;
}


这篇关于无法使用C中的free函数释放指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆