获得结构的大小 [英] obtaining the size of a structure
问题描述
您好我正在写一个小程序,我需要获得一个结构的实际大小
。
该程序是关闭
struct abc
{
int j;
char k;
int i;
} * a;
main()
{
a-> j = 10;
printf(结构大小为%d,sizeof(a));
}
在这个程序中,当我打印结构的大小时,我得到整个
大小(即2个int变量的大小+ char变量的大小)。
但是我需要获得结构的大小,使其类似于实际使用的内存块。
谢谢
Krish
kris写道:
你好我正在写一个小程序,我需要获得一个结构的实际大小
。
该程序是跟随
标准标题,例如< stdio.hmissing
struct abc
{
int j;
char k;
int i;
} * a;
main()
我更喜欢" int main(void)"我认为可能在C99中需要
{
a-> j = 10;
使用未初始化指针的值。坏狗!没有饼干!
>
printf(结构大小是%d,sizeof(a));
它告诉你指针的大小。 "的sizeof(* A)"会告诉你
大小的结构。
}
没有返回声明很糟糕。
kris写道:
你好我正在写一个小程序,我需要获得结构的实际大小
。
程序如下
struct abc
{
int j;
char k;
int i;
} * a;
main()
{
a-> j = 10;
printf (结构的大小是%d,sizeof(a));
}
在这个程序中我打印结构的大小我得到整个
大小(即2个int变量的大小+ char变量的大小)。
但是我需要得到结构的大小,使它类似于
实际使用的内存块。
(该程序不起作用,但我认为它只是一个复制和粘贴错误?)
首先,sizeof(a)将始终返回
指针占用的字节数(因为a是指针),在大多数32位系统上应为4。第二,结构的大小是sizeof(struct abc)。这是与sizeof(* a)相同的
,假设您为a分配了空间。 with:
a = malloc(sizeof(struct abc));
结构的大小始终相同。我不明白你的意思是什么?b $ b意思是我需要得到结构的大小,以便它类似于实际使用的内存释放。 。
Nikos Chantziaras写道:
kris写道:
>您好我正在编写一个小程序,我需要获取结构的实际大小。
程序如下
struct abc
{
int j;
char k;
int i;
} * a;
main()
{
a-> j = 10;
printf(结构大小为%d,sizeof(a));
}
其次,结构的大小是sizeof(struct abc)。这是与sizeof(* a)相同的
,假设您为a分配了空间。 with:
a = malloc(sizeof(struct abc));
Nope - sizeof(* a)将返回结构的大小,即使是
未初始化或NULL指针...
Hi I am writing a small program where I need to obtain the actual size
of a structure.
The programm is as follows
struct abc
{
int j;
char k;
int i;
}*a;
main()
{
a->j=10;
printf("size of structure is %d",sizeof(a));
}
In this program when I print the size of structure I get the whole
size (i.e size of 2 int variables+size of char variable).
But I need to get the size of structure such that it resembles the
chunk of memory that is actually being used.
Thanks
Krish
kris wrote:Hi I am writing a small program where I need to obtain the actual size
of a structure.
The programm is as follows
Standard headers such as <stdio.hmissing
struct abc
{
int j;
char k;
int i;
}*a;
main()I''d prefer "int main(void)" and I think it may be required in C99
{
a->j=10;using the value of a uninitialized pointer. Bad dog! No biscuit!
>
printf("size of structure is %d",sizeof(a));That tells you the size of the pointer. "sizeof(*a)" would tell you the
size of the structure.
}No "return" statement is poor style.
kris wrote:Hi I am writing a small program where I need to obtain the actual size
of a structure.
The programm is as follows
struct abc
{
int j;
char k;
int i;
}*a;
main()
{
a->j=10;
printf("size of structure is %d",sizeof(a));
}
In this program when I print the size of structure I get the whole
size (i.e size of 2 int variables+size of char variable).
But I need to get the size of structure such that it resembles the
chunk of memory that is actually being used.(That program won''t work, but I assume it''s just a copy&paste error?)
First, sizeof(a) will always return the number of bytes occupied by
pointers (since "a" is a pointer), which should be 4 on most 32-bit systems.
Second, the size of the struct is "sizeof(struct abc)". This is the
same as "sizeof(*a)", assuming you allocated the space for "a" with:
a = malloc(sizeof(struct abc));
The size of the struct is always the same. I don''t understand what you
mean with "I need to get the size of structure such that it resembles
the chuck of memory that is actually being used".
Nikos Chantziaras wrote:kris wrote:
>Hi I am writing a small program where I need to obtain the actual size
of a structure.
The programm is as follows
struct abc
{
int j;
char k;
int i;
}*a;
main()
{
a->j=10;
printf("size of structure is %d",sizeof(a));
}
Second, the size of the struct is "sizeof(struct abc)". This is the
same as "sizeof(*a)", assuming you allocated the space for "a" with:
a = malloc(sizeof(struct abc));Nope - sizeof(*a) will return the sizeof the structure, even for an
uninitialized or NULL pointer...
这篇关于获得结构的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!