c++ - 程序崩溃 - 创建新的 char 数组时出现 bad_alloc

标签 c++ arrays crash char bad-alloc

我有一个 C++ 函数,它在遇到分隔符时将一个 char 数组拆分为多个 char 数组。出于某种原因,当保存第三个拆分数组时,程序会崩溃,有时会返回 std::bad_alloc 异常。

char ** explode(const char * arr, const char delim) {
int start, end, curr=0, count=1;
char ** strings;
//Iegūst explodēto stringu skaitu
for (int i = 0; arr[i] != 0; i++) {
    if (arr[i] == delim && i != 0 && arr[i+1] != 0 && arr[i+1] != delim ) { //Nav pirmais, nav pēdējais, nav pa labi vēlviens delimiters
        count++;
    }
}
strings = new char*[count];
start = 0;
for (int i = 0; arr[i] != 0; i++) {
    if (arr[i] == delim || arr[i+1] == 0) {
        if (arr[i] == delim) {
            end = i;
        } else {
            end = i+1;
        }
        if (end-start < 1) {
            start++;
        } else {
            copystring(arr,strings[curr++],start,end-start);
            start = i+1;
        }
    }
}
for (int i = 0; i < count; i++) {
    cout << strings[i] << endl;
}

return strings;
}

//Pārkopē daļu no pirmā char masīva uz otru, no START pozīcijas, līdz GARUMS garumā
void copystring(const char * from, char *& to, const int start, const int garums) {
    int curr=0;
    if (garums < 1 || start > charlen(from)) {
        return;
    }
    to = new char[garums];
    for (int i = start; i < start+garums && from[i] != 0; i++) {
        to[curr++] = from[i];
    }
    to[curr] = 0;
}

这很难说,因为它并没有真正告诉我在哪一行出错了,但我认为它发生在

to = new char[garums];

我试过在 CodeBlocks 中调试这一行,但出于某种原因,当使用断点和跟踪变量时,应用程序工作正常并正确执行。只有正常运行才会崩溃,没有调试...

另请注意,除了 fstream 和 iostream 之外,我不能使用字符串或几乎任何库。

编辑:我尝试将 new char[garums] 部分更改为 new char[100] 并且它神奇地开始工作了。问题是我随后将其更改为 new char[10],在这种情况下一切仍然有效。我什至将保存的文本输出到控制台,它正确地保存了所有内容。它怎么能在 10 个字符长的字符数组中保存大字(我正在测试的字长于 10 个字符)?但是,当我将其更改为 new char[1] 时,它再次开始崩溃,但仅在第 3 次循环迭代之后再次崩溃。所以它以某种方式将前 2 个单词保存在 1 个字符长的数组中?

EDIT2:现在它神奇地开始工作,甚至可以使用 new char[garums]。这里确实有问题,有人有什么想法吗?

最佳答案

您在问题中提到的错误可能会在尝试使用指针时突然出现 指向从 explode 函数返回的指针。 一些指针;如果您必须编写 C 代码,请不要使用 C/C++ 的大杂烩, 使用库函数而不是重新发明轮子(copystring 中的 strncpy) 你的字数统计不对,因为你没有考虑到 between 之间的字 最后一个分隔符和 EOL 作为一个完整示例,以下是对您的代码的一些小改动:

#include <stdio.h>
#include <strings.h>

void copystring(const char *from, char **to, const int numchars)
{
    if (numchars > 0) {
            *to = new char[numchars];
            strncpy(*to, from, numchars) ;
            (*to)[numchars] = '\0' ;
    }
}

char **explode(const char * buffer, const char delim)
{
    int count = 0 ;

    if (strlen(buffer) > 0) {
            int inword = 0 ;
            int idx = 0 ;
            do {
                    if (buffer[idx] == delim || buffer[idx] == '\0') {
                            if (inword == 1) {
                                    count++ ;
                                    inword = 0 ;
                            }
                    } else {
                            inword = 1 ;
                    }
            } while (buffer[idx++] != 0) ;
    }

    int start = 0;
    int end = 0 ;
    int curr = 0 ;
    int idx = 0 ;

    char **values = new char*[count+1];

    do {
            if (buffer[idx] == delim || buffer[idx] == '\0') {
                    end = idx;
                    if (end-start > 0) {
                            copystring(&buffer[start], &values[curr++], end - start) ;
                    }
                    start = ++end ;
            }
    } while (buffer[idx++] != 0) ;

    values[curr] = NULL ;
    for (int idx = 0; idx < count; idx++) {
            fprintf(stdout, "'%s'\n", values[idx]) ;
    }

    return values;
}

int main(int argc, char *argv[])
{
    char inputstr[] = "The, quick, brown, fox, jumped, over,,, ,,, ,,,,, ,,,, the, lazy, dog's, back" ;
    char **values = explode(inputstr, ',') ;

    while (*values != NULL) {
            fprintf(stdout, "%s\n" , *values) ;
            *values++ ;
    }

    return (0) ;
}

关于c++ - 程序崩溃 - 创建新的 char 数组时出现 bad_alloc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23324959/

相关文章:

c++ - 何时不使用 RAII 进行资源管理

c - 为什么在数组 a[] 中,&a 和 a 相同?

c - 取消引用指向结构成员的不完整类型错误的指针

button - 单击显示按钮时应用程序崩溃,没有红色代码

android - 在 Android Studio 中添加图像时应用程序崩溃

c++ - Visual C++ 中的去虚拟化

C++ 迭代器比较断言失败

c++ - 在MinGW的Shell中编译libtcod时出现 “Error 1”

php - 将 mysql 调用加载到数组中

Android 应用程序在 OS 层崩溃