1

構造体内のポインターに free を使用しようとしていますが、構造体で free を使用しています。正しいようですが、次のエラーが発生しました: free(): invalid next size (fast); エラー メッセージの方が大きいですが、エラー メモリに関するものであることがわかるにはこれで十分だと思います。

これが私のコードです:

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

    typedef struct{
        char *nome;
        char *endereco;
        char *matricula;
    }Estudante;

    char *strAlloc( int n ){
        return ( char *) malloc( n * sizeof( char * ));
    }

    int lineReader( char **str ){

        char c;
        int length = 10, strI = 0;

        getchar();

        *str = strAlloc( length );
        while ( 1 ){

            c = getchar();

            if ( strI+1 == length ){

                length += 10;

                *str = ( char * ) realloc( *str, length * sizeof( char *));

                if( *str == NULL){
                    printf("Faltou memoria\n");
                    return 0;
                }
            }
            if ( c == '\n' ) break;

            (*str)[ strI++ ] = c;
        }

        return strI;
    }

    int main(){



        Estudante *alunos;
        int n, length, i, j;
        char *str;
        char c;

        printf("Digite a quantidade de alunos:\n");
        scanf(" %d", &n);

        alunos = ( Estudante * ) malloc( n * sizeof( Estudante * ));

        for (i = 0; i < n; ++i){

            printf("Digite o nome do aluno %d:\n", i+1);

            length = lineReader ( &str );

            alunos[ i ].nome = strAlloc( length );
            strcpy( alunos[ i ].nome, str );

            printf("Digite o endereço do aluno:\n");

            length = lineReader ( &str );
            alunos[ i ].endereco = strAlloc( length );
            strcpy( alunos[ i ].endereco, str );

            printf("Digite a matricula do aluno:\n");

            length = lineReader ( &str );
            alunos[ i ].matricula = strAlloc( length );
            strcpy( alunos[ i ].matricula, str );

            printf("\n");
        }

        free( str );

        for (i = 0; i < n; ++i){

            printf("Dados do aluno %d\n", i+1);
            printf("Nome: %s\n", alunos[ i ].nome );
            printf("Endereço: %s\n", alunos[ i ].endereco );
            printf("matricula: %s\n\n", alunos[ i ].matricula );

            free( alunos[ i ].nome );
            free( alunos[ i ].endereco );
            free( alunos[ i ].matricula );
        }

        free( alunos );
        alunos = NULL;

        return 0;
    }
4

2 に答える 2

0

構造体を解放するための適切な形式があります。他の何かが以前にヒープを破損したに違いありません。

lineReader で文字列が null で終了していないようです

if ( c == '\n' ) break;

する必要があります

if ( c == '\n' ) {
(*str)[strI] = '\0'; //Not incrementing strI because the null character doesn't count towards the string size
break;
}

null で終了していないため、strcpy などの文字列関数は想定以上に取得している可能性があります。利用可能な場合は、strncpy の使用を検討してください。

また、free(str) はこの for ループ内にあるはずですよね? lineReader() は、古い文字列を解放することなく、毎回新しい文字列を割り当て続けます。

for (i = 0; i < n; ++i){

        printf("Digite o nome do aluno %d:\n", i+1);

        length = lineReader ( &str );

        alunos[ i ].nome = strAlloc( length );
        strcpy( alunos[ i ].nome, str );

        printf("Digite o endereço do aluno:\n");

        length = lineReader ( &str );
        alunos[ i ].endereco = strAlloc( length );
        strcpy( alunos[ i ].endereco, str );

        printf("Digite a matricula do aluno:\n");

        length = lineReader ( &str );
        alunos[ i ].matricula = strAlloc( length );
        strcpy( alunos[ i ].matricula, str );

        printf("\n");
    }

    free( str );
于 2013-09-28T04:22:12.147 に答える