c++ - 错误 :Stack around the variable 'hexStr' was corrupted in c++

标签 c++ visual-c++

我想使用 AES 算法。

我用这个功能就可以了

void SubBytes(char *SArr[4][4]){
int r,c;
char xlook[2];char ylook[2]; char hexStr[2]; char *Pad;
int intxlook,intylook;
char *temp;
int subbyte;
Pad = (char *)malloc (sizeof(char));
temp = (char *)malloc (sizeof(char));

for (r = 0; r < 4; r++)
  for(c = 0; c < 4; c++){
        strcpy(temp,"");
        strcpy(xlook,"");strcpy(ylook,"");
        strcpy(temp,SArr[r][c]);
        xlook[0] = temp[0]; xlook[1] = '\0';
        ylook[0] = temp[1];ylook[1] = '\0';
        intxlook = string_to_dec(xlook);
        intylook = string_to_dec(ylook);

     subbyte = SBox(intxlook,intylook);
     itoa(subbyte,hexStr,16);

     if (strlen(hexStr) < 2){
        strcpy(Pad,"");
        Pad = Padding(0,1);
        strcat(Pad,hexStr);  
        strcpy(SArr[r][c],Pad);
     }
     else
        strcpy(SArr[r][c],hexStr);

  }

  //free(temp); free(Pad);
}     //end 

但是我得到了错误

变量“hexStr”周围的堆栈在 c 中已损坏

最佳答案

您忘记了空终止 hexStr

所以当你执行 strcpy(SAr[r][c],hexStr); 时。它会尝试从 hexStr 复制到 SArr[r][c],直到到达 '\0',这可能在任何地方。

关于c++ - 错误 :Stack around the variable 'hexStr' was corrupted in c++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11518303/

相关文章:

c++ - 为什么 memory_order_relaxed 在 x86 上使用原子(锁前缀)指令?

c++ - 如何找到内存泄漏的位置?

visual-studio - 解决 "LINK : fatal error LNK1561: the entry point must be defined"

OpenCV 物体检测教程

c - 检测msvc标准库

c++ - 使用 std::forward 时,为什么我可以在传入右值时调用左值引用函数?

c++ - 调试自定义小部件

c++ - 在类构造函数中调用命名 lambda 与调用实际私有(private)函数的优点

c++ - 在取消引用的类中取消指向仿函数的指针

c++ - 继承单例