std::string as struct member variable

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hugob0ss
    New Member
    • Oct 2007
    • 2

    #1

    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
    Code:
     struct SF {
      
      std::string mnemonic;//mnemonic that represents it
      std::string name;//a descriptive name
      ushort num_val1;// number of the first value
      uchar possiblevalues_num; //number of the possible values.
      uchar type; /*  = 1 if the feature is utilized in the models*/
    };
    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

    Code:
     SF * FTab;
      FTab = (SF *) calloc(6, sizeof(SF));
      
      
      FTab[0].num_val1 = (ushort) 5;
      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.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #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:
    [code=cpp]
    struct SF {

    std::string mnemonic;//mnemonic that represents it
    std::string name;//a descriptive name
    unsigned short num_val1;// number of the first value
    unsigned char possiblevalues_ num; //number of the possible values.
    unsigned char type; /* = 1 if the feature is utilized in the models*/
    };

    int main()
    {
    SF * FTab;
    FTab = new SF[5];

    FTab[0].num_val1 = 5;
    FTab[0].mnemonic ="xpto";
    }
    [/code]

    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.

    Comment

    • hugob0ss
      New Member
      • Oct 2007
      • 2

      #3
      Originally posted by weaknessforcats
      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:
      [code=cpp]
      struct SF {

      std::string mnemonic;//mnemonic that represents it
      std::string name;//a descriptive name
      unsigned short num_val1;// number of the first value
      unsigned char possiblevalues_ num; //number of the possible values.
      unsigned char type; /* = 1 if the feature is utilized in the models*/
      };

      int main()
      {
      SF * FTab;
      FTab = new SF[5];

      FTab[0].num_val1 = 5;
      FTab[0].mnemonic ="xpto";
      }
      [/code]

      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.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Originally posted by hugob0ss
        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.

        Comment

        • Alex340
          New Member
          • Dec 2020
          • 1

          #5
          Thanks a lot for this answer!

          Comment

          Working...