具有char,double,int和t的结构的大小 [英] Size of structure with a char, a double, an int and a t

查看:149
本文介绍了具有char,double,int和t的结构的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我只运行代码片段

  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;

它给我这样的结果:

  1 
8
4
4


b $ b

总计:17。



但是当我测试包含这些数据类型的sizeof结构时,它给我24,我很困惑。

  




$ b < #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 :: end1;

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

return 0;
}

:EDIT



我已经更新了这样的代码

  #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;
}

现在它显示我16个字节。

http://en.wikipedia.org/wiki/Data_structure_alignment\">调整 alignments 正确。例如,为了提高效率,默认情况下,指针位于4字节边界上,即其地址必须是4的倍数。如果结构体只包含一个char和一个指针

  struct {
char a;
void * b;
};

那么 b 不能使用加法器#1 - 它必须放置在#4。

  0 1 2 3 4 5 6 7 
+ --- + - - - - - - + --------------- +
| a | (未使用)| b |
+ --- + - - - - - - + --------------- +

在这种情况下,由于 int * 的对齐,额外的7个字节来自3个字节, double

  0 1 2 3 4 5 6 7 8 9 abcdef 
+ --------------- + --- + - - - - - - + --------------- + - - - - - - - - +
| i | ch | | p | |
+ --------------- + --- + - - - - - - + --------------- + - - - - - - - - +
10 11 12 13 14 15 16 17
+ -------------------------- ----- +
| d |
+ ------------------------------- +


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?

解决方案

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                             |
+-------------------------------+

这篇关于具有char,double,int和t的结构的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆