它是通过一个指针到const的一个很好的做法来释放内存 [英] Is it a good practice to free memory via a pointer-to-const

查看:94
本文介绍了它是通过一个指针到const的一个很好的做法来释放内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有很多问题在讨论C和C ++的细节处理指针到const的缺失,即免费()不接受他们,删除删除[] 做和常量性不prevent对象的破坏。

There are many questions discussing the details of C and C++ dealing with pointer-to-const deletion, namely that free() does not accept them and that delete and delete[] do and that constness doesn't prevent object destruction.

我感兴趣的是你是否认为这是一个很好的做法,这样做,不是什么语言(C和C ++)允许。

What I am interested on is whether you think it is a good practice to do so, not what the languages (C and C++) allow.

有关指针到const的缺失参数包括:

Arguments for pointer-to-const deletion include:


  • Linus Torvalds公司 kfree(),与C的免费(),需要无效常量* 的说法,因为他认为释放内存,不影响什么指向。

  • 免费()引入const关键字之前的设计。

  • C ++的delete运算符允许常量数据删除。

  • Linus Torvalds' kfree(), unlike C's free(), takes a void const* argument because he thinks that freeing the memory does not affect what is pointed to.
  • free() was designed before the introduction of the const keyword.
  • C++'s delete operators allow deletion of const data.

反对意见包括:


  • 程序员不希望被修改的数据(或删除),当他们将一个指针传递给const吧。

  • 许多人认为,指针给const意味着没有得到数据的所有权(但不是非const意味着获得所有权)。

  • 这是大多数图书馆和现有code中的常见做法。

请参数以及在您的答复,可能是指主管部门。我的目的不是要在这里展开的一项民意调查。

Please argument well in your responses and possibly refer to authorities. My intention is not to start a poll here.

推荐答案

那么,这里的一些相关的东西可能太长,无法为注释:

Well, here's some relevant stuff possibly too long to fit into a comment:


  1. 前段时间的做法,通过一个指针到const的无记忆被禁止平原,看到的 这个博士。道博的文章中,语言法(:))部分

有曾两次在 HTTP相关讨论://组。.google.ru ​​/组/ comp.lang.c ++主持:<一href=\"http://groups.google.ru/group/comp.lang.c++.moderated/browse_thread/thread/dabb77ab83597f0c/4f192c89454939b9?pli=1\"相对=nofollow> 删除一个const指针?的,<一个href=\"http://groups.google.ru/group/comp.lang.c++.moderated/browse_thread/thread/709ee6082ec64e4f/3c4c6e78ebcb3c2b\"相对=nofollow> 运营商为什么可以删除一个const指针调用的(实际上两者处理有问题的情况下,即指针常数)。

There has twice been a relevant discussion on http://groups.google.ru/group/comp.lang.c++.moderated: "Delete a const pointer?", "Why can operator delete be called on a const pointer" (both actually deal with the case in question, i.e. pointer to const).

我自己的点(因为你所要求的参数):在任何给定的情况下问题的操作可能是由一个类或函数,而不是的(在文件中或明或暗地定义的)合同中规定仅通过方法签名或者参数类型。

My own point (since you are asking for arguments): possibility of the operation in question in any given context is defined by the (explicitly or implicitly defined in the documentation) contract of a class or a function, not by just the method signature or parameter types.

这篇关于它是通过一个指针到const的一个很好的做法来释放内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆