Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this the correct explanation for this output of this program?

Tags:

c

structure

#include<stdio.h>

int main()
{
    struct test
    {
        char c;
        int y;
        float r;
        double d;
    } t1; 
    printf("%d\n",sizeof(t1));
    return 0;
}   

Output: 24 on my gcc 4.3.2 Ubuntu 12.04

Output: 20 on Ideone Link for Running Code

My Explanation: I think 24 is right. Correct me if i am wrong ?

  1. 1st chunk will be taken by Char 'c'. 1 byte for himself followed by 3 bytes of padding.
  2. 2nd chunk will be taken by Integer 'y'. (No padding required in this case as integer is of 4 byte. so it will completely fill the chunk).
  3. 3rd chunk will be taken by float 'r'. (No padding required as float is of 4 bytes too).
  4. Now next chunk of 4 bytes will be padded because next is double. and storage address have to multiple of size of that data type.
  5. and next 2 chunk of 4 bytes each will be occupied by double variable.

Thus Size = 1(char) + 3(Padded in Char case) + 4(int) + 4(float) + 4(padded for Double) + 8(double) = 24

For Clarity:

Since Chunks are taken in 4 byte collection. so every address will be Multiple of 4. For double , Next address has to be 1012. but it is not multiple of 8. so pad it ! and start from 1016 memory layoutMy Question - Is 24 correct and my explanation correct or my explanation is wrong else please explain ?

like image 582
S J Avatar asked Jun 20 '13 16:06

S J


People also ask

What is the output for the program?

Programs require data to be input. This data is used (processed) by the program, and data (or information ) is output as a result.

What is the output program of C program?

Example 1: C Output The code execution begins from the start of the main() function. The printf() is a library function to send formatted output to the screen. The function prints the string inside quotations.

Where do we see the output of the program?

Output of Program A program or other electronic device's output is any information it processes and sends out. Anything visible on the monitor screen, such as the words you write on your keyboard, is an example of the output.


Video Answer


1 Answers

Your explanation is correct in the sense that the specific compiler outputs a binary with a specific layout for that struct. If you have correctly predicted the outputs, then your prediction is obviously correct for your specific setup at this specific time.

But the struct's size is not, by any means, strictly defined in C. The only thing that's defined in this subject is the order of the struct members and the offset of the first member. The padding and, therefore, the total size of the struct is undefined and it's left as a decision for the compiler. Obviously, the main reason a compiler would pad the struct is higher performance through proper alignment of the members based on their individual types (as you correctly noted). But (and this is important here) the compiler chooses the most efficient padding depending on the target architecture. If an architecture has a 128-bit int, for instance, you can imagine how the size of your struct would be radically different.

So, if it comes to the defined aspects of the C language (or to any setup other than what you described) your prediction would be baseless. It would naturally match some setups, but only by accident.

like image 195
Theodoros Chatzigiannakis Avatar answered Sep 28 '22 04:09

Theodoros Chatzigiannakis