相关疑难解决方法(0)

Visual C++相当于GCC的__attribute __((__ package___))

对于某些编译器,有一个结构的打包说明符,例如::

RealView ARM compiler has "__packed"
Gnu C Compiler has "__attribute__ ((__packed__))"
Visual C++ has no equivalent, it only has the "#pragma pack(1)"

我需要一些我可以放入结构定义的东西.

任何信息/黑客/建议?TIA ...

c c++ gcc visual-c++ data-structures

46
推荐指数
5
解决办法
6万
查看次数

结构的大小有char,double,int和at

当我只运行代码片段时

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;
Run Code Online (Sandbox Code Playgroud)

它给我一个这样的结果:

1
8
4
4
Run Code Online (Sandbox Code Playgroud)

总计:17.

但是,当我测试包含这些数据类型的sizeof结构时,它给了我24,我很困惑.额外的7个字节是多少?

这是代码

#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;
}
Run Code Online (Sandbox Code Playgroud)

:编辑

我已经像这样更新了我的代码

#include <iostream> …
Run Code Online (Sandbox Code Playgroud)

c++ structure-packing

20
推荐指数
3
解决办法
7597
查看次数

标签 统计

c++ ×2

c ×1

data-structures ×1

gcc ×1

structure-packing ×1

visual-c++ ×1