malloc() 中的两倍大小

Double size in malloc()

如果我在 malloc() 中将大小加倍,内存分配是多少。

    struct node{
    int data;
    struct node*next;
    };
    void main(){
    //What if I place 2,3 or 4 times the size.What is the memory allocation looks like.What if I do something like 1.5 times or say 1.3 times
    struct node* a =(struct node *)malloc(2*sizeof(struct node)); 
//Moreover if I print this I get 8,if I am saying twice size,shouldn't I get 16 size(two nodes with 8 bytes each(4 byte of data and 4 byte of next))
printf("%d",sizeof(*a));
    }

当你说 malloc(2*sizeof(struct node)) 时,你正在分配一个内存块,可以存储两个 struct node 彼此紧挨着。如果你说 3 或 4 等等,那就是在这个更大的分配中你可以并排放置多少 struct node

如果你说 malloc(1.5*sizeof(struct node)) 之类的话,那么你应该得到一个编译器警告或错误关于将 double 传递给需要 int 的函数 (malloc) ,因为 1.5*sizeof(struct node)1.5 乘以一个整数,它是一个双精度数。

sizeof(*a) 实际上不取决于您的 malloc 调用,而仅取决于 struct node 的单个实例在内存中的大小。此大小由您的结构中的实际字段以及您的 C 编译器实现所需的任何填充或额外字节组成。

即使您的问题不清楚,我认为您对 sizeof() 运算符感到困惑,它不会为您提供对象的分配大小,而是它的类型的大小,在这种情况下数组,因为它们是数组类型,所以它们的大小将是数组的大小(以字节为单位)。

这是引用自标准草案

6.5.3.4 The sizeof and _Alignof operators

  1. The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type. The size is determined from the type of the operand. The result is an integer. If the type of the operand is a variable length array type, the operand is evaluated; otherwise, the operand is not evaluated and the result is an integer constant.
  2. The _Alignof operator yields the alignment requirement of its operand type. The operand is not evaluated and the result is an integer constant. When applied to an array type, the result is the alignment requirement of the element type.
  3. When sizeof is applied to an operand that has type char, unsigned char, or signed char, (or a qualified version thereof) the result is 1. When applied to an operand that has array type, the result is the total number of bytes in the array. 103) When applied to an operand that has structure or union type, the result is the total number of bytes in such an object, including internal and trailing padding.

此外,如果你正在使用c,请不要将void *转换为其他指针类型,这是没有必要的,如果是c++,则使用new而不是malloc() .

node *a = malloc(2 * sizeof(*a));

分配了足够的 space 来容纳两个 node 类型的对象,因此如果您想要

,您可以使用它和索引符号
a[0].data = 1;
a[1].data = 2;

但这并不意味着 a 是一个数组,这种语法是可能的,因为它等效于使用指针算法是

(*(a + 0)).data = 1;
(*(a + 1)).data = 1;
/* incrementa a by 1, and then dereference it -> a[1] */

我认为更容易看出 a 不是数组。


103)当应用于声明为数组或函数类型的参数时,sizeof 运算符产生调整后(指针)类型的大小(请参阅6.9.1).

sizeof *a 不会改变 - 这是您取消引用 a 时得到的 元素 的大小。因为*a是一个struct node,那么sizeof *a==sizeof (struct node).

顺便说一句,不要转换 malloc() 的结果 - 相反,#include <stdlib.h>.

在任何事情之前 :

  • main() 应该 return int,而不是 void.
  • 不要转换 malloc() 的结果。
  • 请缩进代码以提高可读性。
  • size_t 的格式说明符是 %zu,而不是 %d

现在,让我们回答您的问题:

What if I place 2,3 or 4 times the size.What is the memory allocation looks like.What if I do something like 1.5 times or say 1.3 times

好吧,您为这些结构中的 2/3/4 分配了足够的空间,将它们打包在一个数组中。您可以使用数组索引符号访问它们:

// Accesses the second struct
a[1].data = 47;

Moreover if I print this I get 8,if I am saying twice size,shouldn't I get 16 size(two nodes with 8 bytes each(4 byte of data and 4 byte of next))

sizeof 是编译时*运算符。它测量您传递给它的类型的大小。 a 是指向 struct node 的指针,因此 *astruct node,这就是您要测量的内容。如果您想跟踪动态分配数组的长度,您需要手动完成。

*除非它的操作数是一个VLA。但是没有人喜欢 VLA。