469,927 Members | 1,904 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,927 developers. It's quick & easy.

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

Soujiro
35
Expand|Select|Wrap|Line Numbers
  1. typedef struct 
  2. {
  3.     int age;
  4.     string name;
  5. } structure;
  6. int functionCall( map< char* , structure* >* map_o )
  7. {
  8.     map< char* , structure* >* map_op;
  9.     map_op = map_o;
  10.  
  11.     structure msgData_sp;
  12.     msgData_sp.name="You";
  13.     msgData_sp.age=12;
  14.  
  15.     msgMap_o->insert( map< char* , structure* >::value_type( "Me" , &msgData_sp ) );
  16.  
  17.     // i tried to print the values of the map here.. and it was ok
  18.     return 0;
  19. }
  20.  
  21. int main()
  22. {
  23.     map< string , structure* > msgMap_o;
  24.     functionCall( &msgMap_o );
  25.  
  26.     map< string , structure* >::iterator iter_o = msgMap_o.begin();
  27.     cout << "MainKey: " << iter_o->first << endl;  
  28.     cout << "Line: " << (iter_o->second)->LineContractNumber_i << endl; 
  29.     // the key was printed but
  30.     // when i tried to print the values.. seg fault
  31. }
  32.  
  33.  

Can you help me with this simple program.. Ways in order for main to receive the changes main by functionCall.
Jan 15 '07 #1
2 12452
Banfa
9,065 Expert Mod 8TB
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* >

Expand|Select|Wrap|Line Numbers
  1. typedef struct 
  2. {
  3.     int age;
  4.     string name;
  5. } structure;
  6.  
  7. typedef    map< string , structure* > MY_MAP; /* Please use a name more descriptive of this variables function */
  8.  
  9. int functionCall( MY_MAP *map_o )
  10. {
  11.     MY_MAP *map_op;
  12.     map_op = map_o;
  13.  
  14.     structure msgData_sp;
  15.     msgData_sp.name="You";
  16.     msgData_sp.age=12;
  17.  
  18.     msgMap_o->insert( MY_MAP::value_type( "Me" , &msgData_sp ) );
  19.  
  20.     // i tried to print the values of the map here.. and it was ok
  21.     return 0;
  22. }
  23.  
  24. int main()
  25. {
  26.     MY_MAP msgMap_o;
  27.     functionCall( &msgMap_o );
  28.  
  29.     MY_MAP::iterator iter_o = msgMap_o.begin();
  30.     cout << "MainKey: " << iter_o->first << endl;  
  31.     cout << "Line: " << (iter_o->second)->LineContractNumber_i << endl; 
  32.     // the key was printed but
  33.     // when i tried to print the values.. seg fault
  34. }
  35.  
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.
Jan 15 '07 #2
Soujiro
35
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* >

Expand|Select|Wrap|Line Numbers
  1.     Refer to the code above
  2.  
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
Jan 16 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Marcelo Pinto | last post: by
1 post views Thread by sandwich_eater | last post: by
13 posts views Thread by liujiaping | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.