簡體   English   中英

帶字符串數組C ++的段錯誤

[英]Segfault with String array C++

在C語言中有此程序,並在我學習該語言時嘗試將其轉換為C ++。 基本上將char數組轉換為字符串以及一些輸入/輸出。 唯一的問題是,當嘗試將輸入字符串放入字符串數組時,出現了段錯誤(test2打印,而test3沒有)。

有任何想法嗎? 我在學習c ++時應注意的任何不良編碼習慣?

int main() {
int nstr=0, nchar=0, nint=0, nfloat=0;
string input;
int i, z=0;
float inputFloat;

string *strList = (string*)malloc(sizeof(string) * nstr);
char *charList = (char*)malloc(sizeof(char) * nchar);
int *intList = (int*)malloc(sizeof(int) * nint);
float *floatList = (float*)malloc(sizeof(float) * nfloat);

while (z != -42) {
    cout << "Input:  ";
    cin >> input;
    cin.ignore();

    inputFloat = strtof(input.c_str(), NULL);

    if (inputFloat) {
        if (fmod(inputFloat, 1.0)) {
            nfloat++;
            floatList = (float*)realloc(floatList, sizeof(float) * nfloat);
            floatList[nfloat-1] = inputFloat;
        }
        else {
            nint++;
            intList = (int*)realloc(intList, sizeof(int) * nint);
            intList[nint-1] = (int)inputFloat;
        }
    }
    else {
        if (input.length() == 1) {
            nchar++;
            charList = (char*)realloc(charList, sizeof(char) * nchar);
            if (input.at(0) == 10)
                input = " ";
            charList[nchar-1] = input.at(0);
        }
        else {
            nstr++;
            cout << "test1" << endl;
            strList = (string*)realloc(strList, sizeof(string) * nstr);
            cout << "test2" << endl;
            strList[nstr-1] = input;
            cout << "test3" << endl;
        }
    }

    cout << "Integers: ";
    for (i=0; i<nint; i++)
        cout << intList[i] << " ";

    cout << endl << "  Floats: ";
    for (i=0; i<nfloat; i++)
        cout << floatList[i] << " ";

    cout << endl << "   Chars: ";
    for (i=0; i<nchar; i++)
        cout << charList[i] << " ";

    cout << endl << " Strings: ";
    for (i=0; i<nstr; i++)
        cout << strList[i] << " ";
    cout << endl << endl;
}
}

通常,即使可以,也不要在c ++中完全使用malloccallocrealloc等。 它對於諸如int,char等簡單項目的意義不大,但是在對象(如std::string )上使用時,會引起此類問題:

當此行運行時:

string *strList = (string*)malloc(sizeof(string) * nstr);

您將存儲分配給字符串數組,但沒有調用任何構造函數,因此分配的所有存儲仍然無用。

在c ++中,您必須像這樣使用new

string *strList = new string[nstr];

它更短,更容易並且調用每個分配對象的構造函數。

最后,您可以使用delete []取消它,如下所示:

delete [] strList;

更好的是使用:

vector<string> strList;

並使用以下元素添加元素:

strList.push_back("something"); strList.push_back(some_string);

vector s會注意內存的分配和釋放,並且會在使用壽命結束時作為常規對象自動釋放,因此根本不需要刪除它。

realloc重新分配了比上一個更大的數組。 在數組末尾的新元素,將其填充為整數,float和char,它們是基本的C類型。 對於字符串之類的C ++對象,數組中的最后一個元素不是新字符串,一種可能性是創建字符串指針數組。

在代碼的開頭

string **strList = (string**)malloc(sizeof(string *) * nstr);

在代碼末尾,在數組末尾分配一個新的字符串對象。

nstr++;
strList = (string**)realloc(strList, sizeof(string *) * nstr);
strList[nstr-1] = new string(input);

在程序末尾,您必須刪除通過new運算符創建的所有內容,並通過malloc / realloc。

while (nstr--)
{
   delete strList[nstr];
}
free(strList);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM