简体   繁体   English

使用C9.io和C ++的堆栈粉碎错误

[英]Stack Smashing Error using C9.io and C++

I am getting a Stack Smashing Error using the c9 compiler, and I looked up reasons for the error, which is supposedly buggy code/passing variables or something that isn't declared/initialized, but I cant find the error in my code... 我正在使用c9编译器收到堆栈粉碎错误,并且我查找了错误原因,该原因可能是错误的代码/传递变量或未声明/初始化的内容,但我在代码中找不到错误。 。

I've stripped the following code of any unnecessary code that doesn't pertain to the error, and this is what I have: 我删除了以下与该错误无关的不必要代码,这就是我所拥有的:

#include <iostream>
#include <cstring>

using namespace std;

void checkWord(char *word, int size);

int main () {
    string word1 = "racecar";
    char wordArr[sizeof(word1)];
    strcpy(wordArr, word1.c_str());
    checkWord(wordArr, strlen(wordArr));

    string word2 = "something";
    char word2Arr[sizeof(word2)];
    strcpy(word2Arr, word2.c_str());
    checkWord(word2Arr, strlen(word2Arr));
}

void checkWord(char *wordArr, int size) {
    // cout << "Size1: " << size << endl;
    int workingSize;
    if ((size % 2) != 0) {
        workingSize = (size +1)/2;
    } else {
        workingSize = size/2;
    }
    char *p, *q;
    p = wordArr;
    q = wordArr+strlen(wordArr)-1;
    bool pal = true;
    for (int i = 0; i < workingSize; i++) {
        if (*p != *q) {
            pal = false;
        }
        p++;
        q--;
    }
}

Which gives me this error output: 这给了我这个错误输出:

*** stack smashing detected ***:/home/ubuntu/workspace/.c9/metadata/workspace/3_16Lab2.cpp.o terminated
bash: line 12: 48077 Aborted                 $file.o $args


Process exited with code: 134

Otherwise, the program executes without any issue and, in this instance, confirms whether or not the words "racecar" and "something" are palindromes or not. 否则,程序将毫无问题地执行,并且在这种情况下,将确认“ racecar”和“ something”一词是否是回文。

You declare char wordArr[sizeof(word1)]; 您声明char wordArr[sizeof(word1)]; which is one char short. 这是一个短字符。 Sizeof gives the number of characters in the string, but you need one more for the trailing '\\0' . Sizeof给出了字符串中的字符数,但是结尾的'\\0'还需要一个字符数。

As a consequence, you start overwriting memory that you don't own, and anything beyond that is random undefined behavior. 结果,您将开始覆盖您不拥有的内存,除此之外的任何事情都是随机的未定义行为。

sizeof(word1) returns the number of bytes the variable word1 occupies, which has nothing to do with the length of the string it represents. sizeof(word1)返回变量word1占用的字节数,与它表示的字符串的长度无关。 You should use word1.length() + 1 to get the length of the string, including the terminating nul character. 您应该使用word1.length() + 1来获取字符串的长度,包括终止的nul字符。 (Same of sizeof(word2) .) (与sizeof(word2)相同。)

A better solution would be to either change checkWord to take its first parameter as a const char *wordArr , and pass the word in using word1.c_str() , or just pass the string to checkWord and not use character pointers at all. 更好的解决方案是更改checkWord使其第一个参数为const char *wordArr ,并使用word1.c_str()传递单词,或者仅将字符串传递给checkWord而不使用字符指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM