程序崩溃-创建新字符数组时分配错误 我有一个C++函数,当遇到一个分隔符时,它将一个字符数组拆分成多个字符数组。出于某种原因,在保存第三个拆分数组时,程序会崩溃,有时会返回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; }

程序崩溃-创建新字符数组时分配错误 我有一个C++函数,当遇到一个分隔符时,它将一个字符数组拆分成多个字符数组。出于某种原因,在保存第三个拆分数组时,程序会崩溃,有时会返回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; },c++,arrays,crash,char,bad-alloc,C++,Arrays,Crash,Char,Bad Alloc,我曾尝试在代码块内调试这一行,但由于某些原因,在使用断点和跟踪变量时,应用程序工作正常,执行正确。它只在正常运行时崩溃,没有调试 还要注意的是,除了fstream和iostream之外,我不能使用字符串或几乎任何库 编辑:我尝试将新字符[garums]部分更改为新字符[100],它神奇地开始工作。问题是,我随后将其更改为新字符[10],在这种情况下,所有内容都仍然有效。我甚至将保存的文本输出到控制台,它正确地保存了所有内容。它怎么能在10个字符长的字符数组中保存大字?我正在测试的单词长度超过10

我曾尝试在代码块内调试这一行,但由于某些原因,在使用断点和跟踪变量时,应用程序工作正常,执行正确。它只在正常运行时崩溃,没有调试

还要注意的是,除了fstream和iostream之外,我不能使用字符串或几乎任何库

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


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

这可能与char*类型有关&而不是char*类型。 另一方面,我从来没有像这样编程C++,你确定这不是C?使用像“新”这样的显式内存管理就像玩俄罗斯轮盘赌一样好

这里有一个更标准的C++方式:

#include <vector>
#include <string>
#include <iostream>

std::vector<std::string> splitString(std::string& str, char c) {
  std::vector<std::string> substrings;
  while(true) {
    unsigned pos = str.find(c);
    substrings.push_back(str.substr(0,pos));
    if(pos == std::string::npos) break;
    str = str.substr(pos+1);
  }
  return substrings;
}

int main()
{
  char c = '*';
  std::string str = "Some*string*that we need to split*properly*";
  std::vector<std::string> result = splitString(str,c);

  for(unsigned i = 0; i < result.size(); ++i) {
    std::cout << i << ": " << result[i] << "\n";
  }
}

这可能与char*类型而不是char*类型有关。 另一方面,我从来没有像这样编程C++,你确定这不是C?使用像“新”这样的显式内存管理就像玩俄罗斯轮盘赌一样好

这里有一个更标准的C++方式:

#include <vector>
#include <string>
#include <iostream>

std::vector<std::string> splitString(std::string& str, char c) {
  std::vector<std::string> substrings;
  while(true) {
    unsigned pos = str.find(c);
    substrings.push_back(str.substr(0,pos));
    if(pos == std::string::npos) break;
    str = str.substr(pos+1);
  }
  return substrings;
}

int main()
{
  char c = '*';
  std::string str = "Some*string*that we need to split*properly*";
  std::vector<std::string> result = splitString(str,c);

  for(unsigned i = 0; i < result.size(); ++i) {
    std::cout << i << ": " << result[i] << "\n";
  }
}

由于我不知道您有什么输入数据,我不得不猜测:

这里分配指针数组,但请注意,所有指针都未初始化

字符串=新字符*[计数] 然后,在解析代码时,使用一个变量curr,让它自由运行,因此无法确定所有字符串[]是否都已设置为某个值,或者curr是否位于大于count的数字上

如果我是你,我会进行检查以确保:

货币不超过计数


b如果curr落在小于count的值上,则将其余指针设置为nullptr

,因为我不知道您有什么输入数据,我将不得不猜测:

这里分配指针数组,但请注意,所有指针都未初始化

字符串=新字符*[计数] 然后,在解析代码时,使用一个变量curr,让它自由运行,因此无法确定所有字符串[]是否都已设置为某个值,或者curr是否位于大于count的数字上

如果我是你,我会进行检查以确保:

货币不超过计数


如果curr落在一个小于count的值上,则将其余指针设置为nullptr

您在问题中提到的错误可能在尝试使用指针时出现 指向从explode函数返回的指针。 一些指针;如果必须编写C代码,不要使用C/C++的混搭, 使用库函数,而不是在copystring中重新创建wheel strncpy 你的字数是错的,因为你没有考虑字数 最后一个分隔符和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) ;
}

您在问题中提到的bug可能是在尝试使用指针时出现的 指向从explode函数返回的指针。 一些指针;如果必须编写C代码,不要使用C/C++的混搭, 使用库函数,而不是在copystring中重新创建wheel strncpy 你的字数是错的,因为你没有考虑字数 最后一个分隔符和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) ;
}

您可能希望在每个新字符之前跟踪您的garums[garums]时执行一次,我认为您对要执行的操作的想法是错误的。您正在接收一个一维数组。在我看来,你的结果应该是一个二维数组。结果必须是一个字符数组。如果我是你,我会1迭代输入字符串并存储每个分隔符的计数和位置,2将子字符串复制到二维结果数组中。是的,它是一个二维数组,因为它将一维数组拆分为多个一维数组-一个二维数组。这也是函数正在做的事情,只是因为某些原因,它在尝试初始化数组时崩溃。您可能希望在每个新字符[garums]之前跟踪garums时执行。我认为您对要执行的操作的想法是错误的。你收到的是一张昏暗的照片
全方位阵列。在我看来,你的结果应该是一个二维数组。结果必须是一个字符数组。如果我是你,我会1迭代输入字符串并存储每个分隔符的计数和位置,2将子字符串复制到二维结果数组中。是的,它是一个二维数组,因为它将一维数组拆分为多个一维数组-一个二维数组。这也是函数的作用,只是因为某种原因,当初始化数组时它会崩溃。我几乎可以使用,所以没有向量和字符串。回到你的老师这里说:SIR,C++不是C,而是额外的位。它们是两种不同的语言,碰巧共享一些关键字和语法规则。如果在C++作业中做C样式字符串操作,那么您可能会被解雇。在进行更简单、更高级别的操作之前,最好先了解更基本、更低级的操作。他们不教这个,所以我在工作时使用字符数组,他们教这个,所以我理解编程,并且可以轻松地在语言之间移动。另外,还有什么比动态字符数组更好的学习指针和内存管理的方法,就像弹吉他之前先学弹钢琴一样。这可能是有用的,但如果你开始练习吉他,停止弹钢琴,你会成为一个更好的吉他手。你夸张了,C和C++没有那么不同,至少在这个水平上是这样。这实际上就像是在学着走路,在学习跑步之前。我几乎可以被允许使用,所以没有矢量和线。回到你的老师面前,说:“先生,C++不是C,而是额外的位。”它们是两种不同的语言,碰巧共享一些关键字和语法规则。如果在C++作业中做C样式字符串操作,那么您可能会被解雇。在进行更简单、更高级别的操作之前,最好先了解更基本、更低级的操作。他们不教这个,所以我在工作时使用字符数组,他们教这个,所以我理解编程,并且可以轻松地在语言之间移动。另外,还有什么比动态字符数组更好的学习指针和内存管理的方法,就像弹吉他之前先学弹钢琴一样。这可能是有用的,但如果你开始练习吉他,停止弹钢琴,你会成为一个更好的吉他手。你夸张了,C和C++没有那么不同,至少在这个水平上是这样。这实际上就像学习走路,然后再学习跑步。