【问题标题】:malloc() for an array of strings, not working as hopingmalloc() 用于字符串数组,不能像希望的那样工作
【发布时间】:2018-08-31 16:38:55
【问题描述】:

我正在尝试创建一个函数,该函数接受一个字符串和一个指向字符串数组的指针和malloc() char 数组的数组,并复制字符串的每个单词。这就是我到目前为止所拥有的,我想我已经接近了,我只是在努力在数组数组上使用malloc()

int string_parser(char *inp, char **array_of_words_p[])
{
    int CurrentChar = 0;                //Variable Initialization
    char *buffer;                       //Variable Initialization

    /* Allocate memory and check for errors allocating memory */
    //Allocate memory to buffer the size of the input string
    buffer = (char*)malloc(strlen(inp));    
    if (buffer == NULL)
    {
        printf("Error allocating memory..\n");
        return -1;
    }

    /* Move input string into buffer before processing */
    for (CurrentChar = 0; CurrentChar < strlen(inp) + 1; CurrentChar++)
    {   //For every character in input
        if (inp != NULL)                                                    
        {
            //Move input character into buffer
            buffer[CurrentChar] = inp[CurrentChar];     
        }
    }

    /* Convert string into array of words */
    char ** stringbuffer = NULL;        

    //Convert string to array of words
    char *  CurrentWord = strtok_s(buffer, " ", *array_of_words_p);     

    //Variable Initialization
    int numspaces = 0;

    while (CurrentWord)                                                 
    {
        //Allocate memory for size of string
        stringbuffer = (char**)realloc(stringbuffer, sizeof(char**) * ++numspaces); 
        if (stringbuffer == NULL)                                       
        {
            return -1;
        }
        stringbuffer[numspaces - 1] = CurrentWord;

        //Reset Current word to null        
        CurrentWord = strtok_s(NULL, " ", *array_of_words_p);       
    }

    //Reallocate memory to include terminating character
    stringbuffer = (char**)realloc(stringbuffer, sizeof(char**) * (numspaces + 1)); 
    stringbuffer[numspaces] = 0;                                        

    /* Write processed data into returned argument */
    *array_of_words_p = (char**)malloc(sizeof(char**) * (numspaces + 2));           
    memcpy(*array_of_words_p, stringbuffer, (sizeof(char*) * (numspaces + 2))); 

    free(stringbuffer);                                             
    return numspaces;                                                   
}

【问题讨论】:

  • 请添加 C/C++ 标签
  • 问题/问题是什么?

标签: c arrays string malloc buffer


【解决方案1】:
    //Allocate memory to buffer the size of the input string
    buffer = (char*)malloc(strlen(inp));    

输入字符串的大小包括终止符\0,所以你需要:

    buffer = malloc(strlen(inp)+1);
    //Convert string to array of words
    char *  CurrentWord = strtok_s(buffer, " ", *array_of_words_p);     

滥用*array_of_words_p 作为上下文保存变量是不明智的,因为这需要对其进行适当的初始化。更好:

    char *context, *CurrentWord = strtok_s(buffer, " ", &context);
    …
        CurrentWord = strtok_s(NULL, " ", &context);
        //Allocate memory for size of string
        stringbuffer = (char**)realloc(stringbuffer, sizeof(char**) * ++numspaces); 

这可能没有什么坏处(因为指针大小相等),但严格来说sizeof(char**) 是错误的,因为字符串数组 的元素是char * 类型。正确:

        stringbuffer = realloc(stringbuffer, sizeof (char *) * ++numspaces);
    …
    stringbuffer = realloc(stringbuffer, sizeof (char *) * (numspaces + 1));
    /* Write processed data into returned argument */
    *array_of_words_p = (char**)malloc(sizeof(char**) * (numspaces + 2));           
    memcpy(*array_of_words_p, stringbuffer, (sizeof(char*) * (numspaces + 2))); 

    free(stringbuffer);                                             

您可以省去这种不必要的复制,顺便说一下,通过将上面的内容替换为以下内容来避免访问未分配的内存stringbuffer[numspaces+1]

    *array_of_words_p = stringbuffer;

除此之外,您的函数可以正常工作并且可以这样调用:

    char **array_of_words;
    int n = string_parser("this here is an example string", &array_of_words);
    for (int i = 0; i < n; ++i) puts(array_of_words[i]);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-05
    • 2014-12-30
    • 1970-01-01
    相关资源
    最近更新 更多