内存分配和释放他们。我们应该将它们设置为NULL? [英] Allocating memory and freeing them. Should we set them to NULL?
问题描述
海湾合作委员会(GCC)4.7.0
C89
您好,
我想知道如果我想在这里正确。当我使用malloc分配内存。 malloc的将在存储器中的指针返回到一尺寸。
所以我才分配我的记忆中所有的指针将有NULL值。
使用这个code片断:
结构地址* db_row = NULL;db_row =的malloc(sizeof的(结构地址));
db_row->名称=的malloc(sizeof的(字符)* 10);
db_row->电子邮件=的malloc(sizeof的(字符)* 10);免费(db_row->名);
免费(db_row->电子邮件);
免费(db_row);
我已分配内存之前在gdb调试器进行本作db_row:
(GDB)p db_row
$ 20 =(结构地址*)为0x0
(GDB)P * db_row
在地址0x0不能访问内存
这是正确的,因为没有记忆地址已分配。
之后,我分配内存,我得到下面当我这样做是相同的:
(GDB)p db_row
$ 25 =(结构地址*)0x602310
(GDB)P * db_row
$ 26 = {ID = 0时,设置为0,名称=为0x0,电子邮件=为0x0}
然而,当我有免费的记忆我还是分配任何内存之前得到相同的内存地址,岂不是NULL作为第一种情况?
释放内存后:
(GDB)p db_row
$ 28 =(结构地址*)0x602310
(GDB)P * db_row
$ 27 = {ID = 6300480,设置为0,名称= 0x602330,电子邮件= 0x602350#`}
正如你可以看到它仍指向同一个内存位置,这是正确的?
最后,我添加了这个结尾,看看我可以做一个双重释放:
如果(db_row!= NULL){
免费(db_row);
}如果(db_row!= NULL){
免费(db_row);
}
我得到的第二个呼叫堆栈转储释放。但作为一个安全的措施应你总是检查,以确保你是不是试图做一个双免费的吗?
这是值得设置后的指针为NULL,释放他们?
db_row = NULL;
非常感谢您的任何建议,
但作为一个安全措施,你应经常检查,以确保你是不是要释放一个NULL指针?
块引用>调用
免费()
与NULL
指针是安全的,没有什么会发生。调用免费()
不NULL
指针,你可以做到这一点明确。NULL
呼叫后,ING的指针免费()
将美元双自由上的相同p $ pvent 的指针变量:/ *假设'db_row指的是一个有效的地址
在此阶段下面code将不会导致
在双自由。* /
免费(db_row);
db_row = NULL;
免费(db_row);如果另一个指针变量指向同一个地址,并传递给
免费()
然后双免费仍会发生。仅仅因为一个指针不是 -
NULL
不保证它指向一个有效的地址。这是程序员的责任,以确保双免费()
■不要发生。通话后NULL
ING指针变量免费()
帮助,但不提供保证多重指针变量可以指向相同的地址。
但作为一个安全措施,你应经常检查,以确保你是不是试图做一个双免费的吗?
块引用>有没有办法查询指针变量,以确定是否在它持有已是
免费()
D地址的内存。指针变量拥有一个地址,仅此而已。gcc (GCC) 4.7.0 c89
Hello,
I am wondering if I am thinking correctly here. When I allocate memory using malloc. malloc will return a pointer to a size in memory.
So before I allocate my memory all pointers will have the value NULL.
Using this code snippet:
struct address *db_row = NULL; db_row = malloc(sizeof(struct address)); db_row->name = malloc(sizeof(char) * 10); db_row->email = malloc(sizeof(char) *10); free(db_row->name); free(db_row->email); free(db_row);
I have done this in the gdb debugger for db_row before allocating memory:
(gdb) p db_row $20 = (struct address *) 0x0 (gdb) p *db_row Cannot access memory at address 0x0
Which is correct, as no memory address has been allocated. After I allocate memory I get the following when I do the same:
(gdb) p db_row $25 = (struct address *) 0x602310 (gdb) p *db_row $26 = {id = 0, set = 0, name = 0x0, email = 0x0}
However, after I have free the memory I still get the same memory address, should it not be NULL as in the first case before allocating any memory?
After freeing the memory:
(gdb) p db_row $28 = (struct address *) 0x602310 (gdb) p *db_row $27 = {id = 6300480, set = 0, name = 0x602330 "", email = 0x602350 " #`"}
As you can see its still pointing to the same memory location, is this correct?
Finally, I added this at the end to see if I could do a double free:
if(db_row != NULL) { free(db_row); } if(db_row != NULL) { free(db_row); }
I get a stack dump on the second call to free. But as a safety measure should you always check to make sure you are not trying to do a double free?
It it worth setting the pointers to NULL after free them?
db_row = NULL;
Many thanks for any suggestions,
解决方案But as a safety measure should you always check to make sure you are not trying to free a NULL pointer?
Calling
free()
with aNULL
pointer is safe and nothing occurs. Callingfree()
does notNULL
the pointer, you may do this explicitly.NULL
ing a pointer after callingfree()
would prevent the double free on the same pointer variable:/* Assuming 'db_row' is referring to a valid address at this stage the following code will not result in a double free.*/ free(db_row); db_row = NULL; free(db_row);
If another pointer variable is pointing to the same address and is passed to
free()
then a double free still occurs.Just because a pointer is not-
NULL
does not guarantee that it points to a valid address. It is the programmer's responsibility to ensure doublefree()
s do not occur.NULL
ing pointer variables after a call tofree()
helps but does not provide a guarantee as multiple pointer variables can be pointing at the same address.
But as a safety measure should you always check to make sure you are not trying to do a double free?
There is no way to query a pointer variable to determine if the memory at the address that it holds has already been
free()
d. A pointer variable holds an address, nothing more.这篇关于内存分配和释放他们。我们应该将它们设置为NULL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!