1.h中部分代码:
void *smooth1(void *smarg);//线程函数声明
typedef struct{
LPSTR lpNewDIBBits;
FLOAT * fpArray;//fpArray为指向一个FLOAT类型的数组
LONG lWidth;
}smstruct;1.cpp中的部分代码:
smstruct *argm1=new smstruct;//smstruct为一结构类型
argm1->lpNewDIBBits=lpNewDIBBits;//为结构中各成员赋值
argm1->fpArray=fpArray;//这样赋值对吗?fpArray为指向FLOAT类型的指针
argm1->lWidth=lWidth;
CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)smooth1,(LPVOID)argm1,NULL,NULL);1.cpp中的smooth1()函数:
void *smooth1(LPVOID smarg)
{
smstruct *smarg1=(smstruct *)smarg;
............................
smarg1->fpArray[0];//传递的值不正确
delete smarg1;
return 0;
}设置断点发现在smooth1()函数中fpArray指向的数组传递的值不对,怎么修改呢,谢谢!!!!
void *smooth1(void *smarg);//线程函数声明
typedef struct{
LPSTR lpNewDIBBits;
FLOAT * fpArray;//fpArray为指向一个FLOAT类型的数组
LONG lWidth;
}smstruct;1.cpp中的部分代码:
smstruct *argm1=new smstruct;//smstruct为一结构类型
argm1->lpNewDIBBits=lpNewDIBBits;//为结构中各成员赋值
argm1->fpArray=fpArray;//这样赋值对吗?fpArray为指向FLOAT类型的指针
argm1->lWidth=lWidth;
CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)smooth1,(LPVOID)argm1,NULL,NULL);1.cpp中的smooth1()函数:
void *smooth1(LPVOID smarg)
{
smstruct *smarg1=(smstruct *)smarg;
............................
smarg1->fpArray[0];//传递的值不正确
delete smarg1;
return 0;
}设置断点发现在smooth1()函数中fpArray指向的数组传递的值不对,怎么修改呢,谢谢!!!!
要象上面那样才行,释放可以在
线程函数中delete[](smarg1->fpArray);
主函数funcmain()调用func1()
funcmain()
{
FLOAT aValue[10];
for(int i=0;i<10;i++)
aValue[i]=1.0;
func1(aValue);
................
}在1.cpp中函数func1():func1(FLOAT * fpArray)//func1为子函数
{
smstruct *argm1=new smstruct;//smstruct为一结构类型
argm1->lpNewDIBBits=lpNewDIBBits;//为结构中各成员赋值
argm1->fpArray=fpArray;//这样赋值对吗?fpArray为指向FLOAT类型的指针
argm1->lWidth=lWidth;
CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)smooth1,(LPVOID)argm1,NULL,NULL);
}
这样没错吧?但传给smooth1()的fpArray[0]就不对了。怎么回事?
这样肯定不行的。
float aValue = new float[10];
这样就可以了。
传局部变量给线程函数一定要小心。
除非你能保证线称函数在使用该局部变量的时候它还有效。
这样是肯定不行的。float aValue = new float[10];
是一种方法
或者你在头文件里定义FLOAT aValue[10];
也是可以的。
和
float *aValue = new float[10];
不都是局部变量马?
funcmain()
{
FLOAT *aValue=new FLOAT[10];
for(int i=0;i<10;i++)
aValue[i]=1.0;
func1(aValue);
................
}在1.cpp中函数func1():func1(FLOAT * fpArray)//func1为子函数
{
smstruct *argm1=new smstruct;//smstruct为一结构类型
argm1->lpNewDIBBits=lpNewDIBBits;//为结构中各成员赋值
argm1->fpArray=fpArray;//这样赋值对吗?fpArray为指向FLOAT类型的指针
argm1->lWidth=lWidth;
CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)smooth1,(LPVOID)argm1,NULL,NULL);
}
1.cpp中的smooth1()函数:
void *smooth1(LPVOID smarg)
{
smstruct *smarg1=(smstruct *)smarg;
............................
smarg1->fpArray[0];
deletet [] smarg1->fpArray;
delete smarg1;
return 0;
}
FLOAT aValue[10];
和
float *aValue = new float[10];
不都是局部变量马?
-----------------------------------------------------
aVaule本身都是局部变量,但是第二个aValue指向的内存分配在堆上。
都是局部变量,除了他们堆栈不同外,还有什么区别呢 ?