Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将指针数组作为空指针传递到c++; 我目前正在做一个项目,我必须为C++的DLL构建一个shell,所以一个新的C语言GUI可以使用它的函数。 但是,我遇到了以下问题,在C++部分中,我必须根据特定原因创建新线程,并且我想将int数组传递给新线程。 请注意,在发生这种情况的函数中,分配给数组的值是从代码的C#部分获得的 __declspec( dllexport ) void CreateReportPane(int &id, int &what) { DWORD threadId; int iArray[2] = { id, what}; HANDLE hThread = CreateThread( NULL, 0, CreateReportPaneThread, iArray, 0, &threadId); if (hThread == NULL) { ExitProcess(3); } }_C++_Arrays_Multithreading_Pointers_Void - Fatal编程技术网

将指针数组作为空指针传递到c++; 我目前正在做一个项目,我必须为C++的DLL构建一个shell,所以一个新的C语言GUI可以使用它的函数。 但是,我遇到了以下问题,在C++部分中,我必须根据特定原因创建新线程,并且我想将int数组传递给新线程。 请注意,在发生这种情况的函数中,分配给数组的值是从代码的C#部分获得的 __declspec( dllexport ) void CreateReportPane(int &id, int &what) { DWORD threadId; int iArray[2] = { id, what}; HANDLE hThread = CreateThread( NULL, 0, CreateReportPaneThread, iArray, 0, &threadId); if (hThread == NULL) { ExitProcess(3); } }

将指针数组作为空指针传递到c++; 我目前正在做一个项目,我必须为C++的DLL构建一个shell,所以一个新的C语言GUI可以使用它的函数。 但是,我遇到了以下问题,在C++部分中,我必须根据特定原因创建新线程,并且我想将int数组传递给新线程。 请注意,在发生这种情况的函数中,分配给数组的值是从代码的C#部分获得的 __declspec( dllexport ) void CreateReportPane(int &id, int &what) { DWORD threadId; int iArray[2] = { id, what}; HANDLE hThread = CreateThread( NULL, 0, CreateReportPaneThread, iArray, 0, &threadId); if (hThread == NULL) { ExitProcess(3); } },c++,arrays,multithreading,pointers,void,C++,Arrays,Multithreading,Pointers,Void,问题出现在新线程中,我可以可靠地从数组中获取第一个值,但第二个值似乎已释放,下面是另一侧的代码 DWORD WINAPI CreateReportPaneThread(LPVOID lparam) { int id, what; id = *(( int * )lparam); what = *(((int *)lparam)+1) ; CreateReportPaneOriginal(id, what); return 0; } 是否有任何方法可以

问题出现在新线程中,我可以可靠地从数组中获取第一个值,但第二个值似乎已释放,下面是另一侧的代码

DWORD WINAPI CreateReportPaneThread(LPVOID lparam)
{
    int id, what;
    id = *(( int * )lparam);
    what = *(((int *)lparam)+1) ; 
    CreateReportPaneOriginal(id, what);

    return 0;
}
是否有任何方法可以防止数组中的值在不保留原始线程的情况下被释放?
事先非常感谢

CreateReportPane()退出时,动态分配数组以防止数组超出范围:

int* iArray = new int[2];
iArray[0] = id;
iArray[1] = what;    

否则,线程正在访问不再有效的数组,这是未定义的行为。当不再需要数组时,线程例程
CreateReportPaneThread()
必须
delete[]
该数组(注意使用
delete[]
而不是
delete
)。

CreateReportPane()
退出时,动态分配数组以防止数组超出范围:

int* iArray = new int[2];
iArray[0] = id;
iArray[1] = what;    
否则,线程正在访问不再有效的数组,这是未定义的行为。当不再需要数组时,线程例程
CreateReportPaneThread()
必须
delete[]
该数组(注意使用
delete[]
而不是
delete

问题在于,
iArray
是一个本地数组,这意味着当函数
CreateReportPane()
返回时,它会被破坏。因此,
CreateReportPaneThread()
引用的内容不存在。你只是碰巧得到了第一个值。没有这样的保证,你会得到甚至第一个值

使用动态数组:

int * iArray  = new int[2];
iArray[0] = id;
iArray[1] = what;

HANDLE hThread = CreateThread(...,CreateReportPaneThread, iArray, ...);
CreateReportPaneThread
中使用完数组后,请记住编写并释放该数组:

DWORD WINAPI CreateReportPaneThread(PVOID *data)
{
     int *array = static_cast<int*>(data);

     int id = array[0], what = array[1];

     delete []array; //MUST DO IT to avoid memory leak!

     //rest of your code
}
DWORD WINAPI CreateReportPaneThread(PVOID*数据)
{
int*数组=静态_转换(数据);
int id=array[0],what=array[1];
删除[]数组;//必须这样做以避免内存泄漏!
//代码的其余部分
}
问题在于,
iArray
是一个本地数组,这意味着当函数
CreateReportPane()
返回时,它会被破坏。因此,
CreateReportPaneThread()
引用的内容不存在。你只是碰巧得到了第一个值。没有这样的保证,你会得到甚至第一个值

使用动态数组:

int * iArray  = new int[2];
iArray[0] = id;
iArray[1] = what;

HANDLE hThread = CreateThread(...,CreateReportPaneThread, iArray, ...);
CreateReportPaneThread
中使用完数组后,请记住编写并释放该数组:

DWORD WINAPI CreateReportPaneThread(PVOID *data)
{
     int *array = static_cast<int*>(data);

     int id = array[0], what = array[1];

     delete []array; //MUST DO IT to avoid memory leak!

     //rest of your code
}
DWORD WINAPI CreateReportPaneThread(PVOID*数据)
{
int*数组=静态_转换(数据);
int id=array[0],what=array[1];
删除[]数组;//必须这样做以避免内存泄漏!
//代码的其余部分
}

别忘了关闭句柄(hThread),否则会泄漏句柄和内存。我可能会为此创建一个结构,但如果要使用数组,为什么不在线程中使用数组表示法?例如,
int*iArray=(int*)lparam;int id=iArray[0];int what=iArray[1]不要忘记关闭句柄(hThread),否则会泄漏句柄和内存。我可能会为此创建一个结构,但是如果您想使用数组,为什么不在线程中使用数组表示法呢?例如,
int*iArray=(int*)lparam;int id=iArray[0];int what=iArray[1]啊,是的,当然,非常感谢大家,它解决了我的问题@wyndesmit:如果它解决了你的问题,你应该通过点击勾号来接受这个“答案”。啊,是的,我还是这个界面的新手:)啊,是的,当然,非常感谢大家,它解决了我的问题@wyndesmit:如果它解决了你的问题,你应该通过点击勾号来接受这个“答案”。啊,是的,我还是这个界面的新手:)