<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?

        时间:2023-09-26
          <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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:CString 到 char* 下一篇:Valgrind 在给字符串赋值时报告内存泄漏

                  相关文章

                    <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>
                    2. <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>