Search code examples
cstructmemory-leaksdynamic-memory-allocationc-strings

Pointers and all that [C]


well, I'm trying to create a simple program that simply print the name of a list of students, the thing is,I do all that's required but when I try to print all the elements come out as the same. It's in portuguese but I don't think it should be a problem.

Here is the program:

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

typedef struct struct_t{

   char * nome;
   int * notas; 


}DADOS_ALUNOS;

typedef struct struct_s{

    DADOS_ALUNOS total_alunos;


}ALUNOS;

int main (){

         
    ALUNOS * Classe;
    DADOS_ALUNOS aluno;
    int numero_alunos, quantidade_provas, i;


    printf("Digite a quantidade de alunos\n");
    scanf("%d", &numero_alunos);
    printf("Agora, digite a quantidade de provas dadas\n");
    scanf("%d", &quantidade_provas);

    printf ("Total numeoro alunos: %d\n", numero_alunos);

    Classe = (ALUNOS *) malloc (numero_alunos * sizeof(ALUNOS));
    aluno.nome = (char *) malloc (sizeof(char) * 20);
    
    for (i = 0; i < numero_alunos; i++){

        printf("Digite o nome do aluno\n");
        scanf("%s", aluno.nome);

       //printf("O que está contindo na variavael temporaria: %s", aluno.nome);
        
        Classe[i].total_alunos.nome = (char *) malloc (sizeof(aluno.nome));
        Classe[i].total_alunos.nome = aluno.nome;
        //printf("O que está contindo na variavael : %s", Classe[i].total_alunos.nome);
        //free(aluno.nome);

    }

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

        printf("Nome: %s\n", Classe[i].total_alunos.nome);

    }



    return 0;
}

any suggestions are welcome.


Solution

  • In these statements:

        Classe[i].total_alunos.nome = (char *) malloc (sizeof(aluno.nome));
        Classe[i].total_alunos.nome = aluno.nome;
    

    there are produced memory leaks.

    At first memory was allocated and its address was assigned to the pointer Classe[i].total_alunos.nome and then the pointer was reassigned with the address of the memory allocated before the for loop:

        Classe[i].total_alunos.nome = aluno.nome;
    

    At least you need to use the string function strcpy like:

        strcpy( Classe[i].total_alunos.nome, aluno.nome );
    

    provided that you allocated enough space for the array pointed to by the pointer Classe[i].total_alunos.nome because in this allocation

    Classe[i].total_alunos.nome = (char *) malloc (sizeof(aluno.nome));
    

    you allocating memory only for pointer. That is sizeof(aluno.nome ) is equal to sizeof( char * ).

    However pay attention to that there will be still a memory leak because the memory pointed to by the pointer aluno.nome was not freed.

    There is no great sense to declare an object of the type DADOS_ALUNOS:

    DADOS_ALUNOS aluno;
    

    and then dynamically allocate memory:

    aluno.nome = (char *) malloc (sizeof(char) * 20);
    

    Instead you could declare just a character array as for example:

    char nome[20];
    

    and use this array in the for loop:

    scanf( " %19[^\n]", nome );
           ^^^^^^^^^^