存储来自用户的未知数量、未知大小的字符串的 C 程序 - 00558068 处的堆 block 在 00558096 处修改,过去请求的大小为 26

标签 c string heap-memory block realloc

我正在尝试用 C 语言编写一个程序,该程序从用户那里获取未知数量的字符串(每个字符串的大小未知)作为输入并存储它们,然后在用户完成输入字符串时打印它们。

首先,我使用一个指向字符指针(char** 字符串)的指针,并使用 malloc 为其分配 1 个 char* 大小的内存块。然后,我也使用 malloc 将 1 个 char 大小的 block 分配给 strings 指向的指针( (strings) )。从那里,我使用名为 get_String() 的用户定义函数获取用户输入的字符串,并将其放入 char* string 指向的 char 指针中。然后,我使用 for 循环继续将额外的 char* 内存分配给 char** 字符串,并将 1 个 char 内存分配给新指针。

但是,我在 for 循环的第二次迭代中,在 strings = (char**) realloc (strings, index+1); 行上不断遇到错误。我收到错误消息: Heap block at 00558068 modded at 00558096 Past request size of 26. 似乎我正在将分配的内存写入 char** 字符串,但我不知道在哪里或如何执行此操作。

这是我的完整代码:

#include "stdio.h"
#include "string.h"
#include "stdlib.h"

void get_strings( char* strings); // function to take in a string of an unknown size

int main( void )
{
    char** strings;
    int index, count;

    (strings) = (char**) malloc( sizeof(char*)); // this is the pointer that holds the addresses of all the strings
    *strings = (char*) malloc( sizeof(char)); // the address of the first string

    printf( "Enter a list of stringss. Quit by pressing enter without entering anything\n" );

    get_strings( *strings );

    for( index = 1; strlen(*(strings+index-1))!=1; index++) // stores strings from the user until they enter a blank line which is detected when string length is 1 for the \n
    {
        strings = (char**) realloc (strings, index+1); // adds an extra character pointer for another string
        *(strings + index) = (char*) malloc(sizeof(char)); // allocates memory to the new character pointer 


        get_strings( *(strings + index) ); // stores the string from the user
    } 

    printf( "You entered:\n" );

    for( count = 0; strlen(*(strings + count)) != 1; count++ ) //prints every string entered by the user except for the terminating blank line
    {
        printf( "%s", *(strings + count ) );
        free( *(strings + count ));
    }

    free( strings );


    system( "PAUSE" );
    return 0;
}

void get_strings( char* strings )
{
    fgets( strings, 1, stdin ); 

    while( strings[ strlen( strings ) - 1 ] != '\n' )
    {
        strings = (char*) realloc( strings, strlen(strings)+2 );
        fgets( strings + strlen(strings), 2, stdin ); 
    }

}

如前所述,堆 block 发生在执行以下行时 for 循环的第二次迭代: strings = (char**) realloc (strings, index+1);

for( index = 1; strlen(*(strings+index-1))!=1; index++) // stores strings from the user until they enter a blank line which is detected when string length is 1 for the \n
    {
        strings = (char**) realloc (strings, index+1); // error occurs here
        *(strings + index) = (char*) malloc(sizeof(char)); // allocates memory to the new character pointer 

如果有人可以向我解释此错误的原因以及修复它的方向,我将非常感激。谢谢。

最佳答案

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

char *get_strings(void);

int main( void ){
    char **strings = NULL;
    char *string;
    int index, count;

    printf( "Enter a list of stringss. Quit by pressing enter without entering anything\n" );

    for(index = 0; string = get_strings(); ++index){
        strings = (char**) realloc (strings, (index+1)*sizeof(*strings));
        strings[index] = string;
    }
    printf( "You entered:\n" );

    for( count = 0; count < index; ++count ){
        printf("%s\n", strings[count]);
        free( strings[count] );
    }
    free( strings );

    system( "PAUSE" );
    return 0;
}

char *get_strings(void){
    char *string = NULL;//or calloc(1,sizeof(char)) for return "" (test by caller: *string == '\0')
    int ch;
    size_t len = 0;

    while(EOF != (ch=fgetc(stdin)) && ch != '\n' ) {
        string = (char*)realloc( string,  len + 2);//To realloc to each character is inefficient
        string[len++] = ch;
    }
    if(string)
        string[len] = '\0';
    return string;//The value is NULL when there is no input substantial.
}

关于存储来自用户的未知数量、未知大小的字符串的 C 程序 - 00558068 处的堆 block 在 00558096 处修改,过去请求的大小为 26,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24593684/

相关文章:

c - Linux C,如何安排10个等待线程以FIFO方式执行?

创建和使用字体/避免 Windows GDI 中的内存泄漏

ruby-on-rails - 参数错误 "interning empty string"Rails

javascript - 正则表达式用jQuery中的特殊字符替换子字符串

java - 什么原因指定InitialHeapSize大于1m?

c - *(void **) (&funcp) 在这行涉及 dlsym() 的代码中做了什么?

c - makefile 总是调用最后一个目标

c# - 确定三个字段之一是否具有任何关键字列表

Java 堆空间(java.lang.OutOfMemoryError)

visual-studio - Visual Studio 2008 - 显示堆