Thread: problem with realloc

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    19

    problem with realloc

    hi can anyone tell me whats wrong with my realloc
    I get segmentationfault when my testprogram does: p = realloc(p, 4711);

    Code:
    void *realloc(void *oldp, size_t size)
    {
        if (oldp == NULL){                    
            return(malloc(size));
        }
        void *newp;                       
        size_t oldsize;                       
        oldsize = ((Header *)oldp - 1)->s.size * sizeof(Header);    
        newp = malloc(size);                    
        if (NULL != newp){                   
            memcpy(newp, oldp, min(size, oldsize));       
            free((void *)oldp);                
        }
        return(newp);                                                   
    }
    thanks for your help!

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Why are you trying to redefine realloc? It's already a built-in C function that does relatively the same as what you're trying to do.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    19
    im writing a little library with malloc, realloc and free and then im gonna do some performance analysis and compare with the built in functions. any ideas of why i get segmentationsfaults?

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by mattholm View Post
    Code:
        oldsize = ((Header *)oldp - 1)->s.size * sizeof(Header);
    Why are you multiplying by sizeof(Header)?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    What is Header? I'd be willing to bet that the segfault occurs on that line, as the code compiles and works fine without it, and in place, a random number.



    Quote Originally Posted by mattholm View Post
    im writing a little library with malloc, realloc and free and then im gonna do some performance analysis and compare with the built in functions. any ideas of why i get segmentationsfaults?
    How are you going to do malloc (assuming you don't just calloc()) and free?


  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    19
    the malloc first-fit and free part is from a book (so I will only write realloc and worst and bestfit for malloc) and the Header comes from this:

    Code:
    typedef long Align; /*for alignment to long boundary*/
    
    union header {    /*block header*/
        struct{    
            union header *ptr;    /* next block if on free list*/
            unsigned size;        /* size of this block*/
        }s;
        Align x;    /*force alignment of blocks*/
    };
    
    typedef union header Header;

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    19
    I was hoping this line
    Code:
    oldsize = ((Header *)oldp - 1)->s.size * sizeof(Header);
    would make the oldp point to the header

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by mattholm View Post
    I was hoping this line
    Code:
    oldsize = ((Header *)oldp - 1)->s.size * sizeof(Header);
    would make the oldp point to the header
    The ((Header *)oldp - 1) expression does that, yes. You seem to be trying to access the .size field of the header. Everything makes sense, except that multiplication by sizeof(Header). Since you seem unable to explain why you're doing it, I'm going to guess that it's the cause of your problem.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  9. #9
    Registered User
    Join Date
    Nov 2011
    Posts
    19
    Thank you!!! it should be + sizeof(Header);

    thanks alot!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with realloc()
    By TheDarkAvenger in forum C Programming
    Replies: 3
    Last Post: 05-06-2009, 12:16 PM
  2. problem with realloc()
    By creeping death in forum C Programming
    Replies: 2
    Last Post: 03-01-2009, 12:07 AM
  3. problem with realloc
    By oooflascheooo in forum C Programming
    Replies: 5
    Last Post: 07-31-2008, 04:08 AM
  4. Problem with realloc
    By wd_kendrick in forum C Programming
    Replies: 4
    Last Post: 05-23-2008, 02:41 AM