关于追加申请内存的问题
在C++中如果 new[10]; 如果实际需要20个,代码里怎么追加申请内存?
有MSDN的朋友可以查一下malloc()函数,在其下就有一个realloc()函数,是专门用来对付这种情况的
void *realloc(
void* memblock,
size_t size
)
MSDN中的例子:
#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 );
}