467,165 Members | 858 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

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

std::string as struct member variable

Hi, i'm with a problem here that i can't understand what it is.

Hi have this code
Expand|Select|Wrap|Line Numbers
  1.  struct SF {
  2.  
  3.   std::string mnemonic;//mnemonic that represents it
  4.   std::string name;//a descriptive name
  5.   ushort num_val1;// number of the first value
  6.   uchar possiblevalues_num; //number of the possible values.
  7.   uchar type; /*  = 1 if the feature is utilized in the models*/
  8. };
basically i am trying to declare a std::string object in a struct to later fill it with information

the next step i allocate an array of SF structs and try to do it

Expand|Select|Wrap|Line Numbers
  1.  SF * FTab;
  2.   FTab = (SF *) calloc(6, sizeof(SF));
  3.  
  4.  
  5.   FTab[0].num_val1 = (ushort) 5;
  6.   FTab[0].mnemonic("xpto");
the ushort type i can acess, the std::string generates a segmentation fault error, and i can't understant why. Does anyone know what i am doing wrong?

PS- sorry for the bad english.
Oct 16 '07 #1
  • viewed: 12724
Share:
4 Replies
weaknessforcats
Expert Mod 8TB
It's the call to calloc.

C++ uses constructors and destructors to initialize and clean up objects. calloc doesn't know about this so never called them. Your string members are garbage.

Do not use C memory allocation in C++.

Use only new an delete. Here is the corrected code:
Expand|Select|Wrap|Line Numbers
  1. struct SF {
  2.  
  3.   std::string mnemonic;//mnemonic that represents it
  4.   std::string name;//a descriptive name
  5.   unsigned short num_val1;// number of the first value
  6.   unsigned char possiblevalues_num; //number of the possible values.
  7.   unsigned char type; /*  = 1 if the feature is utilized in the models*/
  8. };
  9.  
  10. int main()
  11. {
  12. SF * FTab;
  13.   FTab = new SF[5];
  14.  
  15.   FTab[0].num_val1 = 5;
  16.   FTab[0].mnemonic ="xpto";
  17. }
  18.  
Decide now whether you will write in C++ or not. If yes, then do not:
1) malloc, calloc, alloc, etc.....
2) memcpy, mem... anything
3) free
4) exit(1)
5) strcpy, str... anything

for openers.
Oct 16 '07 #2
It's the call to calloc.

C++ uses constructors and destructors to initialize and clean up objects. calloc doesn't know about this so never called them. Your string members are garbage.

Do not use C memory allocation in C++.

Use only new an delete. Here is the corrected code:
Expand|Select|Wrap|Line Numbers
  1. struct SF {
  2.  
  3.   std::string mnemonic;//mnemonic that represents it
  4.   std::string name;//a descriptive name
  5.   unsigned short num_val1;// number of the first value
  6.   unsigned char possiblevalues_num; //number of the possible values.
  7.   unsigned char type; /*  = 1 if the feature is utilized in the models*/
  8. };
  9.  
  10. int main()
  11. {
  12. SF * FTab;
  13.   FTab = new SF[5];
  14.  
  15.   FTab[0].num_val1 = 5;
  16.   FTab[0].mnemonic ="xpto";
  17. }
  18.  
Decide now whether you will write in C++ or not. If yes, then do not:
1) malloc, calloc, alloc, etc.....
2) memcpy, mem... anything
3) free
4) exit(1)
5) strcpy, str... anything

for openers.
thanks it worked!
i am not very comfortable also writing code this way, but i'm working in a legacy system in which the memory management is made in C, so i assumed that my program would also have the same structure.
Oct 16 '07 #3
weaknessforcats
Expert Mod 8TB
but i'm working in a legacy system in which the memory management is made in C, so i assumed that my program would also have the same structure.
In that case I would stick with C entirely. Mixing C++ and C is not a good idea unless you clearly know what you are doing.
Oct 16 '07 #4
Bit
Thanks a lot for this answer!
3 Weeks Ago #5

Post your reply

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

Similar topics

10 posts views Thread by Angus Leeming | last post: by
22 posts views Thread by Jason Heyes | last post: by
9 posts views Thread by vsgdp | last post: by
6 posts views Thread by Khuong Dinh Pham | last post: by
8 posts views Thread by vidya.bhagwath@gmail.com | last post: by
84 posts views Thread by Peter Olcott | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.