Thread: Does realloc save the previous buffer data?

  1. #1
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    291

    Does realloc save the previous buffer data?

    Hello, maybe that's a stupid question, but does the realloc function save the previous saved data into the reallocated buffer?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    char *bff,*tmp;
    int l;
    
    l=strlen("hello")+1;
    if(!(bff=malloc(l))) {getch();return 0;}
    memset(bff,0,l);
    sprintf(bff,"hello");
    
    l=strlen(bff)+strlen(" world")+1;
    if((tmp=realloc(bff,l)))//here's my question
        {
        bff=tmp;
        strcat(bff," world");
        }
    
    printf("%s",bff);
    
    memset(bff,0,l);
    free(bff);
    
    getchar();
    return 0;
    }
    
    Output:
    hello world
    I have tested that code (with dev-cpp / mingw) several times and it have worked, but I'm not sure if that works because that's a right coding practice or because somekind of coincidence have done it. I have tested some more and find that the pointer to a reallocated buffer after the reallocation have no data but it isn't a null pointer, but the data contents is still there in the memory, and tmp have the pointer to that memory:

    Code:
    //modification on the previous code
    if((tmp=realloc(bff,l)))
        {
        printf("BFF=%s\nTMP=%s\n%s\n",bff,tmp,((!bff)?"NULL BFF":"NOT NULL BFF"));
        bff=tmp;
        strcat(bff," world");
        }
    
    Output:
    BFF=
    TMP=hello
    NOT NULL BFF
    So realloc quits the pointer from the bff and asign it to the tmp (no?), the data stored previously in that point is still in that point that now is accessible from tmp, because of that I have to 'bff=tmp' to re-set the pointer to the main buffer. That will ensure that the data previously saved there will still be there after a reallocation (in all the cases)? Or is not a safe method? Or there's some situation that can fail that?

    Thank's in advance
    Niara

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You could just look up what realloc is supposed to do.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    The answer is yes. From tabstop's link:
    The function may move the memory block to a new location, in which case the new location is returned. The content of the memory block is preserved up to the lesser of the new and old sizes, even if the block is moved. If the new size is larger, the value of the newly allocated portion is indeterminate.
    As someone once said to me, realloc() wouldn't be much use if it didn't preserve the contents of the already-existing memory block.

    [edit] BTW: getch() is non-standard. I wouldn't use it if I were you. getchar() is good. Oh, and strlen(), strcat(), and friends are in <string.h>. Also, there's no need to memset() something to all zeros if you're about to sprintf() into it.

    Now that I've actually looked at your code, it seems like I should perhaps emphasize this:

    realloc() will preserve the existing block. As that site said, "If the new size is larger, the value of the newly allocated portion is indeterminate." In other words, you can't allocate one byte, write out five bytes, realloc the block to five bytes, and expect the data to be defined. [/edit]

    [edit=2] One last thing: consider working on your indentation. And please don't call a variable "l" (lowercase "L"). It looks too much like "1" (the digit one). [/edit]
    Last edited by dwks; 07-22-2008 at 01:16 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    I hope the standard of indentation and naming isn't representative of all your code.
    It's awful.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    BTW: getch() is non-standard. I wouldn't use it if I were you. getchar() is good.
    Better to use gets() ...
    Code:
    char dummy[2];
    gets(dummy);
    Because if we do:
    Code:
    /* ... */
    getchar();
    /* ... */
    char dummy[2];
    gets(dummy);
    Your program goes well...

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Better to use gets() ...
    How about being quiet for a while, reading the FAQ on why gets() is the worst possible function you could ever suggest to someone as being the thing to use
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    291
    Hi thank's all for your time and help.

    And sorry for the indentation and the getch (I don't know why I have used it, you can see that at the end I have used the getchar() instead; surely I was in a hurry to post the question ). I'll have in mind all your advices, and I'll try to apply (that's not the first post where someone invite me to do a clearer indentation, and to avoid the getch()).

    Thank's
    Niara

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading data from buffer
    By timmer in forum C++ Programming
    Replies: 2
    Last Post: 04-25-2005, 07:03 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM