[英]Why am I able to print the sizeof variables even though I have not initialised them in C?
I am trying to understand when exactly a variable get memory in C when declared inside main function. I found a similar question When does memory gets allocated for a variable in c?我试图了解当在 main function 中声明时,一个变量在 C 中准确获得 memory 的时间。我发现了一个类似的问题When does memory gets allocated for a variable in c? here.这里。 It mentions that there is no hard and fast rule when memory will be allocated.它提到分配memory时没有硬性规定。 But I am trying to understand my case specifically.但我正在尝试具体了解我的情况。 I just declared some variable of different types but did not initialise any one of them.我只是声明了一些不同类型的变量,但没有初始化其中任何一个。 I just tried to print the sizeof those variable and I was able to print it.我只是尝试打印这些变量的大小,并且能够打印出来。 My question is whether memory is already allocated as I am able to print the size of the variable which I declared?我的问题是 memory 是否已经分配,因为我能够打印我声明的变量的大小?
#include <stdio.h>
int main()
{
int x;
int *y;
float a;
float *b;
double a1;
double *b1;
char c;
char *c1;
printf("sizeof of x = %d \n",sizeof(x));
printf("size of *x = %d \n",sizeof(*y));
printf("sizeof of a = %d \n" ,sizeof(a));
printf("sizeof of *b = %d \n" ,sizeof(*b));
printf("sizeof of a1 = %d \n" ,sizeof(a1));
printf("sizeof of *b1 = %d \n" ,sizeof(*b1));
printf("sizeof of c = %d \n" ,sizeof(c));
printf("sizeof of *c = %d \n" ,sizeof(*c));
}
Output:
sizeof of x = 4
size of *x = 4
sizeof of a = 4
sizeof of *b = 4
sizeof of a1 = 8
sizeof of *b1 = 8
sizeof of c = 1
sizeof of *c = 1
I have some follow up questions as well.我也有一些后续问题。 Why there is a difference in the sizeof pointer variable based upon data type?为什么基于数据类型的指针变量的大小不同? At the end, any pointer varaible will contain an address which is an integer.最后,任何指针变量都将包含一个地址,即 integer。
From the C Standard (6.5.3.4 The sizeof and alignof operators)来自 C Standard (6.5.3.4 The sizeof and alignof operators)
2 The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type. 2 sizeof 运算符产生其操作数的大小(以字节为单位),它可以是表达式或类型的括号名称。 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;结果为 integer。如果操作数的类型是可变长度数组类型,则计算操作数;否则计算操作数。 otherwise, the operand is not evaluated and the result is an integer constant.否则,不计算操作数,结果为 integer 常量。
So in your program all expressions with the sizeof
operator (your code does not have a declaration of a variable length array) are not evaluated and their result values are determined at compile time.因此,在您的程序中,所有带有sizeof
运算符的表达式(您的代码没有可变长度数组的声明)都不会被计算,它们的结果值在编译时确定。
The compiler uses an expression in the sizeof
operator to determine the type of the expression.编译器使用sizeof
运算符中的表达式来确定表达式的类型。
You could even write for example你甚至可以写例如
printf("sizeof of ++x = %zu \n",sizeof(++x));
or或者
printf("sizeof of ++x + ++*y = %zu \n",sizeof( ++x + ++*y));
Neither object is changed in these calls of printf
with expressions with the sizeof
operator. object 在printf
的这些调用中都没有改变,表达式带有sizeof
运算符。 The memory that occupied by the objects is not accessed.. object占用的memory没有被访问。。
Pay attention that you have to use the conversion specifier %zu
instead of %d
in calls of printf like请注意,您必须在 printf 的调用中使用转换说明符%zu
而不是%d
printf("sizeof of x = %zu \n",sizeof(x));
Otherwise using an invalid conversion specifier can invoke undefined behavior.否则使用无效的转换说明符会调用未定义的行为。
Also instead of using expressions in the sizeof
operator you may use type specifiers.此外,您可以使用类型说明符而不是在sizeof
运算符中使用表达式。
printf("sizeof of x = %zu \n",sizeof int );
If you would declare a variable length array then the sizeof
operator is evaluated at run-time (that is it is required to determine the number of elements in the array that can be done only at run-time) each time the control achieves an expression with the sizeof
operator with a variable length array.如果您要声明一个可变长度数组,则每次控件实现表达式时,都会在运行时评估sizeof
运算符(即需要确定只能在运行时完成的数组中的元素数量)使用带有可变长度数组的sizeof
运算符。
Consider this simple demonstration program考虑这个简单的演示程序
#include <stdio.h>
int main( void )
{
for ( size_t n = 1; n < 6; ++n )
{
int a[n];
printf( "sizeof( a ) = %zu\n", sizeof( a ) );
}
}
Its output might look like它的 output 可能看起来像
sizeof( a ) = 4
sizeof( a ) = 8
sizeof( a ) = 12
sizeof( a ) = 16
sizeof( a ) = 20
As for the memory allocation then for each object with automatic storage duration memory is allocated when the object is declared.至于 memory 分配,则在声明 object 时为每个具有自动存储持续时间 memory 的 object 分配。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.