Jump to content

Realloc problem

- - - - -

  • Please log in to reply
2 replies to this topic

#1
yelman

yelman

    Newbie

  • Members
  • Pip
  • 2 posts
Hi guys ,I encounter a strange problem with realloc function:
I try to change string size from a different function from where the string definition is.
I send string's pointer to the function,then use malloc with a beginning value which works fine and then use realloc to change length,the realloc itself seems to work but I cannot insert input into the string afterwards.
Also I have to use this way of getting input because our lecturer asked us to write the program for
unknown size of input.

Here is the code: (I marked the problematic lines)

worker* Add_End(worker* head)       /*I use this function to set up a linked list*/
{
    char** ptr;
    worker* tail=head;
    worker* new_item;
    new_item=(worker*)malloc(sizeof(worker));
    if(new_item==NULL)
    {
        printf("Memory allocation failed - return previous value");
        return head;
    }
    ptr=&new_item->f_name;
    [B]Get_Input(ptr);[/B]                  //Input function call//
    printf("%s",new_item->f_name);
    new_item->next=NULL;
    if(head==NULL)
        return new_item;
    while(tail->next!=NULL)
        tail=tail->next;
    tail->next=new_item;
    return head;
}

void Get_Input(char** str)
{
    fflush(stdin);
    int n=1;
    char tmpc;
    *str=(char*)malloc(n*sizeof(char));
    do
    {
        tmpc=getchar();
        *str[n-1]=tmpc;
        ++n;
        *str=(char*)realloc(*str,(n)*sizeof(char));
    }
    while(tmpc!='\n');
    *str[n]='\n';
}


Thanks in advance!!!

Edited by ZekeDragon, 15 April 2010 - 07:06 PM.
Please use [code] tags (the # button) when posting code.


#2
Ancient Dragon

Ancient Dragon

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 400 posts
post worker structure.

>>fflush(stdin);
Non-standard use of fflush(). Its only guarenteed to work on output streams such as stdout.

>>tmpc=getchar();
getchar() returns an int, not a char. tmpc should be declared as an int.

>>*str=(char*)realloc(*str,(n)*sizeof(char));
Two problems here:
1) sizeof(char) is always 1, so you might as well say (n) * 1. That doesn't make any sense so just drop the sizeof(char) stuff.
2) You are wasting a lot of time realloc'ing every character. Increase the size of the array by some block size of characters, then it will only need to be reallocated when its filled up. Of course you will have to have another counter to keep track of the current size of the array so that you can test when the array has reached the allocated size.

>>*str[n]='\n';
Wrong. strings are null terminated with '\0', not '\n'.
Visit Grandpa's Forums, a social networking forum, with family-oriented arcade games, blogs, discussion forums, and photo albums.

#3
yelman

yelman

    Newbie

  • Members
  • Pip
  • 2 posts
Thanks for the reply!
fflush(stdin) do work in Visual studio(doesnt work in GCC),I'll change it but its not the main problem of the program.
You are right about getchar it return's int but its still works and it's purpose by definition is to get characters.
Unfortunatly I cannot do the input the way you suggested (with realloc to correct size of string),because the size have to be unlimited,the realloc after each char way is very wasteful but that the only option I found to get an unknown size string.
I think I figured out how to solve the realloc problem:
To use another string defined in the function and then to do all the realloc operations on it,then just strcpy to the real's string pointer ,it seems to work as far as I can say.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users