积极答复者
一个函数的问题

问题
-
chang("1234");
//函数
unsigned char* chang(char *str)
{
unsigned char m_pu8[3];//我的理解这里声明3个空间应该够用了,但是执行到函数最后就提示m_pu8越界,一定要空间大于等于5时才不报错,不知道哪有问题,请告诉详细指点下
memset(m_pu8,0x00,sizeof(m_pu8));
int i = 0;
char m_stru8[3];
while(0x00 != *str)
{
memset(m_stru8,0x00,sizeof(m_stru8));
memcpy_s(m_stru8,2,str,2);
sscanf_s(m_stru8,"%x",&m_pu8[i],1);
str += 2;
i++;
}
cout<<i<<endl;
m_pu8[i] = 0x00;
return m_pu8;
}
答案
-
cout<<i<<endl;输出来的i的值是多少?
另外您最后return m_pu8;返回的是局部变量的地址,也是有问题的。Visual C++ enthusiast, like network programming and driver development. At present is being engaged in the WinCE/Windows Mobile platform embedded development.
- 已标记为答案 Helen Zhao 2012年5月25日 4:53
- 取消答案标记 Helen Zhao 2012年5月25日 4:54
- 已建议为答案 Helen Zhao 2012年5月25日 4:54
- 已标记为答案 Helen Zhao 2012年5月31日 6:46
-
unsigned char mstr[3]; //此处空间为5的时候就不报错了。run-time check failure #2 - stack around the variable 'mstr' was corrupted.
chang("1234",mstr);
bool chang(char *str,unsigned char * m_pu8)
{
int i = 0;
char m_stru8[3];
while(0x00 != *str)
{
memset(m_stru8,0x00,sizeof(m_stru8));
memcpy_s(m_stru8,2,str,2);
sscanf_s(m_stru8,"%x",&m_pu8[i],1);
str += 2;
i++;
}
cout<<i<<endl; //2 == i;
m_pu8[i] = 0x00;
return true;
}
- 已建议为答案 Helen Zhao 2012年5月25日 4:53
- 已标记为答案 Helen Zhao 2012年5月31日 6:46
全部回复
-
cout<<i<<endl;输出来的i的值是多少?
另外您最后return m_pu8;返回的是局部变量的地址,也是有问题的。Visual C++ enthusiast, like network programming and driver development. At present is being engaged in the WinCE/Windows Mobile platform embedded development.
- 已标记为答案 Helen Zhao 2012年5月25日 4:53
- 取消答案标记 Helen Zhao 2012年5月25日 4:54
- 已建议为答案 Helen Zhao 2012年5月25日 4:54
- 已标记为答案 Helen Zhao 2012年5月31日 6:46
-
unsigned char mstr[3]; //此处空间为5的时候就不报错了。run-time check failure #2 - stack around the variable 'mstr' was corrupted.
chang("1234",mstr);
bool chang(char *str,unsigned char * m_pu8)
{
int i = 0;
char m_stru8[3];
while(0x00 != *str)
{
memset(m_stru8,0x00,sizeof(m_stru8));
memcpy_s(m_stru8,2,str,2);
sscanf_s(m_stru8,"%x",&m_pu8[i],1);
str += 2;
i++;
}
cout<<i<<endl; //2 == i;
m_pu8[i] = 0x00;
return true;
}
- 已建议为答案 Helen Zhao 2012年5月25日 4:53
- 已标记为答案 Helen Zhao 2012年5月31日 6:46