Thread: Can't Realloc!

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    4

    Angry Can't Realloc!

    Help?

    Very odd - I can't realloc the endString.... but I can realloc all the rest of them.

    It compiles either way fine. But I get a runtime error when I actually uncomment the realloc(endString, 0); line at the bottom.

    The function doesn't do anything but print the strings with fprintf (I know, why use functions?).

    I've tried it on linux and cygwin with and without -03 using gcc 3.0.

    Help?

    void * findEnds ( char * input ) {
    int len, x, y, z, n;
    char * endString = calloc(sizeof(char), 1024);
    char * filename = calloc(sizeof(char), 1024);
    char * smallString = calloc(sizeof(char), 9);


    len = strlen(input);

    for (x = len; x > 0; x--) {
    printf ("\%c|%c/%c-%c", 8, 8, 8, 8);
    if (input[x] == 49) {
    z = x + 1;
    endString = substr(input, 0, z);
    y = strlen(endString);
    sprintf(filename, "len/%d.txt", y);
    printChars(endString, filename);
    }
    }
    /* realloc(endString, 0); */
    realloc(filename, 0);
    realloc(smallString, 0);
    return NULL;


    }

    Thanks for any advice.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > printf ("\%c|%c/%c-%c", 8, 8, 8, 8);

    Just curious... why are you printing the character who's value is 8, four times here? What's the purpose of this?

    Anyway, it's possible you're ending up freeing the same block of memory twice. This is the only reason I can see for an error. You really should just use "free(string)" instead of "realloc(string,0)", it's clearer what you're doing that way. Shrug.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    4

    The 8 charachters and re-deleting

    I'm not deleting the values anywhere else. And the odd thing is that I can delete one and not the other...

    And the problem is that the routine seems to grow in memory very quickly and the only things I'm not realloc'ing to 0 I'm not allowed to.

    Why realloc? I prefer the realloc( pointer, 0); to free(pointer); just because it's easier to put in functions and then I don't have to keep track of two bits of knowledge - but whatever.

    the 8 char is the backspace!!!

    Strictly fun.

    robbie

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Since this only returns NULL, why do you have it return anything?
    Code:
    void * findEnds ( char * input ) { 
        int len, x, y, z, n; 
        char * endString = calloc(sizeof(char), 1024); 
        char * filename = calloc(sizeof(char), 1024); 
        char * smallString = calloc(sizeof(char), 9); 
    
        len = strlen(input); 
    
        for (x = len; x > 0; x--) { 
            printf ("\%c|%c/%c-%c", 8, 8, 8, 8); 
            if (input[x] == 49) { 
                z = x + 1; 
                endString = substr(input, 0, z); 
                y = strlen(endString); 
                sprintf(filename, "len/%d.txt", y); 
                printChars(endString, filename); 
            } 
        } 
    
        /* realloc(endString, 0); */ 
        realloc(filename, 0); 
        realloc(smallString, 0); 
        return NULL; 
    }
    Just make it a void function? Additionally, why is "smallString" in this function? It is never used. Also, you don't need 1KB for your file name, since it will only ever be 19 characters in length maximum, including the null. In this case, there is really no need to calloc string space. Simply use a 19 character buffer.

    char filename[19];
    sprintf( filename, "len/%d.txt", y );

    Still unsure why you cannot realloc the one, but I just thought I'd run those suggestions by you. If you can't free that, definately you're going to eat up memory every time you call this function.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    endString = substr(input, 0, z);

    I think is returning an pointer to a new string. As you did not alloc this mem, yours is lost and you can't free the new one.

    Why don't you use free()?
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > It compiles either way fine. But I get a runtime error when I
    > actually uncomment the realloc(endString, 0); line at the bottom.
    Seems a perfectly reasonable thing for it to do from what I can see....

    And I see this....

    > char * endString = calloc(sizeof(char), 1024);
    This is you allocating memory

    > endString = substr(input, 0, z);
    This is you trashing the (only) pointer to this block of memory

    > realloc(endString, 0);
    This is you trying to free a block of memory which wasn't the result of a previous call to malloc/realloc/calloc - of course its going to get upset.

  7. #7
    Registered User
    Join Date
    Nov 2001
    Posts
    4

    Thanks

    Okay, I get the reassigned pointer problem.

    But the program still grows when cleaned up because I can't free the string thatis created in the substr() function.

    Any good ways around that?

    Thanks for all the help.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    This
    Code:
    endString = substr(input, 0, z); 
    y = strlen(endString); 
    sprintf(filename, "len/%d.txt", y); 
    printChars(endString, filename);
    Should be
    Code:
    endString = substr(input, 0, z); 
    y = strlen(endString); 
    sprintf(filename, "len/%d.txt", y); 
    printChars(endString, filename); 
    free( endString );  // all done with this one
    You suggested substr also allocates memory, but you didn't post the code for that.

  9. #9
    Registered User
    Join Date
    Nov 2001
    Posts
    4

    Nope

    Still can't free it, even if I put the free(endString) in the same block as the loop.

    The substr() function adds memory because it creates and returns a string.

    I can't free the string before I return it, can I?

    Or does it just destroy it automatically on exit?

    Well, that's what I get for being an amateur.

    Anyway, here's the code for substr()


    char * substr(char *string, int start, int length) {

    char *substring;

    while (start--) string++;
    substring = string;
    while (length--) string++;
    *string = '\0';

    return substring;
    }

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You shouldn't be calling free(endString) at all on the result of substr - it just returns a modified pointer, it's not allocating any new memory, so there's nothing to free.

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    char * substr(char *string, int start, int length) { 
    
    char *substring; 
    
    while (start--) string++; 
    substring = string; 
    while (length--) string++; 
    *string = '\0'; 
    
    return substring; 
    }
    You should really consider memset() for this.

    memset( string+start, '\0', length );

    Much simpler.

    Quzah.
    Hope is the first step on the road to disappointment.

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