C ++ - 编译器生成的结构代码问题 [英] C++ - Compiler Generated Code for Structs Question

查看:70
本文介绍了C ++ - 编译器生成的结构代码问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我有一个关于C / C ++编译器如何处理关于结构生成机器代码的一般性问题。



让我们比较生成的代码:

Hello,
I have a general question regarding how a C/C++ compiler will handle generating machine code with respect to a struct.

Lets compare the generated code for:

int Var1;
int Var2;
bool Var3;

cout << Var1 << endl;





vs.





vs.

struct MyStruct
{
int Var1;
int Var2;
bool Var3;
};
MyStruct my;
cout << my.Var1;





编译器是否会为这两个代码块生成不同的机器代码?我一直在读它没有,但从来没有看到明确的是或否答案。



另外,typedef仅用于编译器的内部用途,不会导致它生成任何机器代码,对吗?



谢谢!



Will the compiler generate different machine code for those two blocks of code? I've been reading that it does not, but have never seen a clear-cut yes-or-no answer on it.

Also, typedef is only for internal purposes of the compiler and will not cause it to generate any machine code, correct?

Thank you!

推荐答案

这两段代码根本不同。这很清楚。



加载代码后,3个变量静态占用内存中的3个插槽。对于 struct ,它不占用任何内容,但实例化 MyStruct 确实获得了内存(事实并非如此同样,因为它取决于内存布局,它可以是编译器选项并依赖于编译器实现)。初始化 MyStruct 实例的代码也取决于上下文。通常,您显示的代码写在函数内部,这意味着实例将存储在堆栈中。返回后会弹出堆栈,因此结构实例占用的内存可以在其他堆栈框架中重复使用。



相反,个别变量将在初始化一开始为静态数据分配的内存。如果你 struct static 成员,你可以更接近地模拟这个操作。这些案件可能有多少相似之处。同样,它可能取决于特定的编译器及其选项 - 结构布局,内存对齐等等。



-SA
The two pieces of the code are fundamentally different. And this is quite clear.

Your 3 variables statically occupy 3 slots in memory after the code is loaded. As to the struct, it occupies none, but instantiation of MyStruct does get memory (it's not the fact that exactly the same, as it depends on memory layout, which can be a compiler option and depend on compiler implementation). The code for initialization of the instance of MyStruct also depends on context. Usually, the code you show is written inside a function, it means that the instance will be stored on stack. The stack will pop after return, so the memory occupied by the structure instance can be re-used in other stack frame.

In contrast, individual variables will be initialized in the memory allocated in the very beginning for static data. You could more closely simulate this operation, if you struct had static members. How much similar those cases could be. Again, it may depend on a particular compiler and its options — structure layout, alignment in memory, things like that.

—SA


尝试自己检查程序集输出。通过编译器生成的程序集可能非常有趣。



例如,在gcc中你可以使用-S命令行选项

使用Visual Studio,转到项目属性 - >配置属性 - > C / C ++ - >输出文件并设置'汇编输出'选项。



例如,对于以下代码:

Try checking the assembly output for yourself. Going through the assembly generated by a compiler can be quite interesting.

For example, in gcc you can use the -S command line option
With Visual studio, go to project properties->Configuration Properties->C/C++->Output Files and set the 'Assembler output' option.

for instance, for code like:
int a;
int b;

struct c
{
	int a;
	int b;
};

.
.
.

struct c cs;
a = 1;
b = 2;
cs.a = 3;
cs.b = 4;





我的Visual Studio 2008快递生成了以下asm代码:





My Visual studio 2008 express generated the following asm code:

; 18   : 	struct c cs;
; 19   : 	a = 1;
	mov	DWORD PTR ?a@@3HA, 1			; a
; 20   : 	b = 2;
	mov	DWORD PTR ?b@@3HA, 2			; b
; 21   : 	cs.a = 3;
	mov	DWORD PTR _cs


[ebp], 3
; 22 :cs.b = 4 ;
mov DWORD PTR _cs
[ebp], 3 ; 22 : cs.b = 4; mov DWORD PTR _cs


这篇关于C ++ - 编译器生成的结构代码问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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