一个函数的参数传递一个数组,可以通过数组的指针但是不能知道这个数组的大小,除非单独指定比如int * layida(int * pData,int dSum)
pData指向一个数组,也可以是new分配的,
函数内部无法知道这个指针指向数组的大小,只能通过参数dSum知道(还有其它办法吗?)我想问,在函数内部如何检测这个指针指向的空间是不是dSum这么大
如果dSum出错,也可以即时知道,否则会让pData越界就是这么个想法,过来问问查了半天MSDN只找到这个,不知道对不对,有什么问题
ASSERT(dSum==0 || AfxIsValidAddress(pData,dSum*sizeof(double)));

解决方案 »

  1.   

    不好意思写错了
    ASSERT(dSum==0 ¦¦ AfxIsValidAddress(pData,dSum*sizeof(int)));
      

  2.   

    sizeof(double)?
    应该是sizeof(int)吧!
      

  3.   

    有啊 _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 
    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 );
    }