Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. An struct declared within another struct.

An struct declared within another struct.

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpquestionannouncement
11 Posts 4 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Comp_Users

    Hi, I have an structure declared in the .h file.

    typedef struct
    {
    int nID;
    TCHAR szName[MAX_PATH];
    }MyStruct1;

    //I declare a list of MyStruct1 object with a key of type TCHAR as below.
    typedef std::map, MyStruct1> MyStructList;

    Another struct which contains the MyStructList list as it's member.

    typedef struct
    {
    int age;
    TCHAR Place[MAX_PATH];
    MyStructList ObjList;
    }MyMainStruct;

    In the .cpp file.

    void MyApp::main()
    {
    MyMainStruct objMainStruct;
    UpdateList( objMainStruct.ObjList);
    }

    //The list is being updated in this function below.
    void MyApp::UpdateList(MyStructList &ObjList)
    {
    MyStruct1 MyStructObj;
    _tcscpy(MyStructObj.szName, _T("ABCDE"));
    MyStructObj.nID = 2532;
    ObjList.Insert(MyStructList::value_type(_T("First"), MyStructObj)).second;
    //fill this list with multiple MyStruct1 object and unique keys.
    }

    I am not able to update the MyStructList list structure in the function below. When I perform an insert on the list or for that matter any operation on the list, my application (VC2005 on vista) crashes. Is it because the way I have declared an List object inside another structure that this is not working? Plesae help me there.

    R Offline
    R Offline
    Rick York
    wrote on last edited by
    #2

    I am surprised that even compiles. It should be :

    void MyApp::UpdateList( MyStructList &objList )
    {
    MyStruct1 myStructObj;
    _tcscpy( myStructObj.szName, _T("ABCDE") );
    myStructObj.nID = 2532;
    // fill this list with multiple MyStruct1 object and unique keys.
    objList.Insert( MyStructList::value_type( _T("First"), myStructObj ) ).second;
    }

    C 1 Reply Last reply
    0
    • R Rick York

      I am surprised that even compiles. It should be :

      void MyApp::UpdateList( MyStructList &objList )
      {
      MyStruct1 myStructObj;
      _tcscpy( myStructObj.szName, _T("ABCDE") );
      myStructObj.nID = 2532;
      // fill this list with multiple MyStruct1 object and unique keys.
      objList.Insert( MyStructList::value_type( _T("First"), myStructObj ) ).second;
      }

      C Offline
      C Offline
      Comp_Users
      wrote on last edited by
      #3

      I believe, when I create an instance of MyMainStruct object, the memory is not getting allocated to the MyStructList member. Is my understanding right? If so how do i get about it? I dont want to create any object on the heap and store it's pointer in the structure.

      A 1 Reply Last reply
      0
      • C Comp_Users

        Hi, I have an structure declared in the .h file.

        typedef struct
        {
        int nID;
        TCHAR szName[MAX_PATH];
        }MyStruct1;

        //I declare a list of MyStruct1 object with a key of type TCHAR as below.
        typedef std::map, MyStruct1> MyStructList;

        Another struct which contains the MyStructList list as it's member.

        typedef struct
        {
        int age;
        TCHAR Place[MAX_PATH];
        MyStructList ObjList;
        }MyMainStruct;

        In the .cpp file.

        void MyApp::main()
        {
        MyMainStruct objMainStruct;
        UpdateList( objMainStruct.ObjList);
        }

        //The list is being updated in this function below.
        void MyApp::UpdateList(MyStructList &ObjList)
        {
        MyStruct1 MyStructObj;
        _tcscpy(MyStructObj.szName, _T("ABCDE"));
        MyStructObj.nID = 2532;
        ObjList.Insert(MyStructList::value_type(_T("First"), MyStructObj)).second;
        //fill this list with multiple MyStruct1 object and unique keys.
        }

        I am not able to update the MyStructList list structure in the function below. When I perform an insert on the list or for that matter any operation on the list, my application (VC2005 on vista) crashes. Is it because the way I have declared an List object inside another structure that this is not working? Plesae help me there.

        C Offline
        C Offline
        Cedric Moonen
        wrote on last edited by
        #4

        Please, first fix the typos. For instance, you are missing a < character on the map declaration so we can't see the type you are using as a key. If you are using a TCHAR*, then review what we discussed yesterday, because it won't work. Use a std::string or std::wstring instead (because of the comparison operator that won't work for a TCHAR*). Second, if you have a crash, it is very helpfull if you provide the exact location of the crash in your code (using the call stack from the debugger) and any other assert message that you get. Third, the way you use the insert (and not Insert function) is a bit akward: why are you accessing the return value (you write a .second at the end) ? The insert function returns an iterator (the place where your data was inserted), why do you access this iterator ? It doesn't make any sense and makes the code difficult to understand. Furthermore, you I find easier to use the [] operator directly: ObjList[_T("First")] = MyStrucObj; Anyway, as said yesterday, your problem certainly comes from the fact that you are using TCHAR* as key for the map. Please, consider seriously using a string wrapper class instead, or you will make your life a hell for nothing.

        Cédric Moonen Software developer
        Charting control [v1.5] OpenGL game tutorial in C++

        1 Reply Last reply
        0
        • C Comp_Users

          I believe, when I create an instance of MyMainStruct object, the memory is not getting allocated to the MyStructList member. Is my understanding right? If so how do i get about it? I dont want to create any object on the heap and store it's pointer in the structure.

          A Offline
          A Offline
          Arman S
          wrote on last edited by
          #5

          The only flaw I could see is having an implicit cast from CONST TCHAR* into TCHAR*. However, even in this case the code may work. Are you sure the reason of your crash is inside the map::insert? My quick suggestion is trying the code by declaring the map's key as CONST TCHAR* instead. -- Arman

          C 1 Reply Last reply
          0
          • C Comp_Users

            Hi, I have an structure declared in the .h file.

            typedef struct
            {
            int nID;
            TCHAR szName[MAX_PATH];
            }MyStruct1;

            //I declare a list of MyStruct1 object with a key of type TCHAR as below.
            typedef std::map, MyStruct1> MyStructList;

            Another struct which contains the MyStructList list as it's member.

            typedef struct
            {
            int age;
            TCHAR Place[MAX_PATH];
            MyStructList ObjList;
            }MyMainStruct;

            In the .cpp file.

            void MyApp::main()
            {
            MyMainStruct objMainStruct;
            UpdateList( objMainStruct.ObjList);
            }

            //The list is being updated in this function below.
            void MyApp::UpdateList(MyStructList &ObjList)
            {
            MyStruct1 MyStructObj;
            _tcscpy(MyStructObj.szName, _T("ABCDE"));
            MyStructObj.nID = 2532;
            ObjList.Insert(MyStructList::value_type(_T("First"), MyStructObj)).second;
            //fill this list with multiple MyStruct1 object and unique keys.
            }

            I am not able to update the MyStructList list structure in the function below. When I perform an insert on the list or for that matter any operation on the list, my application (VC2005 on vista) crashes. Is it because the way I have declared an List object inside another structure that this is not working? Plesae help me there.

            C Offline
            C Offline
            Cedric Moonen
            wrote on last edited by
            #6

            If you are not yet convinced about using a std::string as a key, here[^] is a discussion about a guy having issues because he is using a char* as key for the map. I'm almost sure that your problem is because of that (maybe not the crash but at least your program won't work at all the way you would expect it). Just because it compiles doesn't mean that your code is correct. Just because it compiles with a TCHAR* as key doesn't mean that it is valid.

            Cédric Moonen Software developer
            Charting control [v1.5] OpenGL game tutorial in C++

            C 1 Reply Last reply
            0
            • A Arman S

              The only flaw I could see is having an implicit cast from CONST TCHAR* into TCHAR*. However, even in this case the code may work. Are you sure the reason of your crash is inside the map::insert? My quick suggestion is trying the code by declaring the map's key as CONST TCHAR* instead. -- Arman

              C Offline
              C Offline
              Cedric Moonen
              wrote on last edited by
              #7

              Arman Z. Sahakyan wrote:

              The only flaw I could see

              Well, see my other post. How is the map comparing the keys ? Using a ==operator. Now, what happens if you compare two TCHAR* strings using a == operator ? The addresses are compared, not the contents. Thus you will end up with crazy results if you don't use a comparison function like strcmp. The map doesn't know that it has to use such function to compare the keys, thus it will compare simply the pointers and you end up with unexpected results.

              Cédric Moonen Software developer
              Charting control [v1.5] OpenGL game tutorial in C++

              A 1 Reply Last reply
              0
              • C Cedric Moonen

                Arman Z. Sahakyan wrote:

                The only flaw I could see

                Well, see my other post. How is the map comparing the keys ? Using a ==operator. Now, what happens if you compare two TCHAR* strings using a == operator ? The addresses are compared, not the contents. Thus you will end up with crazy results if you don't use a comparison function like strcmp. The map doesn't know that it has to use such function to compare the keys, thus it will compare simply the pointers and you end up with unexpected results.

                Cédric Moonen Software developer
                Charting control [v1.5] OpenGL game tutorial in C++

                A Offline
                A Offline
                Arman S
                wrote on last edited by
                #8

                std::map uses operator ==?? I think no. It uses std::less predicate which uses operator < by default. And for string pointers it's common to use them as keys because identical strings are stored in the same location and different strings (I mean const char* ) in different. But surely, I'd chose std::string as a more modern (and event more) alternative to those const char pointers!

                -- Arman

                C 1 Reply Last reply
                0
                • A Arman S

                  std::map uses operator ==?? I think no. It uses std::less predicate which uses operator < by default. And for string pointers it's common to use them as keys because identical strings are stored in the same location and different strings (I mean const char* ) in different. But surely, I'd chose std::string as a more modern (and event more) alternative to those const char pointers!

                  -- Arman

                  C Offline
                  C Offline
                  Cedric Moonen
                  wrote on last edited by
                  #9

                  Arman Z. Sahakyan wrote:

                  std::map uses operator ==?? I think no.

                  Honnestly, I don't exactly what it uses internally, but it uses an arithmetic operator anyway (operator < for instance). And this is not really nice working with pointers :)

                  Arman Z. Sahakyan wrote:

                  And for string pointers it's common to use them as keys because identical strings are stored in the same location

                  No, not necessarily. Otherwise you wouldn't need a strcmp function :) . I guess it's okay if you are using static strings but once you start maniuplating those strings and copying to another TCHAR pointer, then everything is screwed. And you certainly want to use something else than just the same TCHAR pointer for your key.

                  Cédric Moonen Software developer
                  Charting control [v1.5] OpenGL game tutorial in C++

                  1 Reply Last reply
                  0
                  • C Cedric Moonen

                    If you are not yet convinced about using a std::string as a key, here[^] is a discussion about a guy having issues because he is using a char* as key for the map. I'm almost sure that your problem is because of that (maybe not the crash but at least your program won't work at all the way you would expect it). Just because it compiles doesn't mean that your code is correct. Just because it compiles with a TCHAR* as key doesn't mean that it is valid.

                    Cédric Moonen Software developer
                    Charting control [v1.5] OpenGL game tutorial in C++

                    C Offline
                    C Offline
                    Comp_Users
                    wrote on last edited by
                    #10

                    I am not sure if you guys understand the issue that I am facing.

                    typedef struct
                    {
                    int nID;
                    TCHAR szName[MAX_PATH];
                    }MyStruct1;

                    //I declare a list of MyStruct1 object with a key of type TCHAR as below.

                    typedef std::map, MyStruct1> MyStructList;
                    Another struct which contains the MyStructList list as it's member.
                    typedef struct
                    {
                    int age;
                    TCHAR Place[MAX_PATH];
                    MyStructList ObjList;
                    }MyMainStruct;

                    //This code gives a crash.

                    void MyApp::UpdateList(MyStructList &ObjList)
                    {
                    ObjList.clear();//Even this line crashes.
                    MyStruct1 MyStructObj;
                    _tcscpy(MyStructObj.szName, _T("ABCDE"));
                    MyStructObj.nID = 2532;
                    ObjList.Insert(MyStructList::value_type(_T("First"), MyStructObj)).second;
                    //fill this list with multiple MyStruct1 object and unique keys.
                    }

                    //But the given function adds all the structure object with their key without any crash.

                    void MyApp::UpdateList2()
                    {
                    MyStructList localObjList
                    MyStruct1 MyStructObj;
                    _tcscpy(MyStructObj.szName, _T("ABCDE"));
                    MyStructObj.nID = 2532;
                    localObjList.Insert(MyStructList::value_type(_T("First"), MyStructObj)).second;
                    //fill this list with multiple MyStruct1 object and unique keys.
                    }

                    Which means that there is nothing wrong with the insert operation that is performed on the localObjList list object which is a local copy. when I create an instance of MyMainStruct object, will the MyStructList member of MyMainStruct also be instantiated. void main() { MyMainStruct MainObj; UpdateList(MainObj.objMyStructList);//This call crashes. }

                    modified on Friday, February 13, 2009 3:22 AM

                    C 1 Reply Last reply
                    0
                    • C Comp_Users

                      I am not sure if you guys understand the issue that I am facing.

                      typedef struct
                      {
                      int nID;
                      TCHAR szName[MAX_PATH];
                      }MyStruct1;

                      //I declare a list of MyStruct1 object with a key of type TCHAR as below.

                      typedef std::map, MyStruct1> MyStructList;
                      Another struct which contains the MyStructList list as it's member.
                      typedef struct
                      {
                      int age;
                      TCHAR Place[MAX_PATH];
                      MyStructList ObjList;
                      }MyMainStruct;

                      //This code gives a crash.

                      void MyApp::UpdateList(MyStructList &ObjList)
                      {
                      ObjList.clear();//Even this line crashes.
                      MyStruct1 MyStructObj;
                      _tcscpy(MyStructObj.szName, _T("ABCDE"));
                      MyStructObj.nID = 2532;
                      ObjList.Insert(MyStructList::value_type(_T("First"), MyStructObj)).second;
                      //fill this list with multiple MyStruct1 object and unique keys.
                      }

                      //But the given function adds all the structure object with their key without any crash.

                      void MyApp::UpdateList2()
                      {
                      MyStructList localObjList
                      MyStruct1 MyStructObj;
                      _tcscpy(MyStructObj.szName, _T("ABCDE"));
                      MyStructObj.nID = 2532;
                      localObjList.Insert(MyStructList::value_type(_T("First"), MyStructObj)).second;
                      //fill this list with multiple MyStruct1 object and unique keys.
                      }

                      Which means that there is nothing wrong with the insert operation that is performed on the localObjList list object which is a local copy. when I create an instance of MyMainStruct object, will the MyStructList member of MyMainStruct also be instantiated. void main() { MyMainStruct MainObj; UpdateList(MainObj.objMyStructList);//This call crashes. }

                      modified on Friday, February 13, 2009 3:22 AM

                      C Offline
                      C Offline
                      Cedric Moonen
                      wrote on last edited by
                      #11

                      Comp_Users wrote:

                      I am not sure if you guys understand the issue that I am facing.

                      You didn't explain it this way last time, so no I didn't know that it was crashing on clear (you said it was crashing on insert). Anyway, there's something important that you have to understant is that your code is 'broken' and you can end up with unexpected results. First, take some time to fix the issue with your key in the map, chances are that the problem will be solved. If that's not the case, then we can look at a code that is a bit cleaner and try to narrow the problem. Now, there's just too many issues with your code.

                      Cédric Moonen Software developer
                      Charting control [v1.5] OpenGL game tutorial in C++

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups