关于new分配内存的一个疑问

一路向南xxx6 2018-03-11 09:38:55

int *p = new int[20];

cout << " sizeof(p) = " << sizeof( p ) << endl;

delete [] p;


用new分配20个int型的空间给指针p

当调用delete删除内存时,不需要显式的写成 delete [20] p,说明系统心里对p指向的内存的整体大小心里有B数。但是程序运行中,不通过额外变量,我们只知道p是一个8字节的指针,不能推测出p实际指向的内存块大小。有什么办法可以从程序里读出这个隐藏的20吗?
...全文
505 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2018-03-15
  • 打赏
  • 举报
回复
Linux下,malloc和realloc都是开源的。
include_zhao 2018-03-14
  • 打赏
  • 举报
回复
引用 9 楼 zhao4zhong1 的回复:
_msize Returns the size of a memory block allocated in the heap. size_t _msize( void *memblock ); Routine Required Header Compatibility _msize <malloc.h> Win 95, Win NT For additional compatibility information, see Compatibility in the Introduction. Libraries LIBC.LIB Single thread static library, retail version LIBCMT.LIB Multithread static library, retail version MSVCRT.LIB Import library for MSVCRT.DLL, retail version Return Value _msize returns the size (in bytes) as an unsigned integer. Parameter memblock Pointer to memory block Remarks The _msize function returns the size, in bytes, of the memory block allocated by a call to calloc, malloc, or realloc. When the application is linked with a debug version of the C run-time libraries, _msize resolves to _msize_dbg. For more information about how the heap is managed during the debugging process, see Using C Run-Time Library Debugging Support. Example /* REALLOC.C: This program allocates a block of memory for * buffer and then uses _msize to display the size of that * block. Next, it uses realloc to expand the amount of * memory used by buffer and then calls _msize again to * display the new amount of memory allocated to buffer. */ #include <stdio.h> #include <malloc.h> #include <stdlib.h> void main( void ) { long *buffer; size_t size; if( (buffer = (long *)malloc( 1000 * sizeof( long ) )) == NULL ) exit( 1 ); size = _msize( buffer ); printf( "Size of block after malloc of 1000 longs: %u\n", size ); /* Reallocate and show new size: */ if( (buffer = realloc( buffer, size + (1000 * sizeof( long )) )) == NULL ) exit( 1 ); size = _msize( buffer ); printf( "Size of block after realloc of 1000 more longs: %u\n", size ); free( buffer ); exit( 0 ); } Output Size of block after malloc of 1000 longs: 4000 Size of block after realloc of 1000 more longs: 8000 Memory Allocation Routines See Also calloc, _expand, malloc, realloc
'========== 代码说明了一切 可是,malloc和realloc有有什么区别呢?
BoT-Win 2018-03-14
  • 打赏
  • 举报
回复
http://blog.csdn.net/hongye_05/article/details/40274689 这里有
赵4老师 2018-03-13
  • 打赏
  • 举报
回复
_msize Returns the size of a memory block allocated in the heap. size_t _msize( void *memblock ); Routine Required Header Compatibility _msize <malloc.h> Win 95, Win NT For additional compatibility information, see Compatibility in the Introduction. Libraries LIBC.LIB Single thread static library, retail version LIBCMT.LIB Multithread static library, retail version MSVCRT.LIB Import library for MSVCRT.DLL, retail version Return Value _msize returns the size (in bytes) as an unsigned integer. Parameter memblock Pointer to memory block Remarks The _msize function returns the size, in bytes, of the memory block allocated by a call to calloc, malloc, or realloc. When the application is linked with a debug version of the C run-time libraries, _msize resolves to _msize_dbg. For more information about how the heap is managed during the debugging process, see Using C Run-Time Library Debugging Support. Example /* REALLOC.C: This program allocates a block of memory for * buffer and then uses _msize to display the size of that * block. Next, it uses realloc to expand the amount of * memory used by buffer and then calls _msize again to * display the new amount of memory allocated to buffer. */ #include <stdio.h> #include <malloc.h> #include <stdlib.h> void main( void ) { long *buffer; size_t size; if( (buffer = (long *)malloc( 1000 * sizeof( long ) )) == NULL ) exit( 1 ); size = _msize( buffer ); printf( "Size of block after malloc of 1000 longs: %u\n", size ); /* Reallocate and show new size: */ if( (buffer = realloc( buffer, size + (1000 * sizeof( long )) )) == NULL ) exit( 1 ); size = _msize( buffer ); printf( "Size of block after realloc of 1000 more longs: %u\n", size ); free( buffer ); exit( 0 ); } Output Size of block after malloc of 1000 longs: 4000 Size of block after realloc of 1000 more longs: 8000 Memory Allocation Routines See Also calloc, _expand, malloc, realloc
自信男孩 2018-03-13
  • 打赏
  • 举报
回复
系统会知道p指向的内存有多少个字节,因为系统要管理我们申请的内存空间,以便我们不用时,能够通过调用接口释放(堆上的)或者系统能够给我们自动释放(栈上的)。 但是,自己申请的字节数还需要别人告诉我们吗?我们自己肯定知道我们申请了多少个字节。另外,即使我们给别人提供接口或者反过来别人给我们提供接口。那么作为提供方,是需要告知指针p申请了多少个字节的。
mk_lucifer 2018-03-12
  • 打赏
  • 举报
回复
C++ 的new和delete关键字是不同的,必须有平台支持,需要实现源代码,就好像C语言的malloc并不是独立存在的,new的源代码给你贴一段。。。

void *__CRTDECL operator new(size_t size) _THROW1(_STD bad_alloc)  
        {       // try to allocate size bytes  
        void *p;  
        while ((p = malloc(size)) == 0)  
                if (_callnewh(size) == 0)  
                {       // report no memory  
                        _THROW_NCEE(_XSTD bad_alloc, );  
                }  
  
        return (p);  
        }
以上代码你可以修改重载的,也就是说怎么new由编程者说了算,包括你自己都可以改,区别只是你用的VC++微软已经替你写好了,编译器根本就不知道new了多少,是编程者根据运行平台决定,delete同理。。。
mk_lucifer 2018-03-12
  • 打赏
  • 举报
回复
代码并不知道这块内存有多大,但你忘了你的所有程序都不是裸奔的,是运行在windows系统上的内存管理器,new说到底也不过是内存管理器对外的一个接口函数封装,你在new的时候内存管理器自然知道你new了多大,甚至它new的比你更大(比如你new了1字节,实际他申请1024字节,然后细分,和管理器上设置的内存最小页和管理方式有关系)。。。 windows系统有堆上申请内存的API,用此方式内存管理属于windows系统,你根本就不用关心它为什么会知道,这就好像你不用关心为什么银行人员为啥会知道你的手机号银行卡号家庭住址,这都是你告诉它的,你是通过一个代理流程在办事情,代理者怎么会不知道这些呢???
三岁、就很帅 2018-03-12
  • 打赏
  • 举报
回复
sizeof(p)是4个字节 也不是8呀 想知道大小sizeof数组
考拉一枚 2018-03-12
  • 打赏
  • 举报
回复
引用 4 楼 swwllx 的回复:
sizeof(p)是4个字节 也不是8呀 想知道大小sizeof数组
这个看操作系统 64位就是8 32位就是4
mstlq 2018-03-11
  • 打赏
  • 举报
回复
信息肯定有,但是标准没有规定信息一定要放在哪里,所以不同编译器生成的二进制文件,情况是不一样的,甚至同一个编译器在不同的编译选项下,情况也可能是不一样的,没有通用的办法获取这个值. 如果实际使用中楼主确实想知道分配大小,可以考虑使用http://zh.cppreference.com/w/cpp/container/array
真相重于对错 2018-03-11
  • 打赏
  • 举报
回复
除了vs 没有验证过别的编译器 首先内置类型比如int char,不准确的说:不需要这个长度信息。(这个信息肯定有是有内存管理负责) 考虑一下 下面的代码 int* p = (int*)malloc(sizeof(int)*20) ; 在以后的代码中如何确定20 如果是自定义类型 ,vs 在 这个指针前一个内存位置保留
codedoctor 2018-03-11
  • 打赏
  • 举报
回复
编译器的B数当然不能被你猜到啦。毕竟编译器就像女人一样,每个编译器都是不一样的

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧