-1

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? here. It mentions that there is no hard and fast rule when memory will be allocated. 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 ?

#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.

3
  • 3
    Because it’s known at compile time Commented Jan 24, 2023 at 13:29
  • 1
    You don't print the size of any pointer in your program. y is the pointer, *y is the data the ponter pints to.
    – bolov
    Commented Jan 24, 2023 at 13:34
  • 2
    Because the size is known whether or not there is an allocation. You can also do sizeof(int) which isn't a variable but a type. Note that "any pointer variable will contain an address which is an integer" is incorrect. All computer data is bits, but a pointer is not an integer nor int and your %d is undefined behaviour. Please use %zu for type size_t. Commented Jan 24, 2023 at 13:41

1 Answer 1

3

From the 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. 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.

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.

The compiler uses an expression in the sizeof operator to determine the type of the expression.

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. The memory that occupied by the objects is not accessed..

Pay attention that you have to use the conversion specifier %zu instead of %d in calls of printf like

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.

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.

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

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.