reallocating structures dynamically in functions

This is a discussion on reallocating structures dynamically in functions within the C Programming forums, part of the General Programming Boards category; I've recently started using structures, but I am having problems in allocating the structure dynamically. In the code below if ...

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    3

    reallocating structures dynamically in functions

    I've recently started using structures, but I am having problems in allocating the structure dynamically. In the code below if i allocate the structure in the main program it works fine, and i get the expected output. However if i use the function rper below to increase the size of the structure i get totally wrong results. Am i passing the structure wrong to the function? From what i understand I am just passing the pointer and realloc just chages the pointer, which then should be returned to the main function. But it seems that the allocation of elem, poi ,sign messes something up. Thanks for your help
    Cezary

    Program:
    #include
    typedef struct peri{
    int no;
    int *elem;
    int *poi;
    int *sign;
    int reduced;
    int type;
    } peri ;
    void rper(int size,peri *per);

    int main (int argc, char *argv[]){
    int nnods=10,i;
    peri *per;
    per= (peri *) malloc((2)*sizeof(peri));
    per[1].no=2;
    for(i=1;i<=nnods;i++){
    rper(i,per);
    /* per=realloc(per,(i+1)*sizeof(peri));
    per[i].elem=(int*)malloc(2*sizeof(int));
    per[i].poi=(int*)malloc(2*sizeof(int));
    per[i].sign=(int*)malloc(2*sizeof(int));
    per[i].no=i;*/
    printf(" for i %d per.no is %d\n",i,per[i].no);
    }
    return;
    }
    void rper(int size,peri *per){
    per=realloc(per,(size+1)*sizeof(peri));
    per[size].elem=(int*)malloc(2*sizeof(int));
    per[size].poi=(int*)malloc(2*sizeof(int));
    per[size].sign=(int*)malloc(2*sizeof(int));
    per[size].no=size;

    }

    Results if realloc in main:
    for i 1 per.no is 1
    for i 2 per.no is 2
    for i 3 per.no is 3
    for i 4 per.no is 4
    for i 5 per.no is 5
    for i 6 per.no is 6
    for i 7 per.no is 7
    for i 8 per.no is 8
    for i 9 per.no is 9
    for i 10 per.no is 10

    Results if realloc in function:
    for i 1 per.no is 1
    for i 2 per.no is 0
    for i 3 per.no is 0
    for i 4 per.no is 0
    for i 5 per.no is 0
    for i 6 per.no is 0
    for i 7 per.no is 0
    for i 8 per.no is 0
    for i 9 per.no is 0
    for i 10 per.no is 0

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    148
    You need pointer-to-pointer
    Code:
    void rper(int size,peri **per){
    	*per=(peri*)realloc(*per,(size+1)*sizeof(peri));
    	(*per)[size].elem=(int*)malloc(2*sizeof(int));
    	(*per)[size].poi=(int*)malloc(2*sizeof(int));
    	(*per)[size].sign=(int*)malloc(2*sizeof(int));
    	(*per)[size].no=size;
    
    }
    and call it this way
    Code:
    rper(i,&per);

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    3
    Thanks that helped me greatly is there a shortcut as not to type in in every line (*per)

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >is there a shortcut as not to type in in every line (*per)
    Use a temporary pointer that points to *per after you've reallocated it's memory:
    Code:
    void rper(int size, peri **per){
        peri *temp;
    
        *per = realloc(*per, (size + 1) * sizeof (peri));
    
        temp = *per;
        temp[size].elem = malloc(2 * sizeof (int));
        temp[size].poi = malloc(2 * sizeof (int));
        temp[size].sign = malloc(2 * sizeof (int));
        temp[size].no = size;
    }
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointers to structures & functions arguments
    By lautarox in forum C Programming
    Replies: 4
    Last Post: 11-17-2008, 12:27 PM
  2. Replies: 3
    Last Post: 10-31-2006, 02:15 AM
  3. Static member functions more efficient?
    By drrngrvy in forum C++ Programming
    Replies: 6
    Last Post: 06-16-2006, 08:07 AM
  4. pointers to arrays of structures
    By terryrmcgowan in forum C Programming
    Replies: 1
    Last Post: 06-25-2003, 10:04 AM
  5. API "Clean Up" Functions & delete Pointers :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 05-10-2002, 07:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21