Thread: Problem with realloc()

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

    Problem with realloc()

    Hello,

    I am using code::blocks on Windows plateform for my program. In my program, in one of the loop, I am using realloc function to increase the allocation space for one pointer to structure. It almost always fails and program does not run sucessful. For understanding, I tried the following program.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    int main(void)
    {
        int i, j, loop_end=15;
        double x=10.00, *y;
        //char x='A', *y;
        y=(double*)calloc(1, sizeof(double));
        //y=(char*)calloc(1, sizeof(char));
        *y=x;
        printf("\n\n  STARTING: @ %d, value= %lf\n\n",y, *y);
        //printf("\n\n  STARTING: @ %d, value= %c\n\n",y, *y);
        for(i=0; i<=loop_end-1; i++)
        {
            printf("\n Loop No: %d\n--------------",i);
            if((realloc(y, (i+2)*sizeof(double))) == NULL)
            //if((realloc(y, (i+2)*sizeof(char))) == NULL)
            {
                printf("\n\n In loop %d mem-err occured\n",i);
                free(y);
                getchar();
                return 0;
            }
            for(j=0; j<=i; j++)
                printf("\n [%2d] Address: %d   Value: %lf",
                       j, (y+j), *(y+j));
                //printf("\n [%2d] Address: %d   Value: %c",
                       //j, (y+j), *(y+j));
             *(y+i+1)=*(y+i)+1;
        }
        printf("\n Loop completed");
        free(y);
        getchar();
        return 0;
    }
    When I run it for char few loops run (sometimes 5, sometimes 6) and for double it never runs sucessfully even two loops (in compilation, no warning). Please let me know where I am doing some mistake in this ??

    Thanks and regards,
    Amal.

  2. #2
    Registered User
    Join Date
    Jun 2009
    Posts
    120
    realloc() doesn't always return the same pointer as input parameter.
    Code:
    new_pointer = realloc(old_pointer, new_size);

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    19
    Thanks,
    Introducing second pointer solved the problem.......

  4. #4
    Registered User
    Join Date
    Jun 2009
    Posts
    120
    Quote Originally Posted by amalendu View Post
    Introducing second pointer solved the problem.......
    Actually, you could have used already existing pointer by assigning result of realloc() to it.
    Code:
    y = realloc(y, (i+2)*sizeof(double));

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by DRK View Post
    Actually, you could have used already existing pointer by assigning result of realloc() to it.
    Code:
    y = realloc(y, (i+2)*sizeof(double));
    Except that generally means that if realloc fails you leak the original allocation.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Realloc Problem
    By KittenFace in forum C Programming
    Replies: 2
    Last Post: 11-05-2010, 10:19 AM
  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. Realloc problem
    By franziss in forum C Programming
    Replies: 14
    Last Post: 01-25-2005, 10:21 AM