释放指针和将指针赋值为NULL有什么区别?

时间:2022-04-03 02:52:27

Could somebody tell me the difference between:

谁能告诉我两者的区别吗?

int *p;
p=(int*)malloc(10*sizeof(int));
free(p);

or

int *p;
p=(int*)malloc(10*sizeof(int));
p=NULL;

8 个解决方案

#1


39  

free will deallocate the memory that p points to - simply assigning it to NULL will not (and thus you will have a memory leak).

free将释放p点的内存——简单地将其赋值为NULL将不会(这样您就会有内存泄漏)。

It is worth mentioning that it is good practice to assign your pointer to NULL AFTER the call to free, as this will prevent you from accidentally trying to access the freed memory (which is still possible, but absolutely should not be done).

值得一提的是,在调用free之后将指针赋值为NULL是一种很好的实践,因为这将防止您意外地试图访问已释放的内存(这仍然是可能的,但绝对不应该这样做)。

#2


9  

There is no garbage collection in C, so if you don't explicitly free a chunk of memory, it will never get freed, even if there are no references to it. Maybe you come from a background in garbage collected languages, so it might be hard to change your way of thought, but it's always important to remember to "manually" free all resources in low-level languages like C.

C中没有垃圾收集,所以如果不显式释放内存块,那么它将永远不会被释放,即使没有对它的引用。也许您来自垃圾收集语言的背景,所以可能很难改变您的思维方式,但是一定要记住以C这样的低级语言“手工”释放所有资源。

Hope this helps Cheers

希望这有助于干杯

#3


7  

p is pointer (to a block allocated dynamically in memory ["on the Heap"])
This means that p is a variable which contains the address in memory of a particular block (or some particular size, in the example a block big enough to hold 10 integers).

p是指针(指向内存中动态分配的块["on the Heap"]),这意味着p是一个变量,它包含一个特定块内存中的地址(或某个特定大小,在本例中,一个足够容纳10个整数的块)。

free(p); instructs the memory management logic (of the C Runtime) that the memory previously occupied by the block which p points to can be reused.

*(p);指示(C运行时的)内存管理逻辑,即p指向的块所占用的内存可以被重用。

p = NULL; sets the value of p to NULL (the address it formerly contained is lost) but the block in memory it pointed too is still considered in use.

p =零;将p的值设置为NULL(它以前包含的地址丢失了),但是它所指向的内存块仍然在使用中。

There can be some confusion because in languages such as Java, C#, Python etc. merely assigning the variable to NULL (or to another address for that matter), will automatically free the underlying memory (assuming no other references to this address exist in other live variables).

可能会出现一些混乱,因为在Java、c#、Python等语言中,仅仅将变量赋值为NULL(或另一个地址),就会自动释放底层内存(假设在其他活动变量中不存在此地址的其他引用)。

This is not the case in C or C++, leading to errors like the following:

在C或c++中,情况并非如此,导致如下错误:

  free(p);
  // possibly some some code etc.
  // later:
  p[6] = 'a';  // <<---  Ouch we're modifying whatever new variable is there !!!

or

  // didn't call free(p)
  p = NULL;
  // now the memory allocated for p is held in memory even though it
  // is not going to be used (assuming no copies of p or of pointers to part
  // of that block were made.

The latter case is only wasteful of resources, the former can lead to hard to find bugs.

后一种情况只会浪费资源,前者会导致难以找到bug。

That's why a typical C idiom is:

这就是为什么典型的C习语是:

   free(p);
   p = NULL;

#4


2  

Not calling free and directly assigning to NULL will lead to a memory leak as explained by others. You can refer to this link to get clear details about memory leaks and other memory related problems.

不调用free和直接赋值为NULL将导致内存泄漏,这是其他人的解释。您可以参考这个链接来获得关于内存泄漏和其他内存相关问题的详细信息。

#5


1  

Freeing the allocated memory deallocates it and allows that memory to be used elsewhere while the pointer to where the memory was allocated is preserved.

释放已分配的内存分配,并允许在其他地方使用内存,而将内存分配的指针保存在内存中。

Setting a pointer to the allocated memory to NULL does not deallocate it.

将指向已分配内存的指针设置为NULL不会释放它。

If you are using an implementation that uses a heap-based malloc, debug something that allocates, uses and frees memory, and do the same to something that allocates, uses, and sets the pointer to the memory to NULL.

如果您正在使用一个使用基于堆的malloc的实现,调试分配、使用和释放内存的东西,并对分配、使用和设置内存指针为NULL的东西执行相同的操作。

Memory management is implementation-dependent (see http://en.wikipedia.org/wiki/Malloc#Implementations).

内存管理依赖于实现(参见http://en.wikipedia.org/wiki/Malloc#实现)。

#6


1  

1.

1。

int *p; p= (int * ) malloc(10*sizeof(int)); free(p);

int * p;p= (int *) malloc(10*sizeof(int));*(p);

the memory is released back to the heap. but the pointer still pointing to the freed memory location. Thus if its further used , would lead to a memory corruption.

内存被释放回堆。但是指针仍然指向释放的内存位置。因此,如果进一步使用它,将导致内存损坏。

thus correct thing to do is resetting the pointer to NULL explicitly to avoid further usage of that pointer .

因此,正确的做法是显式地将指针重置为NULL,以避免进一步使用该指针。

its not advisable to type cast the return pointer in C

不建议在C中键入cast返回指针

2.

2。

int *p; p=(int * )malloc(10*sizeof(int)); p=NULL;

int * p;p =(int *)malloc(10 * sizeof(int));p =零;

This would result in a memory leak : as the allocated memory is not freed here. you are just reset the pointer to NULL.

这将导致内存泄漏:因为分配的内存不在这里释放。你只是将指针重置为空。

#7


1  

The better way is to first free the memory and then set it to NULL:

更好的方法是先释放内存,然后将其设置为NULL:

free(p);
p = NULL;

#8


0  

p=NULL does not deallocate the memory. If you don't need to use the memory pointed by p anymore you should use free() otherwise there will be memory leak. You should also set p=NULL after calling free() to avoid erroneously accessing that memory again in the future.

p=NULL不释放内存。如果您不再需要使用p指向的内存,那么应该使用free(),否则会出现内存泄漏。您还应该在调用free()之后设置p=NULL,以避免将来再次错误地访问该内存。

#1


39  

free will deallocate the memory that p points to - simply assigning it to NULL will not (and thus you will have a memory leak).

free将释放p点的内存——简单地将其赋值为NULL将不会(这样您就会有内存泄漏)。

It is worth mentioning that it is good practice to assign your pointer to NULL AFTER the call to free, as this will prevent you from accidentally trying to access the freed memory (which is still possible, but absolutely should not be done).

值得一提的是,在调用free之后将指针赋值为NULL是一种很好的实践,因为这将防止您意外地试图访问已释放的内存(这仍然是可能的,但绝对不应该这样做)。

#2


9  

There is no garbage collection in C, so if you don't explicitly free a chunk of memory, it will never get freed, even if there are no references to it. Maybe you come from a background in garbage collected languages, so it might be hard to change your way of thought, but it's always important to remember to "manually" free all resources in low-level languages like C.

C中没有垃圾收集,所以如果不显式释放内存块,那么它将永远不会被释放,即使没有对它的引用。也许您来自垃圾收集语言的背景,所以可能很难改变您的思维方式,但是一定要记住以C这样的低级语言“手工”释放所有资源。

Hope this helps Cheers

希望这有助于干杯

#3


7  

p is pointer (to a block allocated dynamically in memory ["on the Heap"])
This means that p is a variable which contains the address in memory of a particular block (or some particular size, in the example a block big enough to hold 10 integers).

p是指针(指向内存中动态分配的块["on the Heap"]),这意味着p是一个变量,它包含一个特定块内存中的地址(或某个特定大小,在本例中,一个足够容纳10个整数的块)。

free(p); instructs the memory management logic (of the C Runtime) that the memory previously occupied by the block which p points to can be reused.

*(p);指示(C运行时的)内存管理逻辑,即p指向的块所占用的内存可以被重用。

p = NULL; sets the value of p to NULL (the address it formerly contained is lost) but the block in memory it pointed too is still considered in use.

p =零;将p的值设置为NULL(它以前包含的地址丢失了),但是它所指向的内存块仍然在使用中。

There can be some confusion because in languages such as Java, C#, Python etc. merely assigning the variable to NULL (or to another address for that matter), will automatically free the underlying memory (assuming no other references to this address exist in other live variables).

可能会出现一些混乱,因为在Java、c#、Python等语言中,仅仅将变量赋值为NULL(或另一个地址),就会自动释放底层内存(假设在其他活动变量中不存在此地址的其他引用)。

This is not the case in C or C++, leading to errors like the following:

在C或c++中,情况并非如此,导致如下错误:

  free(p);
  // possibly some some code etc.
  // later:
  p[6] = 'a';  // <<---  Ouch we're modifying whatever new variable is there !!!

or

  // didn't call free(p)
  p = NULL;
  // now the memory allocated for p is held in memory even though it
  // is not going to be used (assuming no copies of p or of pointers to part
  // of that block were made.

The latter case is only wasteful of resources, the former can lead to hard to find bugs.

后一种情况只会浪费资源,前者会导致难以找到bug。

That's why a typical C idiom is:

这就是为什么典型的C习语是:

   free(p);
   p = NULL;

#4


2  

Not calling free and directly assigning to NULL will lead to a memory leak as explained by others. You can refer to this link to get clear details about memory leaks and other memory related problems.

不调用free和直接赋值为NULL将导致内存泄漏,这是其他人的解释。您可以参考这个链接来获得关于内存泄漏和其他内存相关问题的详细信息。

#5


1  

Freeing the allocated memory deallocates it and allows that memory to be used elsewhere while the pointer to where the memory was allocated is preserved.

释放已分配的内存分配,并允许在其他地方使用内存,而将内存分配的指针保存在内存中。

Setting a pointer to the allocated memory to NULL does not deallocate it.

将指向已分配内存的指针设置为NULL不会释放它。

If you are using an implementation that uses a heap-based malloc, debug something that allocates, uses and frees memory, and do the same to something that allocates, uses, and sets the pointer to the memory to NULL.

如果您正在使用一个使用基于堆的malloc的实现,调试分配、使用和释放内存的东西,并对分配、使用和设置内存指针为NULL的东西执行相同的操作。

Memory management is implementation-dependent (see http://en.wikipedia.org/wiki/Malloc#Implementations).

内存管理依赖于实现(参见http://en.wikipedia.org/wiki/Malloc#实现)。

#6


1  

1.

1。

int *p; p= (int * ) malloc(10*sizeof(int)); free(p);

int * p;p= (int *) malloc(10*sizeof(int));*(p);

the memory is released back to the heap. but the pointer still pointing to the freed memory location. Thus if its further used , would lead to a memory corruption.

内存被释放回堆。但是指针仍然指向释放的内存位置。因此,如果进一步使用它,将导致内存损坏。

thus correct thing to do is resetting the pointer to NULL explicitly to avoid further usage of that pointer .

因此,正确的做法是显式地将指针重置为NULL,以避免进一步使用该指针。

its not advisable to type cast the return pointer in C

不建议在C中键入cast返回指针

2.

2。

int *p; p=(int * )malloc(10*sizeof(int)); p=NULL;

int * p;p =(int *)malloc(10 * sizeof(int));p =零;

This would result in a memory leak : as the allocated memory is not freed here. you are just reset the pointer to NULL.

这将导致内存泄漏:因为分配的内存不在这里释放。你只是将指针重置为空。

#7


1  

The better way is to first free the memory and then set it to NULL:

更好的方法是先释放内存,然后将其设置为NULL:

free(p);
p = NULL;

#8


0  

p=NULL does not deallocate the memory. If you don't need to use the memory pointed by p anymore you should use free() otherwise there will be memory leak. You should also set p=NULL after calling free() to avoid erroneously accessing that memory again in the future.

p=NULL不释放内存。如果您不再需要使用p指向的内存,那么应该使用free(),否则会出现内存泄漏。您还应该在调用free()之后设置p=NULL,以避免将来再次错误地访问该内存。