在C中声明一个指向结构的指针的数组,但直到需要时才为结构分配内存 [英] Declaring an array of pointers to structures in C, but not allocating memory for structure until needed

查看:141
本文介绍了在C中声明一个指向结构的指针的数组,但直到需要时才为结构分配内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为C中名为 base 的结构的 n 个指针数组分配空间.我不想为一个结构分配空间.除非需要.

I'm trying to allocate space for an array of n pointers to a struct named base in C. I don't want to allocate the space for a struct unless it is needed.

如果在用户会话期间需要多个 n 个结构,那么我将 realloc 另一组n个指针.

If more than n structs are required during a user session, then I'll realloc another set of n pointers.

请告诉我这是声明它们的正确方法,不包括任何重新分配吗?

Would you please tell me if this is the correct method of declaring them, excluding any reallocation?

我问的一个原因是,我不明白为什么 printf(%d",sizeof(ptr [0]))返回 sizeof(base)在尚未分配任何内存之前用于 base 的实例.

One reason I'm asking is, that I don't understand why printf("%d", sizeof(ptr[0])) returns sizeof(base) before any memory has yet been allocated for an instance of base.

仅仅是因为它是指向基础的指针,并且会占用那么多空间吗?

Is it simply because it's a pointer to base and will occupy that much space?

我只是想确保在没有需要之前,不要为 n 个base结构分配空间.

I just wanted to make sure that I'm not allocating the space for n structs of base before any are needed.

/* Global declaration */
struct base { ... };
struct base *ptr;

/* in main() */
ptr = calloc( n, sizeof ( char ) );

/* Function to return pointer to newly allocated struct base */
struct base *base_alloc( void )
{
    return ( struct base * ) malloc( sizeof ( struct base ) );
}

/* Within a function to create new base and populate it.*/
ptr[i] = base_alloc();

推荐答案

我将清除两件事:

我不明白为什么在尚未为base实例分配任何内存之前,printf(%d",sizeof(ptr [0]))返回sizeof(base).

I don't understand why printf("%d", sizeof(ptr[0])) returns sizeof(base) before any memory has yet been allocated for an instance of base.

这是因为 sizeof 会评估在编译时表达式的类型的对象所占用的字节数.例如.这里的表达式 ptr [0] 具有类型 struct base ,因此 sizeof 返回表示 struct base 所需的字节数对象.这与内存分配无关.

That is because sizeof evaluates the number of bytes occupied by an object of the type of an expression at compile time. E.g. here the expression ptr[0] has type struct base so sizeof returns the number of bytes needed to represent a struct base object. This is unrelated to memory allocation.

其余的代码:

  • 您希望 ptr 的类型为 struct base ** .
  • 您也不想使用 calloc ,因为不能保证 NULL 指针实际上将所有位都设置为零.
  • 最后,无需强制转换 malloc 返回的值.
  • You want ptr to have type struct base **.
  • You also don't want to use calloc because NULL pointers are not guaranteed to actually have all bits set to zero.
  • Finally, there is no need to cast the value returned by malloc.

总共:

/* Global declaration */
struct base { ... };
struct base **ptr;

/* in main() */
ptr = malloc( n * sizeof *ptr );
for (size_t i = 0; i < n; ++i)
{
  ptr[i] = NULL;
}

/* Function to return pointer to newly allocated struct base */
struct base *base_alloc( void )
{
  return malloc( sizeof ( struct base ) );
}

/* Within a function to create new base and populate it.*/
ptr[i] = base_alloc();

这篇关于在C中声明一个指向结构的指针的数组,但直到需要时才为结构分配内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆