Allocating text dynamically, and error in free ()

1

I'm trying to allocate dynamically, but the error is appearing:

  

Error in ./name.c; invalid pointer: 0x0000000001c18834

My code:

// FUNCAO // 
char *nome(text[])  
{    
    int n3 = 0;  
    int n2 = 0;     
    char *n = NULL;  

    while((n2 = getchar()) != '
// FUNCAO // 
char *nome(text[])  
{    
    int n3 = 0;  
    int n2 = 0;     
    char *n = NULL;  

    while((n2 = getchar()) != '%pre%' && n2 != EOF)  
    {  
        n3++;  
        n = realloc(n, n3*sizeof(char));  
        if(n == NULL)  
        {  
            puts("Erro ao realocar!");  
            exit(0);  
            free(n);  
        }   
        *(n+(n3-1)) = n2;   
    }  
    *(n+n3) = '%pre%';  
    return n;
}

// USANDO //   
int main(void)  
{  
    char *name = nome("Nome:");  
    while(*name != '%pre%')  
    printf("%c", *name++);    
    free(name); // aqui ta o problema, sem o free roda normal agora com o free me da esse erro
}        
' && n2 != EOF) { n3++; n = realloc(n, n3*sizeof(char)); if(n == NULL) { puts("Erro ao realocar!"); exit(0); free(n); } *(n+(n3-1)) = n2; } *(n+n3) = '%pre%'; return n; } // USANDO // int main(void) { char *name = nome("Nome:"); while(*name != '%pre%') printf("%c", *name++); free(name); // aqui ta o problema, sem o free roda normal agora com o free me da esse erro }
    
asked by anonymous 19.06.2017 / 22:00

1 answer

2

The code has some problems, even it is not readable. I simplified it a little. The most serious problem is that it saves% po% of pointer to the allocated location. It can not be modified so that it can then be given name in the bandstand location. When the increment is going to try to free memory in place that there was no allocation and generates the error. The correct thing is to create an independent iteration variable and increase it, so do not mess with free .

I have not seen any other issues.

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

char *nome(char text[]) {    
    int n3 = 0;
    int n2 = 0;
    char *n = NULL;
    while((n2 = getchar()) != '
#include <stdio.h>
#include <stdlib.h>

char *nome(char text[]) {    
    int n3 = 0;
    int n2 = 0;
    char *n = NULL;
    while((n2 = getchar()) != '%pre%' && n2 != EOF) {
        n = realloc(n, ++n3);
        if (n == NULL) {
            puts("Erro ao realocar!");
            exit(0);
        }
        *(n + (n3 - 1)) = n2;   
    }
    *(n + n3) = '%pre%';  
    return n;
}   

int main(void) {  
    char *name = nome("Nome:");
    char *iterador = name;
    while(*iterador != '%pre%') {
        printf("%c", *iterador++);
    }
    printf("%s", name); //bem mais simples, certo?
    free(name);
}
' && n2 != EOF) { n = realloc(n, ++n3); if (n == NULL) { puts("Erro ao realocar!"); exit(0); } *(n + (n3 - 1)) = n2; } *(n + n3) = '%pre%'; return n; } int main(void) { char *name = nome("Nome:"); char *iterador = name; while(*iterador != '%pre%') { printf("%c", *iterador++); } printf("%s", name); //bem mais simples, certo? free(name); }

See running on ideone . And no Coding Ground . Also put it in GitHub for future reference .

    
19.06.2017 / 22:28