C++笔记-初步窥探全局函数在Debug、Release的地址

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq78442761/article/details/102325444

目录

 

 

前言

代码与实例


 

前言

这里做个笔记,此笔记最后的结论是在各个编译器或各种优化中,函数地址也会被编译器优化。个人觉得搞技术这一块,学着学着就有了交集,在此记录下,没准以后还真有机会接触到反汇编这种项目呢。

代码与实例

如下面的代码:

#include <iostream>
using namespace std;

void test1(){

	cout << "This is test1() called!" << endl;
	cout << "1111111" << endl;
	int a = 1,b = 2, c = 3, d = 4;
	cout << a + b +3 +d;
}

void test2(char *str){

	char buf[128];
	strcpy(buf, str);
	cout << "This is test2() called : " << buf << endl;
}

void test3(){

	cout << "test3" << endl;
}

void test4(){

	cout << "test4" << endl;
}

void test5(){

	cout << "test5" << endl;
}

int main(int *argc, int *argv[]){

	cout << "test1:" << test1 << endl;
	cout << "test2:" << test2 << endl;
	cout << "test3:" << test3 << endl;
	cout << "test4:" << test4 << endl;
	cout << "test5:" << test5 << endl;

	test3();
	test4();

	test2("Hello world!");

	getchar();
	return 0;
}

这里是Debug版本:编译器是MSVC2012:

运行结果如下:

这里看看:从test1到test5:全局函数不像类里面的函数那样,具有规律。这种全局函数看起来是没有什么规律的。

下面来看看Release版本:

这里可以看到使用Realse版本优化后的全局函数地址,是有某种规律的,至少地址是增加的,不像debug那样!

猜你喜欢

转载自blog.csdn.net/qq78442761/article/details/102325444