c - 运行时检查失败 #2 - 变量 'start' 周围的堆栈已损坏

标签 c string function unicode

我从函数readString中得到了上述调试问题。我相信这与函数中定义“start”的方式有关。数组中的 0x07 根据后续字符串的长度而变化。该字符串在 unicode 中应为“testing”。

int main(){
    char readbuffer[] = {0x07, 0x74, 0x65, 0x73, 0x74, 0x69, 0x6e, 0x67};
    char *buf = readbuffer;
    uint32_t *stringread = (uint32_t *) malloc(sizeof(uint32_t));
    *stringread = readString(buf);
    }

    uint32_t readString(char *buf)
    {
         uint32_t *start = (uint32_t *) malloc(sizeof(uint32_t));
         int len;

         len = protobuf_readVarint(buf, &buf);
         memcpy (&start, buf, len);
         buf += len;

         return start;

    }

最佳答案

彼得回答了你的问题。另外我建议你使用

len = max(protobuf_readVarint(buf, &buf),sizeof(uint32_t));

或者当第一个参数大于第二个参数时捕获,否则你会在参数开始处写入太多内容。另外,您还存在内存泄漏,可以通过以下方式修复:

uint32_t readString(char *buf)
{
     uint32_t start;
     int len;

     len = max(protobuf_readVarint(buf, &buf),sizeof(uint32_t));
     memcpy ((void*)&start, buf, len);
     buf += len;

     return start;

}

关于c - 运行时检查失败 #2 - 变量 'start' 周围的堆栈已损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7067170/

相关文章:

C程序删除字符串中重复的字符

string - VBA 字符串清理

Java - 字符串不变性和数组可变性

c - c中的错误函数返回

python - 如何查询数据框两列的值

c - 如何在没有特殊库的情况下读取 C 中的原始图像?

c - 选择最大数并对其进行平方或立方时结果不正确

c - 递归删除C中的目录

java - 不兼容的类型 : void cannot be converted to string

java - 为什么我不能将浮点二维数组从函数返回到主类?