首页 新闻 论坛 群组 Blog 文档 下载 读书 Tag 网摘 搜索 .NET Java 游戏 视频 人才 外包 培训 数据库 书店 程序员
中国软件网
欢迎您:游客 | 登录 注册 帮助
  • delete是如何知道要释放多少内存的? [已结贴,结贴人:kingstarer]
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-12 21:28:56 楼主
    int *p = new int[N];
    delete []p;

    为何这样的代码就能正确的释放p ? 并没有把N的值传给delete啊
    20  修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-12 21:30:151楼 得分:0
    是不是对于每个通过new分配的指针都会有记录下它所指的内存的大小? 如是,我们可不可以自己获得呢?
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-12 21:31:352楼 得分:0
    编译器有办法知道
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-12 21:32:153楼 得分:0
    new当然知道自己多大了,要不哪里来的内存越界啊
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • dbger
    • 等级:
    发表于:2008-05-12 21:34:334楼 得分:1
    是编译器在编译过程中做了处理,貌似程序没法获得。
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • mscf
    • 等级:
    发表于:2008-05-12 21:36:035楼 得分:1
    你的猜测是对的,不过如果数组中的元素是语言带的基本类型,delete和delete[]是一样的效果,如楼主举的例子
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-12 21:42:186楼 得分:0
    MSDN:
      HOW DELETE WORKS:
    The delete operator invokes the function operator delete.

    For objects not of class type (class, struct, or union), the global delete operator is invoked. For objects of class type, the name of the deallocation function is resolved in global scope if the delete expression begins with the unary scope resolution operator (::). Otherwise, the delete operator invokes the destructor for an object prior to deallocating memory (if the pointer is not null). The delete operator can be defined on a per-class basis; if there is no such definition for a given class, the global operator delete is invoked. If the delete expression is used to deallocate a class object whose static type has a virtual destructor, the deallocation function is resolved through the virtual destructor of the dynamic type of the object.

    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-12 21:44:037楼 得分:1
    MSDN:
      HOW DELETE WORKS:
    The delete operator invokes the function operator delete.

      For objects not of class type (class, struct, or union), the global delete operator is invoked.
      For objects of class type, the name of the deallocation function is resolved in global scope
    if the delete expression begins with the unary scope resolution operator (::).
      Otherwise, the delete operator invokes the destructor for an object prior to deallocating memory (if the pointer is not null).
      The delete operator can be defined on a per-class basis;
      if there is no such definition for a given class, the global operator delete is invoked.
      If the delete expression is used to deallocate a class object whose static type has a virtual destructor,
    the deallocation function is resolved through the virtual destructor of the dynamic type of the object.


    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-12 21:45:238楼 得分:0
    哎看不大懂...
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-12 21:49:219楼 得分:1
    你自己重载一下operator new[](size_t)就知道了,
    编译器传给这个操作符的size_t总会比需要的大4个字节,自己看看干什么用的吧。呵呵。
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-12 21:53:0710楼 得分:1
    PS:重载new[]的时候,用HeapAlloc分配内存,然后注意一下HeapAlloc返回的地址和new[]返回给主调程序的地址有什么差别
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-12 22:24:2611楼 得分:0
    编译器知道的
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-12 22:31:3912楼 得分:0
    引用 9 楼 two_ears 的回复:
    你自己重载一下operator new[](size_t)就知道了,
    编译器传给这个操作符的size_t总会比需要的大4个字节,自己看看干什么用的吧。呵呵。

    是不是说做多分配2^16(2^17?)的内存?
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-13 09:00:4213楼 得分:0
    踩点
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-13 11:43:4714楼 得分:5
    http://topic.csdn.net/t/20060425/18/4713073.html
    这个是以前的贴  也是讨论这个的
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-13 11:45:4715楼 得分:1
    http://www.sunistudio.com/cppfaq/freestore-mgmt.html
    csdn的faq上也有
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-13 11:49:4116楼 得分:3
    new分配完后,会有一个结构记录分配的信息

    包括长度等等这些信息,所以删除的时候会知道

    这些编译器都帮你做好了!
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-13 16:05:5217楼 得分:0
    操作系统知道,有记录的.
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • bubu8633
    • 等级:
    发表于:2008-05-13 16:16:4118楼 得分:1
    size_t  _msize_dbg(  void  *userData,  int  blockType  );   
      size_t  _msize(  void  *memblock  );   
       
      注意DEBUG版本应该用_msize_dbg.   
    这个也能知道
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-13 21:33:1719楼 得分:0
    谢谢回答,我在faq上面看到:

    [16.13] p = new Fred[n]之后,编译器在delete[] p的时候如何知道有个对象被析构?
    [Recently changed "popluar" to "popular" thanks to Fabrice Clerc (on 7/00). Click here to go to the next FAQ in the "chain" of recent changes.]
    精简的回答:魔法。

    详细的回答:运行时系统将对象的数量 n 保存在某个通过指针 p 可以获取的地方。有两种普遍的技术来实现。这些技术都在商业编译器中使用,各有权衡,都不完美。这些技术是:

    超额分配数组并将 n 放在第一个Fred对象的左边。
    使用关联数组, p 作为键, n 作为值。
    -------------------------------------------
    我曾经试过在VC里把分配的指针前面的地址所存的整数打印出来,但发现并不是数组的长度
    我想VC里面用的应该关联数组吧?
    那请问我要从哪获得这个数组呢?
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-13 22:10:0420楼 得分:0
    引用 12 楼 lbc122 的回复:
    引用 9 楼 two_ears 的回复:
    你自己重载一下operator new[](size_t)就知道了, 
    编译器传给这个操作符的size_t总会比需要的大4个字节,自己看看干什么用的吧。呵呵。

    是不是说做多分配2^16(2^17?)的内存?

    4字节,32位
    理论上的
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-13 22:12:4121楼 得分:0
    引用 12 楼 lbc122 的回复:
    引用 9 楼 two_ears 的回复:
    你自己重载一下operator new[](size_t)就知道了, 
    编译器传给这个操作符的size_t总会比需要的大4个字节,自己看看干什么用的吧。呵呵。

    是不是说做多分配2^16(2^17?)的内存?

    4字节,32位
    理论上的
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-13 22:26:4122楼 得分:0
    最近上csdn老是出现有人回贴但看不见的情况,ctrl+f5也看不见 郁闷~
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-13 22:52:0423楼 得分:5
    C/C++ code
    #include "stdafx.h" #include "stdio.h" #include "windows.h" void* operator new[](size_t size) { void *p=HeapAlloc(GetProcessHeap(),0,size); printf("new %p: %u\n",p,size); return p; } void operator delete[](void* p) { printf("delete %p: %d\n",p, ((int*)p)[0]); BOOL ret=HeapFree(GetProcessHeap(),0,p); } class A { public: ~A(){} }; int main(int argc, char* argv[]) { A *p=new A[10]; printf("%p\n",p); delete [] p; system("pause"); return 0; }

    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-13 22:56:1924楼 得分:0
    PS:以上代码,VC6,输出
    new 00152A78: 14
    00152A7C
    delete 00152A78: 10
    请按任意键继续. . .

    这个记录是针对定义了析构函数的类的,lz有兴趣可以注释掉~A(),那样size就会输出10,而后面就没有意义了
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-13 23:16:4325楼 得分:0
    当你自己重载 delete[] 时你就晓得了!
    修改 删除 举报 引用 回复

    网站简介广告服务网站地图帮助联系方式诚聘英才English 问题报告
    世纪乐知(北京)网络技术有限公司 版权所有 京 ICP 证 020026 号
    Copyright © 2000-2007, CSDN.NET, All Rights Reserved