Thread: array of pointers of a struct

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    23

    array of pointers of a struct

    I've got an array of struct pointers as following
    Code:
    struct record{....}
    struct record*[]
    and two methods:
    Code:
    create_a_record(struct record*)
    create_record_array(struct record*[])
    Basically, create_a_record() is to create an individual record, while create_record_array() is to create an array by calling create_a_record() in a loop.

    My question is here:
    if the memory for an individual record pointer is declared in the loop of create_record_array(), it's fine; if it's declared in create_a_record(), then it fails. the code is like the following:

    Code:
    int create_record_array(struct record* recs[], int rec_no){
          int i;
          for ( i = 0; i < rec_no; i++ ){
                //if declare here, work!
                recs[i] = malloc( sizeof( struct record) );
                create_a_record( recs[i] );
          }
    
          return i;
    }
    
    struct record* create_a_record(struct record* rec){
          //if declared here, does NOT work!
          rec = malloc( sizeof( struct record) );
          ......
    
    }
    Many thanks!

    Paul

  2. #2
    Registered User vinit's Avatar
    Join Date
    Apr 2006
    Location
    India
    Posts
    39
    //if declared here, does NOT work!
    What u want to declare??

    can u post code which doesn't work, those 2 functions will do,so it will be easy to understand problem

    Thanks & Regards
    Vinit
    Last edited by vinit; 04-13-2006 at 01:09 AM.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    23
    Sorry about the confusing code.

    What i want to ask is that if
    Code:
    recs[i] = malloc( sizeof( struct record) );
    is declared in create_record_array(), the program runs correctly; but if the code is put in create_a_record(), it doesn't.

    Many thanks!

    Paul

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Do the malloc in one place or the other, not both
    Code:
    int create_record_array(struct record* recs[], int rec_no){
      int i;
      for ( i = 0; i < rec_no; i++ ){
        recs[i] = malloc( sizeof( struct record) );
        create_a_record( &recs[i] );
      }
      
      return i;
    }
    
    void create_a_record(struct record* rec){
      // fill in record
    }
    
    // OR
    // ==
    
    int create_record_array(struct record* recs[], int rec_no){
      int i;
      for ( i = 0; i < rec_no; i++ ){
        recs[i] = create_a_record();
      }
      return i;
    }
    
    struct record* create_a_record(void){
      struct record* rec;
      rec = malloc( sizeof( struct record) );
      // fill in record
      return rec;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    23
    Salem,

    Thanks for reply.

    The problem is that if malloc() is put in the loop of create_rec_array(), the program works; but if malloc() is in create_an_rec(), it does not work. That's my question.

    Sorry for the mispresentation.

    Paul

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Trim it down. Post full (non) working code to illustrate what exactly "doesn't work". Be as concise as possible.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    23
    Here is the code:
    Code:
    struct record * build_record(struct record* rec, int rec_id){
            //comment 1   ????
            //rec   = (struct record *)malloc(sizeof( struct record));
            rec->id         = rec_id;
    
            return rec;
    }
    
    int build_records(struct record* records[]){
            int i;
            for ( i = 0; lines[i] < 5; i++ ){
                    //comment 2
                    records[i] = (struct record *)malloc(sizeof( struct record));
                    build_record( records [i], i );
             }
    
            return i;
    }
    My question is that if malloc(sizeof( struct record)) is used at //comment 2, the program works; if malloc(sizeof( struct record)) is used at //comment 1, the program does not work.

    Many thanks!

    Paul

  8. #8
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    looking at your code it seems that it won't work. in create_an record fucntion u call malloc fucntion and allocate space for an record. when u leave that f........ion it's left there. what u where suppose to do is to return the allocated record back to the fucntion create_an_array as show bellow

    Code:
    struct Record *create_an_array(struct Record *recs[], int rec_no)
    {
        int i;
        
        for(i=0;i<10;i++)
            recs[i] = create_an_record(recs[i]);
    }
    
    struct Record *create_an_record(struct Record *rec)
    {
        rec = malloc(sizeof(struct Record));
        rec->data =10;   // some data filling code
        rec->ch='c';
    
        return rec;
    }
    
    //---- > my struct skeleton < ------
    struct Record
    {
        int data;
        char ch;
    };
    
    /*my output
    10  c
    10  c
    10  c
    10  c
    10  c
    10  c
    10  c
    10  c
    10  c
    10  c
    */
    ssharish2005

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    when you pass a pointer to a function, you're passing an address by value. you can alter the contents at that address, but any attempt to reassign the pointer to a new address will fail since it only affects the functions copy of the pointer.

    Code:
    void
    set(int * ptr, int value)
    {
    	*ptr = value; 
    }
    
    void
    repoint(int ** ptr, int * new_address)
    {
    	*ptr = new_address; 
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  10. #10
    Registered User
    Join Date
    Mar 2006
    Posts
    23
    ssharish2005,

    Thanks for the message.

    My real code is a bit more than what is shown, but like following:
    Code:
    struct record * build_record(struct record* rec, int rec_id){
            string words[] = {"hello", "world"};
            //comment 1 ????
            //rec   = (struct record *)malloc(sizeof( struct record));
            rec->id         = rec_id;
            rec->name       = words[0];
            rec->remark     = words[1];
    
            return rec;
    }
    It doesn't seem to work if malloc() is used at comment 1.....


    Paul

  11. #11
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    it should work if u do it like that. i guess u'r problem is with the create_an_array fucntion. u are not getting the record which is returned by the create_an_record fucntion.

    Code:
    int i;
            for ( i = 0; lines[i] < 5; i++ ){
                    //comment 2
                    //records[i] = (struct record *)malloc(sizeof( struct record));
                    records[i] = build_record( records [i], i );
             }
    
            return i;
    ssharish2005

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > is used at //comment 1, the program does not work.
    What part of "call malloc in one place" didn't you understand?

    Meh - I'm bored with this one....
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    Registered User
    Join Date
    Mar 2006
    Posts
    23
    My question is that it's supposed that malloc can be called at either place; but my program only works if malloc is called at //comment 2, but does not work if malloc is called at //comment 1.

    I guess that's probably platform dependent problem. I'm programming on Fedora, and compiling using gcc....

    Maybe i'm wrong, but I've meet several times such problems. Say, i've got to malloc() to a struct before a sub-function is called, i.e. before a struct is passed to a function, it has to be malloc().
    Last edited by paulur; 04-14-2006 at 06:34 AM.

  14. #14
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    it shouldn't be like that

    post your whole code let's have look at it

    ssharish2005

  15. #15
    Registered User
    Join Date
    Mar 2006
    Posts
    23
    ssharish2005,

    Here is the code. You can see at line 18, which is commented because malloc() doesn't work here, and i've got to put it at line 35.

    Code:
        14 struct record * build_record(struct record* rec, int rec_id, string line, string sep){
         15         string words[MAX_FIELD];
         16         parse_line(words, line, sep);
         17
         18         //rec   = (struct record *)malloc(sizeof( struct record));
         19         rec->id         = rec_id;
         20         rec->name       = words[0];
         21         rec->birth[0]   = 1;
         22         rec->birth[1]   = 2;
         23         rec->birth[2]   = 3;
         24         rec->remark     = words[2];
         25         rec->status     = '1';
         26
         27         printf("build record:\t");
         28         display_record( rec );
         29         return rec;
         30 }
         31
         32 int build_records(struct record* records[], string lines[], string sep){
         33         int i;
         34         for ( i = 0; lines[i] != NULL; i++ ){
         35                 records[i] = (struct record *)malloc(sizeof( struct record));
         36                 build_record( records [i], i, lines[i], sep );
         37
         38                 printf("check record after built, ");
         39
         40                 printf("Record: id: %d, dob: %d, name: %s, remark: %s\n",
         41                         records[i]->id, records[i]->birth[0], records[i]->name, records[i]->remark);
         42
         43         }
         44
         45         return i;
         46 }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. MergeSort with array of pointers
    By lionheart in forum C Programming
    Replies: 18
    Last Post: 08-01-2008, 10:23 AM
  2. array of pointers to an array pointers
    By onebrother in forum C Programming
    Replies: 2
    Last Post: 07-28-2008, 11:45 AM
  3. Array of struct pointers - Losing my mind
    By drucillica in forum C Programming
    Replies: 5
    Last Post: 11-12-2005, 11:50 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM