例如:
typedef struct
{
   timeb  time;  
   long  state;  
} s_time;
函数:
    bool a(s_time* ps_time);//ps_time指向一个s_time数组
在函数a内部,怎样做才能得到ps_time所指内存块的大小,以确保对该内存块赋值时不会越界?

解决方案 »

  1.   

    一般的做法是传递参数时附加数组大小信息,如:
    bool a(s_time* ps_time, UINT nSize);
      

  2.   

    你应该指定ps_time指针所指内存的大小,否则程序出错
      

  3.   

    sizeof(*ps_time);是行不通地。
    ps_time指向的是一个数组,而*ps_time只代表数组的第一个元素(如果未作ps_time增减变化的话),sizeof(*ps_time)只能获得一个结构体元素的大小。
      

  4.   

    If u don't use another parameter to transfer the size,it's difficult to get it in the function :)
      

  5.   

    typedef struct
    {
       int    nSize;   //结构大小
       timeb  time;  
       long  state;  
    } s_time;
    这样ok?
      

  6.   

    只有当前模块声明的固定长度数组可以用sizeof得到大小,指针则没办法得到。看来只能手工指定了。
      

  7.   

    Returns the size of a memory block allocated in the heap.size_t _msize( void *memblock );#include<malloc.h>
      

  8.   

    没有办法知道。
    您可以
    (1)在结构中附加结构大小的信息(Windows.h中很多结构是这样)
    typedef struct
    {
       size_t size;// =sizeof(s_time);
       timeb  time;  
       long  state;  
    } s_time;(2)给函数一个额外的参数,表示大小(API很多就是这样)
    bool a(s_time* ps_time,size_t size); //size=sizeof(s_time);(3)其他标记
       比如字符串,就是用'\0'来表示结束。函数根据它的位置可以判别大小。