Thread: Question about calloc and realloc

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

    Question about calloc and realloc

    Hello!

    I have some problem with 'realloc' and 'calloc' in a program...My question is that if some pointer is passed to a function from say Main. In that function can I allocate/ reallocate the memory to the passed pointer and can these allocated addresses with data can be retained after return of the function again i.e. in Main. If this is not possible in this way then what is the way out?

    Thanks and regards,
    Amal

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Yes, but because you're modifying the pointer value, you have to pass it by another pointer:

    Code:
    void func (void **ptr)
    {
        free (*ptr);
        *ptr = malloc(10);
    }
    
    int main (void)
    {
        void *ptr;
        func (&ptr);
        return 0;
    }
    If the code is correct, after func() is called, ptr should point to something else.

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    realloc will first try to allocate more memory after the memory already allocated. If it can't do that it will copy the old data into the newly allocated space. Note that this means that the pointer may change.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    19
    Hello,

    Thanks for reply...
    I tried but not succeeded. Below is the code in which I tried to do thing in two ways (two functions), but no function did work....
    Where am I doing mistake??

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    // First test function
    void test_alloc1(double *ptr)
    {
        double *ptr_local;
        if((ptr_local=(double*)calloc(5, sizeof(double))) == NULL)
        {
            printf("\n\n  Error");
            return;
        }
        ptr=ptr_local;
    
        int i;
        double b=0.5;
        for(i=0; i<=4; i++)
        {
            *(ptr+i)=b;
            printf("\n In fun-1 @ %d value is %lf",(ptr+i),
                   *(ptr+i));
            b=b+1.0;
        }
        return;
    }
    
    // Second test function
    void test_alloc2(double **ptr)
    {
        if((*ptr=(double*)calloc(5, sizeof(double))) == NULL)
        {
            printf("\n\n  Error");
            return;
        }
    
        int i;
        double b=0.5;
        for(i=0; i<=4; i++)
        {
            **(ptr+i)=b;
            printf("\n In fun-2 @ %d value is %lf",*(ptr+i),
                   **(ptr+i));
            b=b+1.0;
        }
        return;
    }
    
    // Main
    int main(void)
    {
        double *a;
        int i;
    
        // Calling first function
        test_alloc1(a);
        for(i=0; i<=4; i++)
          printf("\n In MAIN-1 @ %d Value is : %lf",
                 (a+i),*(a+i));
        free(a);
    
        // Calling second function
        test_alloc2(&a);
        for(i=0; i<=4; i++)
          printf("\n In MAIN-2 @ %d Value is : %lf",
                 (a+i),*(a+i));
    
        return 0;
    }
    There is no compilation error (mingw with Code::blocks on windows).

    Thanks and regards,
    Amal.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    test_alloc1 is obviously wrong because you are assiging to a parameter, so the changes will not get reflected in the caller.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Oct 2011
    Posts
    19
    Thanks Laserlight....

    But test_alloc2 is also not working.....Please help me to figure out the mistake in this function.......

    Regards,
    Amal...

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by amalendu
    But test_alloc2 is also not working.
    How does it not work?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Oct 2011
    Posts
    19
    After running 'for' loop only once in test_alloc2, program stops....

    Regards,
    Amal.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I might write it in this way:
    Code:
    #define ARRAY_SIZE 5
    
    // Second test function
    void test_alloc2(double **ptr)
    {
        int i;
        double b = 0.5;
        *ptr = calloc(ARRAY_SIZE, sizeof(**ptr));
        if(*ptr == NULL)
        {
            printf("\n\n  Error");
            return;
        }
     
        for (i = 0; i < ARRAY_SIZE; i++)
        {
            (*ptr)[i] = b;
            printf("\n In fun-2 @ %p value is %f", (void*)((*ptr) + i), (*ptr)[i]);
            b++;
        }
    }
    Take note of the difference between **(ptr+i) and (*ptr)[i].
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. structure array calloc realloc help
    By BrandNew in forum C Programming
    Replies: 2
    Last Post: 03-08-2011, 09:04 AM
  2. calloc and realloc
    By sebby64 in forum C Programming
    Replies: 1
    Last Post: 12-04-2010, 03:38 PM
  3. malloc, realloc, calloc
    By newbie30 in forum C Programming
    Replies: 25
    Last Post: 08-13-2009, 01:14 PM
  4. Difference BW malloc() and calloc() and realloc()
    By svelmca in forum C Programming
    Replies: 2
    Last Post: 03-31-2009, 05:59 AM
  5. Malloc, Realloc and Calloc Help/Suggestion
    By zensatori in forum C Programming
    Replies: 7
    Last Post: 04-14-2007, 02:46 PM