Dynamically allocating text, and error in free()

Asked

Viewed 133 times

1

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

Error in . /name. c; invalid : 0x000001c18834

My code:

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

    while((n2 = getchar()) != '\0' && 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) = '\0';  
    return n;
}

// USANDO //   
int main(void)  
{  
    char *name = nome("Nome:");  
    while(*name != '\0')  
    printf("%c", *name++);    
    free(name); // aqui ta o problema, sem o free roda normal agora com o free me da esse erro
}        
  • 1

    This code doesn’t even compile, there are syntax errors, so it gets complicated until you say something. But you tried to use free(name). There’s no reason to overreact.

  • I can’t edit the text here last time someone edits to me

  • To edit your question click on edit.

  • test someone with the free and without the free

1 answer

2


The code has some problems, even is not readable. I simplified a little. The most serious problem is that it keeps in name the pointer to the allocated location. It cannot be modified so that the free at bandstand. When increment will try to free memory instead that there was no allocation and generates the error. The correct is to create an independent iteration variable and increment it, so do not move me name.

I didn’t see if you had any other problems.

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

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

int main(void) {  
    char *name = nome("Nome:");
    char *iterador = name;
    while(*iterador != '\0') printf("%c", *iterador++);
    printf("%s", name); //bem mais simples, certo?
    free(name);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • it worked using the iterator vlw friend, and on the code is not readable is because I can not edit here in stackoverflow

  • No, that’s not the problem, even mine is not so readable yet, because I didn’t want to mess with your code too much. Note that using the printf() nor would I need the iterator.

  • I didn’t copy and paste I wrote everything here

  • First you shouldn’t have done this, second, illegibility is code year, not typing.

  • it may be that my code is not legible, vlw by touch I will try to improve in this aspect, more the code is already running thanks to all who will help

Browser other questions tagged

You are not signed in. Login or sign up in order to post.