把数组当作指针传递时,发生以下错误

lifejoy 2003-11-25 11:46:54
Run-Time Check Failure #2 - Stack around the variable 'sCommand_ID' was corrupted.

byte* EMSP2_0::C_MSG_Head::getBytes()
{
byte sTotal_length[2];
Utility::shortToBytes(mh.Total_length,sTotal_length);
result[0]=sTotal_length[0];
result[1]=sTotal_length[1];
//delete sTotal_length;
byte sCommand_ID[2];
Utility::shortToBytes(mh.Command_ID,sCommand_ID);
result[2]=sCommand_ID[0];
result[3]=sCommand_ID[1];


result[4]=mh.Crypt;
byte sSeqNum[4];
Utility::intToBytes(mh.Seqnum,sSeqNum);
result[5]=sSeqNum[0];
result[6]=sSeqNum[1];
result[7]=sSeqNum[2];
result[8]=sSeqNum[3];

byte sUserNo[4];
Utility::intToBytes(mh.UserNo,sUserNo);
result[9]=sUserNo[0];
result[10]=sUserNo[1];
result[11]=sUserNo[2];
result[12]=sUserNo[3];
bsize=13;
return result;

}
result的定义为byte result[13];
...全文
64 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
lifejoy 2003-11-25
  • 打赏
  • 举报
回复
这个函数时正常的阿!在其他的方法里面可以使用,我本来是用vc6调试的,正确的,但是换成vc7的时候就出错了!换成bcb,和GNU C++也不成。
Utility是我自己写的一组函数,用于网络操作的,
/*
用于准换字节和整数,用于替换ms 的nltoh,hlton,nstoh,hston
函数增加代码的可用性

*/
#if !defined _UTILITY_H
#define _UTILITY_H
#pragma once


class Utility
{

public:
inline static void fillchar( byte* arr,char ch)//对字节数组清零
{ int size=sizeof(arr);
for(int i=0;i<size;i++)
{
arr[i]=ch;
}
}
inline static short bytesToShort( byte abyte[])//字节数组到字
{
int j;
int length=sizeof(abyte);
if(length <= 2)
j = length;
else
j = 2;
int i = 0;
for(int k = 0; k < j; k++)
i = (i << 8) + (abyte[k] & 0xff);

return i;
}
inline static int bytesToInt( byte abyte[])//字节数组到整数
{
int j;
int length=sizeof(abyte);
if(length <= 4)
j = length;
else
j = 4;
int i = 0;
for(int k = 0; k < j; k++)
i = (i << 8) + (abyte[k] & 0xff);

return i;
}

inline static byte* decodeMsgID( byte abyte[])//消息ID解码
{
byte* abyte1=new byte(20);
int i;
int length=sizeof(abyte);
if(length < 10)
i = length;
else
i = 10;
for(int j = 0; j < i; j++)
{
abyte1[j * 2] = (byte)((abyte[j] >> 4 & 0xf) + 48);
abyte1[j * 2 + 1] = (byte)((abyte[j] & 0xf) + 48);
}

return abyte1;
}

inline static byte* intToBytes(int i, byte* abyte0)//整数到字节转换
{

fillchar(abyte0,0);
abyte0[3] = ( byte)i;
i >>= 8;
abyte0[2] = i;
i >>= 8;
abyte0[1] = i;
i >>= 8;
abyte0[0] = i;
return abyte0;
}
inline static byte* shortToBytes(short i, byte* abyte0 )//字到字节转换
{

fillchar(abyte0,0);
abyte0[1] = ( byte)i;
i >>= 8;
abyte0[0] = i;

return abyte0;
}
inline static void exchange(byte* bytes,int len)
{
for(int i=0;i<len/2;i++)
{ byte temp=bytes[i];
bytes[i]=bytes[len-i-1];
bytes[len-i-1]=temp;
}
}
inline static void ZeroBytes(byte* bytes,int len)
{
for(int i=0;i<len;i++)
{
bytes[i]=0;
}
}
inline static int HToNL(const int value)
{ byte theb[4];
Utility::ZeroBytes(theb,4);
int nl=0;
memcpy(theb,&value,4);
Utility::exchange(theb,4);
memcpy(&nl,theb,4);

return nl;


}
inline static int NToHL(int nl)
{
byte theb[4];
Utility::ZeroBytes(theb,4);
int hl=0;
memcpy(theb,&nl,4);
Utility::exchange(theb,4);
memcpy(&hl,theb,4);

return hl;

}

};
#endif
是正常的都进行过测试的
wangweintk 2003-11-25
  • 打赏
  • 举报
回复
Utility::shortToBytes(mh.Command_ID,sCommand_ID);这里会不会有问题?越界?
wangweintk 2003-11-25
  • 打赏
  • 举报
回复
调一下,看那里停下来?

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

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