adriano
adriano

Reputation: 11

problem with memory alocation using realloc function in C

I've a problem with this function. This function reallocs the size of the array 'music' when its needed. But a execution error occurred when i try call this function many times in a loop. its probably a memory allocation problem, but i cant figure this out.

 void adicionar( Musica** music , int* cont){//adiciona uma música à playlist na posição cont.
     int tam;//armazenará o tamanho do nome da música.
     char *str_aux;//receberá temporariamente o nome da música.
     if ((*cont) == 0){//aloca memória para o primeiro ponteiro para Musica.
         music[*cont] = (Musica*) malloc( sizeof (Musica));
         if( music[*cont] == NULL ){
         printf("Erro na alocacao de memoria!\n");
         exit(-1);
         }
     }
     else{//redimensiona o vetor a cada nova adição de música.
         music = (Musica**) realloc( music , ((*cont) + 1) * sizeof( Musica* ));
         music[*cont] = (Musica*) malloc( sizeof (Musica));
         if( music == NULL ){//verifica se o realloc foi bem sucedido.
            printf("Erro na alocacao de memoria!\n");
            exit(-1);
         }
         if( music[*cont] == NULL ){
            printf("Erro na alocacao de memoria!\n");
            exit(-1);
         }
         printf("%0x\n",music);
         printf("%d\n", music[(*cont) - 1]->tempo);
         //printf("%d\n",*cont - 1);
     }
     str_aux = (char*) malloc( 26*sizeof(char));
     printf("Musica: ");
     scanf("%25[^\n]", str_aux);
     fflush(stdin);
     tam = strlen( str_aux );
     printf("Tempo: ");
     scanf("%d", &music[*cont]->tempo);
     music[*cont]->nome = (char*) malloc(tam * sizeof(char));
     if( music[*cont]->nome == NULL ){
         printf("Erro na alocacao de memoria!\n");
         exit(-1);
     }
     strcpy( music[*cont]->nome , str_aux );
     printf("musica adicionada.\n\n");
     free( str_aux );
     (*cont)++;//incrementa cont para que ele aponte para a próxima posição do vetor caso queira adicionar mais músicas.
 }

Upvotes: 1

Views: 416

Answers (3)

Zan Lynx
Zan Lynx

Reputation: 54325

I believe the problem is that you pass the music pointer into the function and inside the function you realloc() the memory that music points to, but you never update the music pointer outside the function.

As you know, realloc() will often return a different pointer than was passed to it. It needs to do this when it needs to move the memory block.

To solve this problem you need to either return the music pointer from this function, or you need to pass a Musica*** and use *music everywhere inside the function.

Upvotes: 1

codaddict
codaddict

Reputation: 454920

Looks like you are not allocating memory for the terminating NUL character.

Currently you are doing:

tam = strlen( str_aux );
...
music[*cont]->nome = (char*) malloc(tam * sizeof(char));
...
strcpy( music[*cont]->nome , str_aux );

You need to allocate memory as:

music[*cont]->nome = malloc(tam+1);

Upvotes: 2

Alex Reynolds
Alex Reynolds

Reputation: 96927

It is possible you do not end up with a null-terminated string:

music[*cont]->nome = (char*) malloc(tam * sizeof(char));
...
strcpy( music[*cont]->nome , str_aux );

and this might be causing problems somewhere else.

Upvotes: 0

Related Questions