How to return a map< string , structure* > from a function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Soujiro
    New Member
    • Jan 2007
    • 35

    How to return a map< string , structure* > from a function

    Code:
    typedef struct 
    {
        int age;
        string name;
    } structure;
    int functionCall( map< char* , structure* >* map_o )
    {
        map< char* , structure* >* map_op;
        map_op = map_o;
    
        structure msgData_sp;
        msgData_sp.name="You";
        msgData_sp.age=12;
    
        msgMap_o->insert( map< char* , structure* >::value_type( "Me" , &msgData_sp ) );
    
        // i tried to print the values of the map here.. and it was ok
        return 0;
    }
    
    int main()
    {
        map< string , structure* > msgMap_o;
        functionCall( &msgMap_o );
    
        map< string , structure* >::iterator iter_o = msgMap_o.begin();
        cout << "MainKey: " << iter_o->first << endl;  
        cout << "Line: " << (iter_o->second)->LineContractNumber_i << endl; 
        // the key was printed but
        // when i tried to print the values.. seg fault
    }

    Can you help me with this simple program.. Ways in order for main to receive the changes main by functionCall.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Step 1: typedef your map, it will make the code so much easier to read and maintain and by doing this you will avoid passing incompatable types around functionCall expects map< char* , structure* >, main passes map< string , structure* >

    Code:
    typedef struct 
    {
        int age;
        string name;
    } structure;
    
    typedef    map< string , structure* > MY_MAP; /* Please use a name more descriptive of this variables function */
    
    int functionCall( MY_MAP *map_o )
    {
        MY_MAP *map_op;
        map_op = map_o;
    
        structure msgData_sp;
        msgData_sp.name="You";
        msgData_sp.age=12;
    
        msgMap_o->insert( MY_MAP::value_type( "Me" , &msgData_sp ) );
    
        // i tried to print the values of the map here.. and it was ok
        return 0;
    }
    
    int main()
    {
        MY_MAP msgMap_o;
        functionCall( &msgMap_o );
    
        MY_MAP::iterator iter_o = msgMap_o.begin();
        cout << "MainKey: " << iter_o->first << endl;  
        cout << "Line: " << (iter_o->second)->LineContractNumber_i << endl; 
        // the key was printed but
        // when i tried to print the values.. seg fault
    }
    Now in function you have a structure msgData_sp. And you store a pointer to this structure in the map. However msgData_sp has auto storage class. That is it only exists for the lifetime of the function call. Once you have exited to main the data is no longer allocated to that structure and the map entry is pointing at unallocated data.

    The solution is to either change the map so that it contains the structure rather than a pointer to the structure or to allocate memory for the stucture using new or malloc.

    In the second case you will have to make sure you free the memory as you delete map entries otherwise you will have a memory leak.

    Comment

    • Soujiro
      New Member
      • Jan 2007
      • 35

      #3
      Originally posted by Banfa
      Step 1: typedef your map, it will make the code so much easier to read and maintain and by doing this you will avoid passing incompatable types around functionCall expects map< char* , structure* >, main passes map< string , structure* >

      Code:
          Refer to the code above
      Now in function you have a structure msgData_sp. And you store a pointer to this structure in the map. However msgData_sp has auto storage class. That is it only exists for the lifetime of the function call. Once you have exited to main the data is no longer allocated to that structure and the map entry is pointing at unallocated data.

      The solution is to either change the map so that it contains the structure rather than a pointer to the structure or to allocate memory for the stucture using new or malloc.

      In the second case you will have to make sure you free the memory as you delete map entries otherwise you will have a memory leak.

      Thanks a lot banfa.. that was cool

      Comment

      Working...