cpp: 内存地址

在 Visual Studio 调试器中使用“内存”窗口(C#、C++、Visual Basic、F#)

在调试器中查看变量的内存 - Visual Studio (Windows) | Microsoft Learn

 

/// <summary>
	/// 冒泡排序函数
	/// int * arr 也可以写为int arr[]
	/// </summary>
	/// <param name="arr"></param>
	/// <param name="len"></param>
	void Geovin::bubbleSort(int* arr,int len)
	{
		//, int len
		//int len = sizeof(arr)/sizeof(int);
		for (int i = 0; i < len - 1; i++)
		{
			for (int j = 0; j < len - 1 - i; j++)
			{
				if (arr[j] > arr[j + 1])
				{
					int temp = arr[j];
					arr[j] = arr[j + 1];
					arr[j + 1] = temp;
				}
			}
		}
		//printArray(arr);
	}
	/// <summary>
	/// 打印数组函数
	/// </summary>
	/// <param name="arr"></param>
	/// <param name="len"></param>
	void Geovin::printArray(int arr[],int len)
	{
		//int len = sizeof(arr) / sizeof(int);
		for (int i = 0; i < len; i++)
		{
			cout << arr[i] << endl;
		}
	}

	int arrDu[5] = {1,9,25,5,8};
	int len = sizeof(arrDu) / sizeof(int);
	geovin.bubbleSort(arrDu, len);
	geovin.printArray(arrDu, len);

	cout << "冒泡排序:" << endl;
	for (auto d : arrDu)
	{
		cout << "" << d << "  ";
	}
	cout << endl;

  

 

	int a = 10;
	char b;
	bool c;
	string d;

	cout << "变量a的地址:" << &a << endl;
	cout << "变量b的地址:" << &b << endl;
	cout << "变量c的地址:" << &c << endl;
	cout << "变量d的地址:" << &d << endl;
	cout <<"a占用内存大小" << sizeof(a) << endl;
	cout << "变量b的内存地址:" << (long long)&b << endl;
	cout << "变量b的地址:" << (void*)&b << endl;
	cout << "变量b的地址:" << (int*)&b << endl;

	int du = 10;
	//1.变量地址赋值给指针变量	
	cout << "du的地址:" << &du << endl;//指向变量du的首字节的地址
	int* geovin2 = &du;
	cout << "geovin的指向" << geovin2 << endl;
	cout << "&geovin=" << &geovin2 << endl;//geovin自身的地址的编号
	cout << "*geovin=" << *geovin2 << endl;//指向geovin的值
	//2指针赋值给指针变量
	int* geovindu = geovin2;
	cout << "geovindu的指向" << geovindu << endl;
	cout << "&geovindu=" << &geovindu << endl;
	cout << "*geovindu=" << *geovindu << endl;

  

 

	/// <summary>
	/// 
	/// </summary>
	/// <param name="number1"></param>
	/// <param name="number2"></param>
	void Geovin::pointFunc(int* number1, int* number2)
	{
		int temp;
		temp = *number1;
		*number1 = *number2;
		*number2 = temp;
		cout << "number1=" << *number1 << endl;
		cout << "number2=" << *number2 << endl;
	}

	/// <summary>
	///显示变量的十六进制地址的函数:varname - 变量名,addr - 变量的地址。
	/// </summary>
	/// <param name="varname"></param>
	/// <param name="p"></param>
	void Geovin::func(string varname, void* p)//void *把其它类型的指针赋值给void*指针不需要转换
	{
		cout << varname << "的地址是" << p << endl;
	}

	/// <summary>
	/// 
	/// </summary>
	void Geovin::arryFunc()
	{

		int arr[] = { 1,2,3,4,5,6,7,8,9,10 };	
		int* p = arr;	
		
		cout<<"第一个元素" << arr[0] << endl;	
		cout << "P:=" << p << endl;
		cout << "指针访问第一个元素:" << *p << endl;
		for (int i = 0; i < 100; i++)
		{

			cout << *p << " ";
			p++;
		}				
		cout << endl;
	}

	/// <summary>
	/// 
	/// </summary>
	/// <param name="numberNO"></param>
	/// <param name="name"></param>
	void Geovin::constFunc(int numberNO, string name)
	{

		cout << "亲爱的" << name << "我是" << numberNO <<"号" << endl;
	}

	/// <summary>
	/// 显示变量的十六进制地址的函数:varname - 变量名,addr - 变量的地址。
	/// </summary>
	/// <param name="varname"></param>
	/// <param name="p"></param>
	void func(string varname, void* p)//void *把其它类型的指针赋值给void*指针不需要转换
	{
		cout << varname << "的地址是" << p << endl;
	}
	/// <summary>
	/// 
	/// </summary>
	void Geovin::diplayPoint()
	{

		int a = 10;
		char b;
		bool c;
		string d;

		cout << "变量a的地址:" << &a << endl;
		cout << "变量b的地址:" << &b << endl;
		cout << "变量c的地址:" << &c << endl;
		cout << "变量d的地址:" << &d << endl;
		cout << "a占用内存大小" << sizeof(a) << endl;
		cout << "变量b的内存地址:" << (long long)&b << endl;
		cout << "变量b的地址:" << (void*)&b << endl;
		cout << "变量b的地址:" << (int*)&b << endl;

		int du = 10;
		//1.变量地址赋值给指针变量	
		cout << "du的地址:" << &du << endl;//指向变量du的首字节的地址
		int* geovin2 = &du;
		cout << "geovin的指向" << geovin2 << endl;
		cout << "&geovin=" << &geovin2 << endl;//geovin自身的地址的编号
		cout << "*geovin=" << *geovin2 << endl;//指向geovin的值
		//2指针赋值给指针变量
		int* geovindu = geovin2;
		cout << "geovindu的指向" << geovindu << endl;
		cout << "&geovindu=" << &geovindu << endl;
		cout << "*geovindu=" << *geovindu << endl;

		//常量指针,语法:const 数据类型
		int aa = 3, bb = 8;
		const int* pp = &bb;
		const int* cc = &aa;
		cc = pp;
		// 
		// //2
		 //int* pp = &bb;
		 //*pp = 13;
		bb = 13;
		cout << "a=" << a << ",*p=" << *pp << endl;
	}

  

 

	Geovin geovin;
	//随机抽取字符串
	//geovin.DisplayRomd();

	//geovin.DisplayRomd(1);

	//显示十六进制地址
	int aaa;
	char bbb;
	//cout << "a的指针是" <<(void*)&a << endl;//void*表示强制用十六进制转换
	//cout << "b的指针是" <<(void*)&b << endl;
	geovin.func("a", &aaa);
	geovin.func("b", &bbb);

	geovin.arryFunc();


	int ddu = 10;
	int ddu2 = 50;

	geovin.pointFunc(&ddu, &ddu2);


	int add = 1;
	string namedd = "geovindu";

	geovin.constFunc(add,namedd);

  

 

posted @ 2023-05-07 11:12  ®Geovin Du Dream Park™  阅读(30)  评论(0编辑  收藏  举报