Thread: using malloc & realloc

  1. #1
    Registered User shruthi's Avatar
    Join Date
    Jan 2012
    Posts
    59

    using malloc & realloc

    Hi Everyone!
    Is there a way to reduce the number of pointers used in this program using malloc itself.
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    int main()
    {
    int *p,*q,i,*r,*w;
    p=(int *)malloc(5*sizeof(int));
    printf("Enter the 5 numbers\n");
    q=p;
    for(i=0;i<5;i++)
    {
    scanf("%d",p);
    p++;
    }
    p=q;
    r=(int *)realloc(q,8*sizeof(int));
    w=r;
    r+=5;
    for(i=0;i<3;i++)
    {
    scanf("%d",r);
    r++;
    }
    r=w;
    for(i=0;i<8;i++)
    {
    printf("%d\n",*(int *)r);
    r++;
    }
    p=q;
    r=w;
    free(r);
    q=NULL;
    w=NULL;
    return 0;
    }
    


  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You could allocate 8 integers instead of only 5 and stop dicking around with realloc(), unless you're meant to show some competency with that function.

    If you are, you have a potential leak. Here's how you handle it:

    Code:
    int *q = realloc( p, newSize * sizeof( *p ) );
    if ( q != NULL ) { 
       p = q;
    }
    else {
       free( p );
       p = NULL;
    }
    Last edited by whiteflags; 09-11-2012 at 04:45 AM.

  3. #3
    Registered User shruthi's Avatar
    Join Date
    Jan 2012
    Posts
    59
    I was just experimenting with using malloc and realloac on how to access and see if it was reading into the new loaction,so took an example of 5 and 8.That's when I realised the number of pointers required to do this simple operation.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Well if it's a problem, you need to roll up your own functions to do this kind of stuff. If you think about it, a lot of this type of work is part of data structure management. So you need to do adequate planning and decide what functions you need to write. It'll be a lot easier if you can write it once, test, and forget about it.

    You could also look for a library for a particular data structure. A lot of code is already written, you just have to find it.

  5. #5
    Registered User shruthi's Avatar
    Join Date
    Jan 2012
    Posts
    59
    ok.Thanks

  6. #6
    Registered User
    Join Date
    Sep 2012
    Posts
    1
    hi.
    The malloc() is used the create the memory allocation in dynamically.
    and the realloc() is used the reallocating the memory for already exitsted memory in dynamically in C language.

  7. #7
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by shruthi View Post
    Is there a way to reduce the number of pointers used in this program using malloc itself.
    You would only need two pointers: One which is always pointing to the start of your array and another "helper" pointer which does all the moving around.
    Something like this:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
        int *numbers, *temp;
        int i;
    
        if ((numbers = malloc(5 * sizeof(*numbers))) == NULL)
        {
            fprintf(stderr, "Error allocating memory\n");
            exit(EXIT_FAILURE);
        }
    
        printf("Enter the 5 numbers\n");
        for(i = 0, temp = numbers; i < 5; i++, temp++)
            scanf("%d", temp);
    
        temp = realloc(numbers, 8 * sizeof(*numbers));
        if (temp != NULL)
            numbers = temp;
        else
        {
            fprintf(stderr, "Error reallocating memory\n");
            free(numbers);
            exit(EXIT_FAILURE);
        }
    
        printf("Enter 3 more numbers\n");
        for(i = 0, temp = numbers + 5; i < 3; i++, temp++)
            scanf("%d", temp);
    
        printf("The numbers: ");
        for(i = 0, temp = numbers; i < 8; i++, temp++)
            printf("%d ", *temp);
        putchar('\n');
    
        free(numbers);
        return EXIT_SUCCESS;
    }


    Bye, Andreas

  8. #8
    Registered User shruthi's Avatar
    Join Date
    Jan 2012
    Posts
    59
    Quote Originally Posted by AndiPersti View Post
    You would only need two pointers: One which is always pointing to the start of your array and another "helper" pointer which does all the moving around.
    Something like this:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
        int *numbers, *temp;
        int i;
    
        if ((numbers = malloc(5 * sizeof(*numbers))) == NULL)
        {
            fprintf(stderr, "Error allocating memory\n");
            exit(EXIT_FAILURE);
        }
    
        printf("Enter the 5 numbers\n");
        for(i = 0, temp = numbers; i < 5; i++, temp++)
            scanf("%d", temp);
    
        temp = realloc(numbers, 8 * sizeof(*numbers));
        if (temp != NULL)
            numbers = temp;
        else
        {
            fprintf(stderr, "Error reallocating memory\n");
            free(numbers);
            exit(EXIT_FAILURE);
        }
    
        printf("Enter 3 more numbers\n");
        for(i = 0, temp = numbers + 5; i < 3; i++, temp++)
            scanf("%d", temp);
    
        printf("The numbers: ");
        for(i = 0, temp = numbers; i < 8; i++, temp++)
            printf("%d ", *temp);
        putchar('\n');
    
        free(numbers);
        return EXIT_SUCCESS;
    }


    Bye, Andreas
    This is great! Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Use of malloc and realloc
    By mrbains in forum C Programming
    Replies: 5
    Last Post: 11-11-2010, 02:53 AM
  2. malloc and realloc
    By jayfriend in forum C Programming
    Replies: 4
    Last Post: 01-05-2007, 02:25 PM
  3. malloc and realloc
    By odysseus.lost in forum C Programming
    Replies: 3
    Last Post: 05-27-2005, 08:44 AM
  4. Need help on malloc and realloc
    By YevGenius in forum C Programming
    Replies: 8
    Last Post: 03-06-2004, 01:55 AM
  5. malloc and realloc
    By C-Struggler in forum C Programming
    Replies: 2
    Last Post: 03-11-2003, 11:31 AM