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];

解决方案 »

  1.   

    Utility::shortToBytes(mh.Command_ID,sCommand_ID);这里会不会有问题?越界?
      

  2.   

    这个函数时正常的阿!在其他的方法里面可以使用,我本来是用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
    是正常的都进行过测试的