C程序使用pragma和属性来减小struct的大小 [英] C program to reduce the size of struct using pragma and attributes

查看:77
本文介绍了C程序使用pragma和属性来减小struct的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我被问到以下逻辑的面试问题



我想要例如,使用pragma和C中的属性来减小结构大小的逻辑



如果我有结构



 struct a 
{
int c;
char b;
char d;
};





分配给上面结构的总大小是8个字节,但我想减少上面的结构使用pragma和属性逻辑到6字节(即确切的大小)任何人都可以解释这个





谢谢

shan

解决方案

哦, Stack Overflow的魔力 [ ^ ]。

您正在标记C和Linux。我不知道linux是否有打包选项,但在windows世界中,你想看看#pragma pack。 MSDN信息可以在这里找到[ ^ ] ...



对于Windows以外的任何其他内容,Google都是您的朋友。


hi all,

I was asked a interview question for the below logic

I want a logic for reducing the size of structure using pragma and attributes in C for example

If i have structure

struct a
{
   int c;
   char b;
   char d;
};



The total size allocated to the above the structure was 8 bytes, But i want to reduce the above structure to 6 byte (i.e exact size) using the pragma and attributes logic can anyone explain this


Thanks
shan

解决方案

Oh, the magic of Stack Overflow[^].


You are flagging C and linux. I don't know whether or not linux has a pack option, but in the windows world, you want to look at "#pragma pack". The MSDN info can be found here[^]...

For anything other than Windows, Google is your friend.


这篇关于C程序使用pragma和属性来减小struct的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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