I understand the difference between memory allocation using malloc for an array of struct and for an array of pointers to struct. But I want to assign memory using malloc for an array of struct, in the case that the struct itself contains a pointer to another struct. Here is the code for what I am trying to do:
我理解使用malloc为struct数组和struct指向数组的内存分配之间的区别。但是我希望使用malloc为struct数组分配内存,在struct本身包含指向另一个struct的指针的情况下。这是我想要做的代码:
#include<stdio.h>
#include<stdlib.h>
typedef struct linkedlist {
int pCol;
struct linkedlist *next;
} plist;
typedef struct particle {
int color;
double rad;
double rx;
double ry;
double vx;
double vy;
struct linkedlist *event;
} state;
int main()
{
int N = 5, w = 4;
plist *ls = (plist*)malloc((N+w)*sizeof(plist));
printf("N=%d, w=%d, sizeof state=%d, total=%d, sizeof discs=%d\n\n",
N,w,sizeof(plist), (N+w)*sizeof(plist), sizeof(ls));
state *discs = (state*)malloc((N+w)*sizeof(state));
printf("N=%d, w=%d, sizeof state=%d, total=%d, sizeof discs=%d\n",
N,w,sizeof(state), (N+w)*sizeof(state), sizeof(discs));
return 0;
}
When I run this, I get the following output:
当我运行它时,我得到以下输出:
N=5, w=4, sizeof plist=8, total=72, sizof ls=4
N=5, w=4, sizeof state=56, total=504, sizof discs=4
So, I want to know why does the ls or the discs get assigned only 4 bytes and how can I get total memory required assigned for these arrays of structs?
所以,我想知道为什么ls或光盘只被分配4个字节,我怎样才能获得为这些结构数组分配的总内存?
2 个解决方案
#1
2
You are printing size of pointer(ls) and not size of datatype/structrure pointed by the pointer (*ls).
您正在打印指针(ls)的大小,而不是指针指向的数据类型/ structrure的大小(* ls)。
Do sizeof(*ls)
and sizeof(*discs)
in your printf statements if you intend to find size of plist
and state
because that is where ls
and discs
point to.
如果您打算查找plist和state的大小,请在printf语句中执行sizeof(* ls)和sizeof(* disc),因为这是ls和disc指向的位置。
If you are thinking of printing the total size allocated to ls
or discs
by malloc. That is not possible. Its not going to print ((N+w)*sizeof(plist))
. So even doing sizeof(*ls)
is not going to tell you how many plist
structures you allocated
如果您正在考虑通过malloc打印分配给ls或光盘的总大小。这是不可能的。它不会打印((N + w)* sizeof(plist))。所以即使做sizeof(* ls)也不会告诉你你分配了多少个plist结构
#2
1
in your code, ls
is of type plist *
,.i.e., a pointer.
在你的代码中,ls是plist *类型,即指针。
sizeof()
is an operator which returns the size of the data type, not the amount of memory allocated.
sizeof()是一个运算符,它返回数据类型的大小,而不是分配的内存量。
Same goes for discs
.
光盘也一样。
#1
2
You are printing size of pointer(ls) and not size of datatype/structrure pointed by the pointer (*ls).
您正在打印指针(ls)的大小,而不是指针指向的数据类型/ structrure的大小(* ls)。
Do sizeof(*ls)
and sizeof(*discs)
in your printf statements if you intend to find size of plist
and state
because that is where ls
and discs
point to.
如果您打算查找plist和state的大小,请在printf语句中执行sizeof(* ls)和sizeof(* disc),因为这是ls和disc指向的位置。
If you are thinking of printing the total size allocated to ls
or discs
by malloc. That is not possible. Its not going to print ((N+w)*sizeof(plist))
. So even doing sizeof(*ls)
is not going to tell you how many plist
structures you allocated
如果您正在考虑通过malloc打印分配给ls或光盘的总大小。这是不可能的。它不会打印((N + w)* sizeof(plist))。所以即使做sizeof(* ls)也不会告诉你你分配了多少个plist结构
#2
1
in your code, ls
is of type plist *
,.i.e., a pointer.
在你的代码中,ls是plist *类型,即指针。
sizeof()
is an operator which returns the size of the data type, not the amount of memory allocated.
sizeof()是一个运算符,它返回数据类型的大小,而不是分配的内存量。
Same goes for discs
.
光盘也一样。