【问题标题】:Expanding the size of an array of strings in c在c中扩展字符串数组的大小
【发布时间】:2018-08-19 10:49:19
【问题描述】:

我正在尝试从用户那里获取字符串,然后如果他们输入一个长字符串,则扩展字符串的大小,如果他们输入的字符串比预期的多,我还会扩展保存字符串的数组的大小。这是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>

int main(int argc, char* argv[]) 
{
    int number_of_strings = 5;
    int string_size = 5;
    int count = 0;
    char **array = (char**)calloc(number_of_strings, sizeof(char*));
    for (int i = 0; i < number_of_strings; i++)
    {
        array[i] = (char*)calloc(string_size + 1, sizeof(char));
    }

////////////////////////////MAIN PART///////////////////////////////////////////////
    int arr_size = number_of_strings;
    int str_count = 0;                          //Total number of input strings counter

    for (int j = 0; j < arr_size; j++)
    {
        if (arr_size >= str_count)              //Check if the number of input strings is more than expected
        {
            array = (char**)realloc(array, (arr_size + 1) * sizeof(char*));     //allocate memory for 1 more string
            arr_size++;                         //Increase the loop rounds
        }

        int str_size = string_size; 
        int char_count = 0;                     //Total number of input characters counter

        for (int h = 0; h < str_size; h++)
        {
            if (str_size >= char_count)         //Check if the input string size is more than expected
            {
                array[j] = (char*)realloc(array[j], (str_size + 1) * sizeof(char));     //allocate memory for 1 more char
                str_size++;                     //Increase the loop rounds
            }
            scanf(" %c", &array[j][h]);         //get a single char
            char_count++;                       //Increment the total input character count
        }
        str_count++;                            //Increment the total input string count
    }
////////////////////////////////////////////////////////////////////////////////////

    for (int k = 0; k < number_of_strings; k++)
    {
        printf("%s", array[k]);
        free(array[k]);
    }
    free(array);

    return 0;
}

输入:这不是看起来的样子,但我不知道为什么会这样
输出:空标准输出。超过时间限制
预期输出:这不是它看起来的样子,但我不知道为什么会发生这种情况

程序等待用户输入很长时间,即使用户没有输入任何输入,它也不会停止扫描输入,因此最终程序崩溃。

我认为错误是由于数组重新分配不当引起的。非常感谢任何关于为什么会导致此错误以及如何修复它的想法。谢谢!

【问题讨论】:

  • 将其减少到 只是有问题的部分minimal reproducible example。我们不是来调试这样的完整程序的。并且不要从malloc() 转换返回值。

标签: c memory multidimensional-array memory-management


【解决方案1】:

目前尚不清楚您要做什么。以下是代码中的一些问题:

  • 您故意忽略输入中的空格,那么如何检测字符串边界?
  • 所有输入都集中在一个字符串中,内部循环永远不会停止。
  • 您也不检查 EOF:如果遇到输入流的结尾,则循环继续,重新分配内存,直到系统崩溃或分配失败...
  • 分配失败也未经过测试,因此在所有情况下都未定义行为。
  • 没有必要在 C 中强制转换 malloccalloc 的返回值。在 C++ 中这是必需的,但您肯定希望在 C++ 中使用不同的方法。为避免类型差异,您可以使用目标指针的类型而不是对其进行硬编码:要重新分配数组,请将 array = (char**)realloc(array, (arr_size + 1) * sizeof(char*)); 替换为

    array = realloc(array, sizeof(*array) * (arr_size + 1)); 
    

【讨论】:

  • ...并且不要转换 calloc 和 realloc 的结果。
  • @ThomasPadron-McCarthy:好点,但与之前的相比相对较小。
  • 感谢您的建议解决了问题。
猜你喜欢
  • 2020-10-18
  • 2014-04-24
  • 2012-12-27
  • 1970-01-01
  • 2023-03-07
  • 2015-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多