<i id='SRSQ2'><tr id='SRSQ2'><dt id='SRSQ2'><q id='SRSQ2'><span id='SRSQ2'><b id='SRSQ2'><form id='SRSQ2'><ins id='SRSQ2'></ins><ul id='SRSQ2'></ul><sub id='SRSQ2'></sub></form><legend id='SRSQ2'></legend><bdo id='SRSQ2'><pre id='SRSQ2'><center id='SRSQ2'></center></pre></bdo></b><th id='SRSQ2'></th></span></q></dt></tr></i><div id='SRSQ2'><tfoot id='SRSQ2'></tfoot><dl id='SRSQ2'><fieldset id='SRSQ2'></fieldset></dl></div>
  • <tfoot id='SRSQ2'></tfoot>
  • <small id='SRSQ2'></small><noframes id='SRSQ2'>

      <legend id='SRSQ2'><style id='SRSQ2'><dir id='SRSQ2'><q id='SRSQ2'></q></dir></style></legend>
        <bdo id='SRSQ2'></bdo><ul id='SRSQ2'></ul>
      1. 为什么 free(p) 不将 p 设置为 NULL?

        Why doesn#39;t free(p) set p to NULL?(为什么 free(p) 不将 p 设置为 NULL?)
          <tbody id='HWmTc'></tbody>
      2. <legend id='HWmTc'><style id='HWmTc'><dir id='HWmTc'><q id='HWmTc'></q></dir></style></legend>

            <tfoot id='HWmTc'></tfoot>

              <small id='HWmTc'></small><noframes id='HWmTc'>

                  <bdo id='HWmTc'></bdo><ul id='HWmTc'></ul>
                  <i id='HWmTc'><tr id='HWmTc'><dt id='HWmTc'><q id='HWmTc'><span id='HWmTc'><b id='HWmTc'><form id='HWmTc'><ins id='HWmTc'></ins><ul id='HWmTc'></ul><sub id='HWmTc'></sub></form><legend id='HWmTc'></legend><bdo id='HWmTc'><pre id='HWmTc'><center id='HWmTc'></center></pre></bdo></b><th id='HWmTc'></th></span></q></dt></tr></i><div id='HWmTc'><tfoot id='HWmTc'></tfoot><dl id='HWmTc'><fieldset id='HWmTc'></fieldset></dl></div>
                  本文介绍了为什么 free(p) 不将 p 设置为 NULL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  为什么这不能成为 free() 的标准行为?

                  Any reasons why this can not be standard behavior of free()?

                  多个指针指向同一个对象:

                  multiple pointers pointing to the same object:

                  #include <stdlib.h>
                  #include <stdio.h>
                  
                  void safefree(void*& p)
                  {
                      free(p); p = NULL;
                  }
                  
                  int main()
                  {
                      int *p = (int *)malloc(sizeof(int));
                      *p = 1234;
                      int*& p2 = p;
                      printf("p=%p p2=%p
                  ", p, p2);
                      safefree((void*&)p2);
                      printf("p=%p p2=%p
                  ", p, p2);
                      safefree((void*&)p); // safe
                  
                      return 0;
                  }
                  

                  来自 malloc 的赋值要求来自 void*

                  assignment from malloc demands cast from void*

                  反之亦然:

                  safefree() 要求强制转换为 void*&(参考)

                  safefree() demands cast to void*& (reference)

                  推荐答案

                  如果是这样,你就必须传递一个指向函数指针的指针:

                  If it did, you would have to pass a pointer to a pointer to the function:

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

                  我相信很多人都会弄错.

                  which I'm sure many people would get wrong.

                  这篇关于为什么 free(p) 不将 p 设置为 NULL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Constructor initialization Vs assignment(构造函数初始化 Vs 赋值)
                  Is a `=default` move constructor equivalent to a member-wise move constructor?(`=default` 移动构造函数是否等同于成员移动构造函数?)
                  Has the new C++11 member initialization feature at declaration made initialization lists obsolete?(声明时新的 C++11 成员初始化功能是否使初始化列表过时了?)
                  Order of constructor call in virtual inheritance(虚继承中构造函数调用的顺序)
                  How to use sfinae for selecting constructors?(如何使用 sfinae 选择构造函数?)
                  Initializing a union with a non-trivial constructor(使用非平凡的构造函数初始化联合)

                        <small id='CwP4L'></small><noframes id='CwP4L'>

                      1. <legend id='CwP4L'><style id='CwP4L'><dir id='CwP4L'><q id='CwP4L'></q></dir></style></legend>

                        1. <tfoot id='CwP4L'></tfoot>
                            <bdo id='CwP4L'></bdo><ul id='CwP4L'></ul>
                          • <i id='CwP4L'><tr id='CwP4L'><dt id='CwP4L'><q id='CwP4L'><span id='CwP4L'><b id='CwP4L'><form id='CwP4L'><ins id='CwP4L'></ins><ul id='CwP4L'></ul><sub id='CwP4L'></sub></form><legend id='CwP4L'></legend><bdo id='CwP4L'><pre id='CwP4L'><center id='CwP4L'></center></pre></bdo></b><th id='CwP4L'></th></span></q></dt></tr></i><div id='CwP4L'><tfoot id='CwP4L'></tfoot><dl id='CwP4L'><fieldset id='CwP4L'></fieldset></dl></div>
                              <tbody id='CwP4L'></tbody>