malloc分配失败最多能分配多少内存

请教malloc分配的内存连续性。
来源:csdn
【在我电脑显示
。为什么第二个地址比第一个大了不只4字节?是分配不连续还是别的道理?】
:首先这个是什么进制的?
其次你不能认为它就应该连续!
%p 是以16进制的形式输出内存地址
16进制的话就比较正常了,意思是地址差16,符合对齐的原则(为了访问速度更快)
楼主觉得地址应该差4才正常?
那么你想想。如果我此时去free,怎么能知道我应该释放多大的内存空间呢?
malloc的时候,申请的空间一定是大于括号中的字节数的,因为它还需要保存边界信息以及内存管理信息,可能还要对齐
分配内存时还会有一些记录相关信息的结构
zhao4zhong1:
参考Linux源代码中malloc函数的实现。
malloc 从来不会试图保证两次分配的内存是连续的。
zhao4zhong1:
为什么不使用realloc呢?
我是学到哪里思考到哪里,问到哪里。
recalloc昨天晚还没学习,但是今晚就要学习了。
zhao4zhong1:
所以建议你今后改掉这个毛病。
学一段时间后再问。
jianyuezhimei:
为什么要使用realloc呢????????????????
难道realloc比malloc高级吗????????????????
zhao4zhong1:
realloc同一个指针多次就相当于实现了多次malloc且内存连续。
zhao4zhong1:
Reallocate memory blocks.
void *realloc( void *memblock, size_t size );
Routine Required Header Compatibility
realloc &stdlib.h& and &malloc.h& ANSI, Win 95, Win NT
For additional compatibility information, see Compatibility in the Introduction.
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
realloc returns a void pointer to the reallocated (and possibly moved) memory block. The return value is NULL if the size is zero and the buffer argument is not NULL, or if there is not enough available memory to expand the block to the given size. In the first case, the original block is freed. In the second, the original block is unchanged. The return value points to a storage space that is guaranteed to be suitably aligned for storage of any type of object. To get a pointer to a type other than void, use a type cast on the return value.
Parameters
Pointer to previously allocated memory block
New size in bytes
The realloc function changes the size of an allocated memory block. The memblock argument points to the beginning of the memory block. If memblock is NULL, realloc behaves the same way as malloc and allocates a new block of size bytes. If memblock is not NULL, it should be a pointer returned by a previous call to calloc, malloc, or realloc.
The size argument gives the new size of the block, in bytes. The contents of the block are unchanged up to the shorter of the new and old sizes, although the new block can be in a different location. Because the new block can be in a new memory location, the pointer returned by realloc is not guaranteed to be the pointer passed through the memblock argument.
realloc calls malloc in order to use the C++ _set_new_mode function to set the new handler mode. The new handler mode indicates whether, on failure, malloc is to call the new handler routine as set by _set_new_handler. By default, malloc does not call the new handler routine on failure to allocate memory. You can override this default behavior so that, when realloc fails to allocate memory, malloc calls the new handler routine in the same way that the new operator does when it fails for the same reason. To override the default, call
_set_new_mode(1)
early in your program, or link with NEWMODE.OBJ.
When the application is linked with a debug version of the C run-time libraries, realloc resolves to _realloc_dbg. For more information about how the heap is managed during the debugging process, see Using C Run-Time Library Debugging Support.
/* 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 )
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 )) ))
exit( 1 );
size = _msize( buffer );
printf( "Size of block after realloc of 1000 more longs: %u\n",
free( buffer );
exit( 0 );
Size of block after malloc of 1000 longs: 4000
Size of block after realloc of 1000 more longs: 8000
Memory Allocation Routines
calloc, free, malloc
免责声明:本站部分内容、图片、文字、视频等来自于互联网,仅供大家学习与交流。相关内容如涉嫌侵犯您的知识产权或其他合法权益,请向本站发送有效通知,我们会及时处理。反馈邮箱&&&&。
学生服务号
在线咨询,奖学金返现,名师点评,等你来互动C语言malloc分配的空间,超出为啥也能正常使用?
你好,想跟你请教个问题:
&&&&很不哈意思,老是麻烦您b( ̄▽ ̄)d……
&&&&做练习的时候,发现malloc函数分配的空间,超出了也能照样使用,突然就不能理解了,那还要realloc干啥。假如说,超出了malloc分配的空间就不能使用,需要动态增加空间的话,那么新增加的空间,不还是malloc上限后面的那几个吗?毕竟要分配一个连续的空间嘛,假如后面的空间正好被其他重要软件占用,那么realloc应该怎么分配出新的空间,换个地方?这样的话空间不就不连续了吗?越想越迷糊……求指点
& & 测试了一下,分配一个空间,可以压入一个字符串
# include &stdio.h&
# include &string.h&
# include &malloc.h&
int main( void )
str = ( char * )malloc( sizeof( char ) );
strcpy( str, "abcdef" );
puts( str );
--- 共有 1 条评论 ---
说的好有道理,我竟然无言以对
你超过访问的长度肯定能继续,但是你破坏了其他地址的数据(当然后面的地址也不一定有人用,有人用肯定是破坏了)。
malloc分配的空间肯定是连续的,realloc会先探测后面的空间够不够,如果够就直接开辟,如果不够他会找一个新的地方开辟你要的空间,然后把内存内容拷贝到新开辟的空间。
你的例子肯定是有问题的,只不过你不知道你到底影响了谁,111222假如这是内存的数据111是脏数据(111就是你分配的)你改成abcd后就是abcd22你的abcd访问是没问题,但是222的数据就被损坏了
--- 共有 1 条评论 ---
恩恩,您这么一讲解顿时就明白了,谢谢您了哈2009年4月 总版技术专家分月排行榜第一
2009年11月 Linux/Unix社区大版内专家分月排行榜第一2009年6月 Linux/Unix社区大版内专家分月排行榜第一2009年4月 C/C++大版内专家分月排行榜第一2009年3月 C/C++大版内专家分月排行榜第一2009年3月 Linux/Unix社区大版内专家分月排行榜第一2009年2月 Linux/Unix社区大版内专家分月排行榜第一
2008年2月 VB大版内专家分月排行榜第一2003年4月 VC/MFC大版内专家分月排行榜第一2002年11月 VC/MFC大版内专家分月排行榜第一
2011年11月 VC/MFC大版内专家分月排行榜第二2008年3月 VB大版内专家分月排行榜第二2008年3月 硬件/嵌入开发大版内专家分月排行榜第二2003年4月 其他开发语言大版内专家分月排行榜第二2003年4月 VB大版内专家分月排行榜第二2003年3月 VB大版内专家分月排行榜第二
2008年2月 VB大版内专家分月排行榜第一2003年4月 VC/MFC大版内专家分月排行榜第一2002年11月 VC/MFC大版内专家分月排行榜第一
2011年11月 VC/MFC大版内专家分月排行榜第二2008年3月 VB大版内专家分月排行榜第二2008年3月 硬件/嵌入开发大版内专家分月排行榜第二2003年4月 其他开发语言大版内专家分月排行榜第二2003年4月 VB大版内专家分月排行榜第二2003年3月 VB大版内专家分月排行榜第二
2013年7月 C/C++大版内专家分月排行榜第一
2015年9月 C/C++大版内专家分月排行榜第二2013年6月 C/C++大版内专家分月排行榜第二
本帖子已过去太久远了,不再提供回复功能。如何减少频繁分配内存(malloc或者new)造成的内存碎片呢?
全部答案(共1个回答)
动态内存分配,可以自由的分配指定大小的内存空间。当程序运行时,程序员并不清楚某一数据需要的具体内存空间大小时,可以使用动态分配。 malloc函数原型 void...
进入系统后,将硬盘分2000,剩下的2000随便分一下就行了.
大家还关注
内存条显存颗粒的安装方法 跪求
确定举报此问题
举报原因(必选):
广告或垃圾信息
激进时政或意识形态话题
不雅词句或人身攻击
侵犯他人隐私
其它违法和不良信息
报告,这不是个问题
报告原因(必选):
这不是个问题
这个问题分类似乎错了
这个不是我熟悉的地区

我要回帖

更多关于 malloc分配失败 的文章

 

随机推荐