Thread: Realloc

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    69

    Realloc

    Im trying to read in from a text file with an unknown amount of memory, its not reading in the entire text file though. Heres my output and snippet of code.

    text file
    Code:
    Bryan has managed to be quite prolific and successful as bears go.
    The eighteen known subspecies of Bryan can be found throughout the United States and Canada.
    Estimates of the number of black Bryans in North America vary, with 750,000 being the most often suggested.
    In the state of Pennsylvania there are believed to be more than 7000 of the animals scattered across the state.
    output
    Code:
    [cat]$ ./cat -A test.txt
    mals scattered across the state.$re are believed to be more than 7000 of the ani
    code
    Code:
            do
            {
                    cap+=10;
                    buf = realloc(buf,  sizeof(char) * cap);
                    fread ( buf, sizeof( char ), cap, fptr );
            }
            while(!feof(fptr));

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    fread doesn't add to the end of what's already there, but overwrites what had been there (which is why you can see the end of the previous read, since the last read was "short").

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    69
    so then i should put the buf into a temp, realloc then add the two together?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You could, I suppose. As long as you keep track of where everything is (and how much of everything is there), it'll work.

  5. #5
    Registered User
    Join Date
    Jan 2010
    Posts
    69
    is there an easier way?

  6. #6
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    You could do that. I think you could also "add onto it" by reading 10 bytes at a time after reallocating with something like:
    Code:
    fread ( buf+cap-10, sizeof char, 10, fptr );
    You could also avoid using feof() as your loop control- See that FAQ.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  7. #7
    Registered User
    Join Date
    Jan 2010
    Posts
    69
    i changed it to this
    Code:
            do
            {
                    cap+=10;
                    buf = realloc(buf,  sizeof(char) * cap);
                    fread ( buf+cap-10, sizeof( char ), cap, fptr );
            }
            while(!feof(fptr));
    and got this error
    *** glibc detected *** ./cat: realloc(): invalid next size: 0x0000000015df1010 ***

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The difference between your line and Neon's line is significant. You have added 10 characters to your buffer. You must not try to then read another 20 (or 30, or 40) characters into those new 10 spots, because, well, you've only got 10 new spots.

    (And edit to clarify an additional possible piece of confusion: and also, fread doesn't go back to the beginning of the file each time; it will pick up where it left off.)

  9. #9
    Registered User
    Join Date
    Jan 2010
    Posts
    69
    Code:
            do
            {
                    cap+=10;
                    buf = realloc(buf,  sizeof(char) * cap);
                    fread ( buf+cap-10, sizeof( char ), 10, fptr );
                    printf("%d\n", num_items++);
                    printf("%s\n", buf);
            }
            while(!feof(fptr));
    this is what i have now and when i run it, it goes through my loop 39 times buf each time it prints a blank line for buf....

    Code:
    [dodoherty@vulcan cat]$ ./cat -A test.txt
    0
    
    1
    
    2
    
    3
    
    4
    
    5
    
    6
    
    7
    
    8
    
    9
    
    .
    .
    /*put period in to skip all the other numbers*/
    .
    .
    .
    
    36
    
    37
    
    38

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Do you have a priming/preliminary read outside this loop? (And please tell me you remembered to set cap = 0 before the loop started, if not.)

  11. #11
    Registered User
    Join Date
    Jan 2010
    Posts
    69
    actually i have cap set to 10, and no the first read is inside the loop

  12. #12
    Registered User
    Join Date
    Jan 2010
    Location
    Ca, US
    Posts
    29
    Make sure cap starts as 0, also num_items.
    And make sure your buf pointer points to NULL (or points to valid malloc'ed memory) or the realloc will try and work with memory it might *not* be able to.

    Also with your do/while you are going to run it one more time even after you hit the end of the file.

    Dylan

  13. #13
    Registered User
    Join Date
    Jan 2010
    Posts
    69
    ok everything seems to work fine now thank you, i have my cap intialized to 10. In a previous program i had written i had done pretty much the same thing but i was reading in from a binary file and all i was reading was and array of numbers. But thanks a lot.

  14. #14
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Huh? I haven't heard of black Bryans in Pennsylvania -- though Google does show a whole bunch of people named Bryan Black.

    Bryan --> Bruan --> Bruin --> Bear?

    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. did i understood right this explantion of realloc..
    By transgalactic2 in forum C Programming
    Replies: 3
    Last Post: 10-24-2008, 07:26 AM
  2. writing a pack-style function, any advices?
    By isaac_s in forum C Programming
    Replies: 10
    Last Post: 07-08-2006, 08:09 PM
  3. using realloc
    By bobthebullet990 in forum C Programming
    Replies: 14
    Last Post: 12-06-2005, 05:00 PM
  4. segfault on realloc
    By ziel in forum C Programming
    Replies: 5
    Last Post: 03-16-2003, 04:40 PM
  5. Realloc inappropriate for aligned blocks - Alternatives?
    By zeckensack in forum C Programming
    Replies: 2
    Last Post: 03-20-2002, 02:10 PM