473,473 Members | 2,159 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

std::string as struct member variable

2 New Member
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 16824
weaknessforcats
9,208 Recognized Expert Moderator Expert
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
hugob0ss
2 New Member
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 Recognized Expert Moderator Expert
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 New Member
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.