C++ 在DLL中静态创建的数组通过调用程序被覆盖
我有一个主程序PMAIN,它使用动态链接库。假设PMAIN使用DLL导出的两个函数foo1、foo2和foo3。函数如下所示:C++ 在DLL中静态创建的数组通过调用程序被覆盖,c++,dll,memory-management,overwrite,C++,Dll,Memory Management,Overwrite,我有一个主程序PMAIN,它使用动态链接库。假设PMAIN使用DLL导出的两个函数foo1、foo2和foo3。函数如下所示: int __stdcall foo1(){ str = new MyStruct; } int __stdcall foo2(){ str.LC->objF1(); } int __stdcall foo3(int val){ str.LC->objF2(val); } struct MyStruct{ MyObject LC } My
int __stdcall foo1(){
str = new MyStruct;
}
int __stdcall foo2(){
str.LC->objF1();
}
int __stdcall foo3(int val){
str.LC->objF2(val);
}
struct MyStruct{
MyObject LC
}
MyStruct是这样的:
int __stdcall foo1(){
str = new MyStruct;
}
int __stdcall foo2(){
str.LC->objF1();
}
int __stdcall foo3(int val){
str.LC->objF2(val);
}
struct MyStruct{
MyObject LC
}
我的对象是:
class MyObject{
private:
int arr1[100];
int arr2[100];
int Index;
public:
MyObject();
~MyObject();
void objF1();
void objF2(int val);
}
void MyObject::objF1(){
for(int i=0;i<100;i++){
arr1[i]=1;
}
}
void MyObject::objF2(int val){
Index++;
arr2[Index]=val;
}
MyObject::MyObject(){
for(int i=0;i<100;i++){
arr1[i]=0;
arr2[i]=0;
}
Index=-1;
}
从PMAIN开始,我先调用foo1,然后调用foo2,然后循环调用foo3 100次;数组arr1在PMAIN的整个执行过程中得到正确更新并“保留”值,而每次我调用foo3时,数组arr2只包含零,它得到更新,当程序再次调用foo3时,它再次为空。PMAIN如何覆盖DLL中数组的地址,我在运行调试时看到了这种行为
你知道这怎么可能吗
PMAIN和DLL不应该在内存中的两个不同位置吗?在DLL中创建的变量与程序使用的变量相同,因此如果程序中有错误,它可以覆盖它们 这在我看来是错误的:
int __stdcall foo1(){
str = new MyStruct;
}
int __stdcall foo2(){
str.LC->objF1();
}
int __stdcall foo3(int val){
str.LC->objF2(val);
}
如果str是一个指针,因为你是新的,那么你需要使用操作符->来访问它的成员。像这样:
int __stdcall foo1(){
str = new MyStruct;
}
int __stdcall foo2(){
str->LC.objF1();
}
int __stdcall foo3(int val){
str->LC.objF2(val);
}
由于LC不是指针,而是结构中的一个自动变量,因此需要使用。操作人员如上所述。对不起,你是对的,我颠倒了操作员->和操作员的位置。在写文章的时候,但是在源代码中他们就像你说的。顺便说一句,PMAIN从来没有做过这样的事情:arr2[n]=0;但是arr2[n]指出的值changes@Daniele:您的问题不完全清楚,因此我缺少重要信息,请在您的问题中添加信息,以便我们了解我们正在查看的内容。