【问题标题】:C Program that stores an unknown number of strings of unknown sizes from user - Heap block at 00558068 modified at 00558096 past requested size of 26存储来自用户的未知数量的未知大小字符串的 C 程序 - 00558068 处的堆块在 00558096 处修改,请求大小为 26
【发布时间】:2014-08-26 22:29:56
【问题描述】:

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

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

但是,我在 for 循环的第二次迭代中一直遇到错误,在行 strings = (char**) realloc (strings, index+1);我收到错误消息:00558068 处的堆块在 00558096 处修改,请求大小为 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 ); 
    }

}

如前所述,堆块发生在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 

如果有人能向我解释这个错误的原因和修复它的方向,我将不胜感激。谢谢。

【问题讨论】:

  • 你应该做一些调试来找出原因。
  • 这个:*(strings + index) = (char*) malloc(sizeof(char)); 的用处高度值得怀疑。您的get_string 应该返回其获取的字符串的动态地址,并且应该在您的指针数组中分配所述地址(其索引同样高度有问题)。祝你好运。
  • 你看过 C++ 吗?诸如 std::vector 和 std::string 这样的类使得编写这种代码几乎是微不足道的。
  • 1)fgets( strings, 1, stdin ); :基本上无效,因为它不保存输入。
  • 2)strings = (char*) realloc( strings, strlen(strings)+2 ); :实质上是无效的,因为它没有反映在调用者中。

标签: c string heap-memory block realloc


【解决方案1】:
#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.
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-11
    • 2013-04-15
    • 2022-06-14
    • 2019-06-30
    • 2017-07-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多