Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Size of structure with a char, a double, an int and a t [duplicate]

Tags:

When I run only the code fragment

int *t;
std::cout << sizeof(char)   << std::endl;
std::cout << sizeof(double) << std::endl;
std::cout << sizeof(int)    << std::endl;
std::cout << sizeof(t)      << std::endl;

it gives me a result like this:

1
8
4
4

Total: 17.

But when I test sizeof struct which contains these data types it gives me 24, and I am confused. What are the additional 7 bytes?

This is the code

#include <iostream>
#include <stdio.h>
struct struct_type{
    int i;
    char ch;
    int *p;
    double d;
} s;

int main(){
    int *t;
    //std::cout << sizeof(char)   <<std::endl;
    //std::cout << sizeof(double) <<std::endl;
    //std::cout << sizeof(int)    <<std::endl;
    //std::cout << sizeof(t)      <<std::endl;

    printf("s_type is %d byes long",sizeof(struct struct_type));

    return 0;
}

:EDIT

I have updated my code like this

#include <iostream>
#include <stdio.h>
struct struct_type{
    double d_attribute;
    int i__attribute__(int(packed));
    int * p__attribute_(int(packed));;
    char  ch;
} s;

int main(){
    int *t;
    //std::cout<<sizeof(char)<<std::endl;
    //std::cout<<sizeof(double)<<std::endl;
    //std::cout<<sizeof(int)<<std::endl;
    //std::cout<<sizeof(t)<<std::endl;

    printf("s_type is %d bytes long",sizeof(s));

    return 0;
}

and now it shows me 16 bytes. Is it good, or have I lost some important bytes?

like image 615
dato datuashvili Avatar asked Aug 15 '10 09:08

dato datuashvili


People also ask

What is the size of char in structure?

A char (one byte) will be 1-byte aligned. A short (two bytes) will be 2-byte aligned. An int (four bytes) will be 4-byte aligned.

Is char * and int * the same size?

While their size is the same in practice, their alignment requirements are not. If you are doing "kernel coding", that already limits the number of platforms the code can run on. Check with the OS spec what is supported.

How do you determine the size of a variable in a structure?

In C language, sizeof() operator is used to calculate the size of structure, variables, pointers or data types, data types could be pre-defined or user-defined. Using the sizeof() operator we can calculate the size of the structure straightforward to pass it as a parameter.

What is the size of char type in C?

Char Size. The size of both unsigned and signed char is 1 byte always, irrespective of what compiler we use.


2 Answers

... it gives me 24, and I am confused. What are the additional 7 bytes?

These are padding bytes inserted by the compiler. Data structure padding is implementation dependent.

From Wikipedia, Data structure alignment:

Data alignment means putting the data at a memory offset equal to some multiple of the word size, which increases the system's performance due to the way the CPU handles memory. To align the data, it may be necessary to insert some meaningless bytes between the end of the last data structure and the start of the next, which is data structure padding.

like image 30
Prasoon Saurav Avatar answered Sep 25 '22 07:09

Prasoon Saurav


There is some unused bytes between some members to keep the alignments correct. For example, a pointer by default reside on 4-byte boundaries for efficiency, i.e. its address must be a multiple of 4. If the struct contains only a char and a pointer

struct {
  char a;
  void* b;
};

then b cannot use the adderss #1 — it must be placed at #4.

  0   1   2   3   4   5   6   7
+---+- - - - - -+---------------+
| a | (unused)  | b             |
+---+- - - - - -+---------------+

In your case, the extra 7 bytes comes from 3 bytes due to alignment of int*, and 4 bytes due to alignment of double.

  0   1   2   3   4   5   6   7   8   9   a   b   c   d   e   f
+---------------+---+- - - - - -+---------------+- - - - - - - -+
| i             |ch |           | p             |               |
+---------------+---+- - - - - -+---------------+- - - - - - - -+
 10  11  12  13  14  15  16  17
+-------------------------------+
| d                             |
+-------------------------------+
like image 160
kennytm Avatar answered Sep 21 '22 07:09

kennytm