user-0 提问: 请教malloc分配的内存连续性。

回复:
user: user-0
content: 引用 9 楼 zhao4zhong1 的回复:为什么不使用realloc呢？ 我是学到哪里思考到哪里，问到哪里。 recalloc昨天晚还没学习，但是今晚就要学习了。

user: user-1
content: 为什么不使用realloc呢？

user: user-2
content: malloc 从来不会试图保证两次分配的内存是连续的。

user: user-1
content: 参考Linux源代码中malloc函数的实现。

user: user-3
content: 分配内存时还会有一些记录相关信息的结构  http://blog.csdn.net/a10615/article/details/41063063

user: user-4
content: 楼主觉得地址应该差4才正常？ 那么你想想。如果我此时去free,怎么能知道我应该释放多大的内存空间呢？ malloc的时候，申请的空间一定是大于括号中的字节数的，因为它还需要保存边界信息以及内存管理信息，可能还要对齐 http://blog.csdn.net/misskissc/article/details/17717717

user: user-1
content: realloc 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.  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  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  memblock  Pointer to previously allocated memory block  size  New size in bytes  Remarks  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.  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, free, malloc

user: user-1
content: 引用 12 楼 jianyuezhimei 的回复:为什么要使用realloc呢？？？？？？？？？？？？？？？？ 难道realloc比malloc高级吗？？？？？？？？？？？？？？？？ realloc同一个指针多次就相当于实现了多次malloc且内存连续。

user: user-5
content: 为什么要使用realloc呢？？？？？？？？？？？？？？？？ 难道realloc比malloc高级吗？？？？？？？？？？？？？？？？

user: user-1
content: 引用 10 楼 qq_16774199 的回复:[quote=引用 9 楼 zhao4zhong1 的回复:] 为什么不使用realloc呢？ 我是学到哪里思考到哪里，问到哪里。 recalloc昨天晚还没学习，但是今晚就要学习了。 [/quote] 所以建议你今后改掉这个毛病。 学一段时间后再问。

user: user-6
content: 引用 3 楼 qq_16774199 的回复:[quote=引用 1 楼 user-6 的回复:] 00410218   00410228：首先这个是什么进制的？ %p 是以16进制的形式输出内存地址[/quote] 16进制的话就比较正常了，意思是地址差16，符合对齐的原则（为了访问速度更快）

user: user-0
content: 引用 1 楼 user-6 的回复:00410218   00410228：首先这个是什么进制的？ %p 是以16进制的形式输出内存地址

user: user-6
content: 引用 1 楼 user-6 的回复:00410218   00410228：首先这个是什么进制的？ 其次你不能认为它就应该连续！

user: user-6
content: 00410218   00410228：首先这个是什么进制的？