混淆这个C ++编译 [英] Confuse about this C++ Compile

查看:73
本文介绍了混淆这个C ++编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello export expert,

以下是我找到的代码

Hello export expert,
Below is the code I found

#include <iostream>
#include <cstdlib>
#include <stdio.h>

using namespace std;

void a()
{
	printf("Hello World!\n");
}

int main()
{
	typedef int (*fun)();
	fun tmp = (fun)a;
	printf("0x%x\t0x%x\n",tmp,&a);
	if(tmp())
	{
		printf("a is ok!\n");
	}
	else
	{
		printf("a is not ok!\n");
	}
	system("pause");
}





但结果是:

0x401318 0x401318

Hello world

a不行!



我的编译器是CodeBlock GCC。

请帮帮我解释一下。如果我想打印它,该怎么办?



But the result given is:
0x401318 0x401318
Hello world
a is not ok!

My compiler is CodeBlock GCC.
Please help me to explain this. What if I want it to print a is Ok.

推荐答案

代码说明

Explanation of the code
#include <iostream>
#include <cstdlib>
#include <stdio.h>

using namespace std;
 
void a()
{
	printf("Hello World!\n");
}
 
int main()
{
        // I am guessing the problem area starts here
	typedef int (*fun)();// declaring a type, when you would use the type it would represent a function pointer return integer
         defining a variable that would point to a function whose return type would be int and wont take any argument.
	fun tmp = (fun)a;   //here you have assigned the function but the mistake you have made is function a is void returning function. but you have done the type casting so no error is being showed. 
	printf("0x%x\t0x%x\n",tmp,&a);  //format string %x tells the compiler to print number in hexadecimal format. so, you have printed two hexadecimal data. temp is already returning a pointer(all pointer are 32 bit long). a is the function but when you type &a it means the address location of function a()
	if(tmp()) // this will act bit wrong. if you call a() instead temp() you will get compilation error. but you are using pointer of a() which has wrong type casting. anyway because of nothing returned you are not getting this. But there would be situation where this condition will be true. 
	{
		printf("a is ok!\n");
	}
	else
	{
		printf("a is not ok!\n");
	}
	system("pause");
}





我不确定这些是否会通过你。另一个建议仔细了解。你犯了一个被认为是c / c ++的危险方面的错误。



将你的函数从void更改为int





返回工作的解释:(我的一些陈述可能是错的。如果我是,请告诉我)



当函数返回值编译器时,首先将返回值保存在AX寄存器中,然后进行下一步操作。

ie



I am not sure if any of this will go through you. Another advice learn carefully. You have made a mistake which is considered as dangerous side of c/c++.

change your function a type from void to int


explanation of how return work: (Some of my statement might be wrong. Let me know if I am)

When a function return a value compiler save the return value in AX register first then make the next move.
i.e.

int a()
{
 return 10; // 
}

int main()
{
 int b=a(); // this line will be break up into many many small pieces of assembly 
 //but in simple
 //10 will be stored in ax resister. then the value will be stored in b variable.
}





现在,printf函数返回的数量打印的字符。该值自动存储在ax寄存器中。在您的void函数中,您已经开发的函数不会返回任何东西,但是ax寄存器将具有值13.因此,您的if语句将为true但不是false。



另一个例子函数返回行为:





now, printf function return the number of character it printed. the value is automatically stored in ax register. In your void function that you have developped will not return anything but ax register will have value 13. so, your if statement will be true but not false.

Another example of function return behave:

#include <stdio.h>

using namespace std;
 
int a()
{
	if(0)
		return 1;  //compiler wont reach here
	printf("Hello World!\n");
 //function end without returning. compiler would create a simple warning.
}
 
int main()
{
	printf("%d\n",a()); //can you guess what it would print?
}





答案是main函数中的printf函数会打印13.因为在()函数中的printf函数会输出13个字符。所以它返回13.保留在AX寄存器中。在主函数中由printf函数打印



为什么?



the answer is the printf function in the main function would print 13. because printf function in in a() function prints 13 character. so it return 13. which remain in AX register. which is printed by printf function in the main function

Why?


由于代码未定义,因此没有意义解释它。



将函数指针转换为另一种类型并将其用作它是非法的。结果未定义,因此不可移植。在实践中,结果将特定于编译器,甚至可能根据选项而变化。



未定义仅表示结果未定义且可以是任何内容。这将取决于编译器的实现方式,在某些情况下甚至可能导致崩溃,因为有许多调用约定,其中一些调用者调整堆栈和其他被调用者。



通常,因为寄存器用于小型返回值,所以转换后的结果将是寄存器中剩余的任何内容,通常用于返回int的函数。



由于void函数返回notthing,它通常对存储在寄存器中的值没有任何作用,并且在转换后,结果将是该寄存器中已有的任何内容(可能是之前函数调用的最后结果)。 />


如果您真的想了解箍下发生的事情,请查看反汇编代码窗口。
Gived that the code is undefined, it is pointless to explain it.

It is illegal to cast a function pointer to another type and use it as it. The result is not defined and thus not portable. In practice, the result will be specific to a compiler and might even vary according to options.

Undefined simply means that the result is not defined and can be anything. It will depends on how the compiler is implemented and it might even cause crashes in some cases as there are many calling conventions some of which the caller adjust the stack and other the callee.

Typically since a register is used for small type return value, the result after a cast would be whatever is remainding in the register that is usually used for function returning an int.

Since a void function returns notthing, it generally does nothing on the value stored in the register and after a cast, the result will be whatever was already in that register (might be last result of previous function call).

If you really want to understand what is happening under the hoop, then look at the disassembly code window.


您需要更改这样的函数:



you need to change the function a like this:

int a()
{
	printf("Hello World!\n");
	return 1;
}





如果您需要解释,请告诉我



Just let me know if you need an explanation


这篇关于混淆这个C ++编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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