简体   繁体   English

为什么我能够打印 sizeof 变量,即使我没有在 C 中初始化它们?

[英]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.

相关问题 为什么 C 宏充当指针,即使我没有通过指针传递变量 - Why do C macros act as pointers even though I am not passing variables by pointer 我如何能够在不交换或混合它们的情况下专门打印出数组中的偶数? - How am I able to specifically print out the even numbers inside the array without interchanging or mixing them up? 为什么即使我用 C 语言扫描小数字,我的数组也会打印大数字 - Why I my array prints large numbers even though i am scanning the small numbers in C language 为什么即使没有错误也没有得到想要的输出? - Why am I not getting the desired output even though no errors? 即使我在联合中使用指针,为什么联合的大小不是 8 个字节而是 4 个字节? - Why is the sizeof union not 8 bytes but 4 bytes even if I am using pointer in my union? 我有一个名称相似的结构变量,如何在 c 中打印出来 - I have a structure variables with similar names, how to print them out in c 为什么即使我定义了我正在使用的命令,也会得到对另一个文件的未定义引用? - Why am I getting an undefined reference to another file, even though I define the command I am using? 我收到一个“错误:重新定义”符号,即使我之前在文件中使用了相同的语法, - I am getting an "error: redefinition of" symbol, even though I have used the same syntax earlier in the file, 为什么我可以使用数组索引访问其他变量? - Why am i able to access other variables using array indexing? 为什么我在执行时收到“public_stream, = nullptr”错误? 即使我已成功使用“fopen_s”读取文件? - Why am I getting a 'public_stream ! = nullptr' error on execution, even though I have successfully used 'fopen_s' to read a file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM