我用new分配了一块内存,并且也存放了数据。
是否存在这样的函数,将这块内存区扩大,并且不改变原来的数据?
我查过,turbo c 中好像有setblock,但是在vc中找不到!

解决方案 »

  1.   

    void *realloc( void *memblock, size_t size );以下摘自msdn:
    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 );
    }