库函数返回值的问题?
有些函数,比如:inet_ntoi(...),返回值是char*,它是动态开辟的空间吗?我在用完以后,应不应该释放这个空间?怎么释放?
[解决办法]
系统函数返回的指针有可能是指向在堆上分配的内存的,具体要自己查阅相关文档了解。。。
The second class of memory leaks is referred to as the hidden allocation problem. It is characterized by a call to another function to deliver an object without realizing that the object has been created on the heap. As an example, consider the standard function strdup(). It duplicates a string and returns a pointer to it. The duplicated string is created on the heap. If not deallocated later, the memory leaks. However, shouldn 't all C/C++ programmers know what a standard function such as strdup() does? Of course, but do they all know everything that standard functions do? And how about their knowledge of the particular operating system being used? Very often we need the services of external procedures, as in particular system calls. For instance, consider the UNIX system call
struct group *getgrnam(const char *name)
Is the structure group allocated on the heap or somewhere else (say, in the kernel of the operating system)? Should it be deallocated by the user when no longer needed? Or consider the following Windows example:
HBRUSH br;
...
br = CreateSolidBrush(RGB(255,255,255));
Here br is a handle to a brush. We created a solid brush of certain color and captured the reference to it in br. Is the brush an object on the heap? Do we need to deallocate it? A diligent and thorough programmer would find answers to these questions, but often the questions are simply not asked. It 's almost as if the programmer is behaving according to the principle "I am not responsible for what I have not done explicitly ". The consequences of implicit actions are too often ignored.
---------------------------------------------------
so, "A diligent and thorough programmer would find answers to these questions ", and just find it yourself.
[解决办法]
释放的话,就确定不再会使用该返回值的指针的时候,就可以了阿
不过不要忘记赋予 NULL不要让其成为野指针 (老生常谈了。。)
[解决办法]
至于如何确定是不是申请的堆内存, 还需要查看源码 或者相关的文档。
[解决办法]
说明文档会告诉你的.
[解决办法]
不同的库有不同的内存管理任务配置,看文档肯定会提及的~
[解决办法]
inet_ntoi(...)之类的是不须要你来释放的.
其它的有些是要你释放的.
[解决办法]
查阅你调用库的接口说明
返回值一般会提起使用的方法
需要你释放还是自动释放