可以把int保存的内存地址转型成vector吗?

fu15229018 2013-03-19 05:29:02
下面的例子是是把p函数的地址保存到整型变量里,然后再转型成函数来访问。

举个例子:

int p(int a,int b)
{
return ((a>b)?a:b);
}


int main()
{
printf("%x\n",p);

int n=(int)&p;
printf("%d\n",n);

int (*ptr)(int,int);
ptr=(int(*)(int,int))n;
int c=ptr(5,6);
printf("%d\n",c);

return 0;
}


那么我就想,能不能把vector<string>的内存地址保存到一个整型变量里,然后利用这个整型变量转成vector<string>后,拿到vector里的数据呢?
比如:

int main()
{
vector<string> vc;
vc.push_back("123");
int n=&vc;
vector<string>* vs=(vector<string>*)n;
vs->push_back("456");

return 0;
}



再或者不用整型变量,用其它的C标准类型变量是不是可以?
新手,虚心求教。
...全文
158 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2013-03-20
  • 打赏
  • 举报
回复
电脑内存只是一个一维二进制字节数组及其对应的二进制地址; 人脑才将电脑内存中的这个一维二进制字节数组及其对应的二进制地址的某些部分看成是vector<string>、整数、有符号数/无符号数、浮点数、复数、英文字母、阿拉伯数字、中文/韩文/法文……字符/字符串、函数、函数参数、堆、栈、数组、指针、数组指针、指针数组、数组的数组、指针的指针、二维数组、字符点阵、字符笔画的坐标、黑白二值图片、灰度图片、彩色图片、录音、视频、指纹信息、身份证信息……
图灵狗 2013-03-20
  • 打赏
  • 举报
回复
应该是可以的。
引用 3 楼 fu15229018 的回复:
引用 2 楼 turingo 的回复:C/C++ code ? 12345678910111213141516171819 #include <iostream> #include <vector> using namespace std; int main(void) { vector<string> vc; vc.push_b……
赵4老师 2013-03-20
  • 打赏
  • 举报
回复
崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处。 判断是否越界访问,可以在数组的最后一个元素之后对应的地址处设置数据读写断点。如果该地址对应其它变量干扰判断,可将数组多声明一个元素,并设置数据读写断点在该多出元素对应的地址上。
fu15229018 2013-03-20
  • 打赏
  • 举报
回复
回复5、6楼: 当我传pv指针到dll中的函数test(void* pVector)后,在这个函数里,pVector指针貌视无效了。 因为我在传之前push_back了一个数据,然而在test(void* pVector)里第一行,输出pVector->size()大小时竟然是0,按理来说应该是1才对。 当我把pVector指针转型成vector<string>*时vector<string>* vs = (vector<string>*)p; 再利用vs->push_back("123456"); 结果报下面的错误: Windows has triggered a breakpoint in TestDll.exe. This may be due to a corruption of the heap, which indicates a bug in TestDll.exe or any of the DLLs it has loaded. This may also be due to the user pressing F12 while TestDll.exe has focus. The output window may have more diagnostic information.
fu15229018 2013-03-19
  • 打赏
  • 举报
回复
接三楼的问题,并在test(void* pVector)里对vector<string>进行push_back添加数据。
fu15229018 2013-03-19
  • 打赏
  • 举报
回复
引用 2 楼 turingo 的回复:
C/C++ code ? 12345678910111213141516171819 #include <iostream> #include <vector> using namespace std; int main(void) { vector<string> vc; vc.push_back("123"); void* p = (void*……
如果我现在在dll中有一个函数test(void* pVector),里面接收一个void*通用类型形参,然后传一个vector<string> vc;中的void* pv=(void*)&vc; 把这个pv传到dll中的test(void* pVector)里,可吗?
图灵狗 2013-03-19
  • 打赏
  • 举报
回复

#include <iostream>
#include <vector>
using namespace std;

int main(void)
{
	vector<string> vc;
	vc.push_back("123");
	void* p = (void*)&vc;

	vector<string>* vs = (vector<string>*)p;
	vs->push_back("456");

	cout << vs->at(0) << endl;
	cout << vs->at(1) << endl;

	return 0;
}
乔巴好萌 2013-03-19
  • 打赏
  • 举报
回复
这种通用类型 可以用void *保存地址

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧