473,231 Members | 1,497 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,231 software developers and data experts.

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
4 16714
weaknessforcats
9,208 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
9,208 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
Alex340
1 Bit
Thanks a lot for this answer!
Dec 31 '20 #5

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

Similar topics

10
by: Angus Leeming | last post by:
Hello, Could someone explain to me why the Standard conveners chose to typedef std::string rather than derive it from std::basic_string<char, ...>? The result of course is that it is...
2
by: Neil Zanella | last post by:
Hello, Consider the following program. There are two C style string stack variables and one C style string heap variable. The compiler may or may not optimize the space taken up by the two stack...
22
by: Jason Heyes | last post by:
Does this function need to call eof after the while-loop to be correct? bool read_file(std::string name, std::string &s) { std::ifstream in(name.c_str()); if (!in.is_open()) return false; ...
9
by: vsgdp | last post by:
Hi, Is there a unicode equivalent to std::string?
2
by: Susan Baker | last post by:
Hi, I have declared a class MyClass in a header file MyClass.h I have then gone onto define the class in MyClass.cpp. This is (roughly) what the definition (.cpp) file looks like: #include...
6
by: Khuong Dinh Pham | last post by:
How do i read the contents of a xml file and output it to a std::string. Thx in advance
8
by: vidya.bhagwath | last post by:
Hello Experts, I am using std::string object as a member variable in one of the my class. The same class member function operates on the std::string object and it appends some string to that...
3
by: doubts | last post by:
Hi all, I am trying to convert my bulk of code from VC++ 6.0 to VC++.Net. when using std::string type variable, the application causes exception at one instance and does not cause an exception at...
84
by: Peter Olcott | last post by:
Is there anyway of doing this besides making my own string from scratch? union AnyType { std::string String; double Number; };
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.