Struct variables initialised?

This is a discussion on Struct variables initialised? within the C++ Programming forums, part of the General Programming Boards category; i am creating a map of search nodes and they are to be represented as struct or classes if i ...

  1. #1
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,438

    Struct variables initialised?

    i am creating a map of search nodes and they are to be represented as struct or classes if i decide on C++; with there being potentially a lot of them.

    Given this type of scenario with a lot of objects of the same type is it is it normal to leave all member variables uninitialised? i mean when dealing with arrays i always try to zero everyhing or whatever the appropriate value is before i start.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Only if you like bugs.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,438
    haha so i thought true true

  4. #4
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,438
    for simple usage is there any difference between using struct or class?

  5. #5
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Classes are more powerful than structs. Meaning they provide more options. And if you want to initialize every member variable don't even think about it. Go with C++ and use classes so you can use the constructor. Not that in C it is difficult to create an initializer function...

    For initializing member variables it depends. If you have this struct:
    Code:
    struct Big {
        char c[1000];
        int i[1000];
    }
    and you care about performance you migth not want to initialize them. It might be pointless if after initializing you just copy two arrays in the struct

    EDIT: To answer more straight, if by simple usage you mean to only have member variables then the class provides a constructor to initialize them. If you also want to use functions on the structs, then the class is a far better choice

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Classes are structs in C++. The only difference is default visibility, private versus public.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,438

    so far >

    right, my usage would be along these lines, i need to look up how the constructor works and or what it actually is (sorry!) but i like the sound of it and if it is a function not available to struct then that is a good thing as i want to get used to C++ and thats a way of pushing me into it. >

    performance will definitely be an issue in the finished program if i use several units on the map.

    Code:
    class SearchArea
    {
          public:
                 int Fscore;           //total value to asses select node or not
                 int Gscore;          //movement cost
                 int Hscore;          //heurestic
                 
                 int Walkstate;        //is it valid terrain
                 int Liststate;         //is it available? on open or closed list             
                 int STnode;            //is it a / the search start point 
                 int TGnode;           //is it a / the search target point
                 
                 int Xcord;           // map node X (across) location
                 int Ycord;           // map node Y (down) location
    };
    
    int main()
    {
        
       int acrMAx = 100;
       int dwnMax = 100;
       int acr = 0;
       int dwn = 0;
       
       SearchArea node[dwnMax][acrMax];                       //is this worth doing? could this overload the stack on bigger maps?
       
       
       for(dwn = 0; dwn < dwnMax; dwn++)                        // (test stuff)
       {
               for(acr = 0; acr < acrMax; acr++)
               {
                       // i was going to use these loops to initialize members.......
                }
       }
    
    return 0;
    }
    sorry i think i should have posted this in C++ now!
    Last edited by rogster001; 09-14-2009 at 05:02 AM.

  8. #8
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,744
    You can't really use constructors with arrays anyway. I don't really see a problem with what you have, besides how it looks like you could use some bool variables. Is something confusing you?

  9. #9
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,438
    i was going to use bool for several of the variables yes, but for example 'int Walkstate' while in this version will probably only ever be true or false, in later versions could be more conditions, to allow for 'semi-walkable' i.e more difficult terrain.

    also is copy constructor useful to me for this object use? just a half-remembered one that.

    No real problems so far, the respones are useful thanks all, only wondering if this can be too big for stack if i used thousands because its seems big to me because each instance is also containing several variables, probably very naive of me that tho..>



    Code:
    SearchArea node[dwnMax][acrMax];                   //if this was thousands is it too big?

  10. #10
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    For 1d array you can initialize objects like this:
    Code:
    class SearchArea
    {
          public:
                 int Fscore;           //total value to asses select node or not
                 int Gscore;          //movement cost
                 int Hscore;          //heurestic
                 
                 int Walkstate;        //is it valid terrain
                 int Liststate;         //is it available? on open or closed list             
                 int STnode;            //is it a / the search start point 
                 int TGnode;           //is it a / the search target point
                 
                 int Xcord;           // map node X (across) location
                 int Ycord;           // map node Y (down) location
                 SearchArea(int a, int b, ....) {
                     Fscore = a;
                     Gscore = b;
                     ...
                }
    };
    
    int main()
    {
    SearchArea temp[2] = {SearchArea(1, 3, 1, ...), SearchArea(2, 3, 4, ...)};
    ...
    }
    If you want to initialize everything to 0 you could do this:
    Code:
    class SearchArea
    {
          public:
                 int Fscore;           //total value to asses select node or not
                 int Gscore;          //movement cost
                 int Hscore;          //heurestic
                 
                 int Walkstate;        //is it valid terrain
                 int Liststate;         //is it available? on open or closed list             
                 int STnode;            //is it a / the search start point 
                 int TGnode;           //is it a / the search target point
                 
                 int Xcord;           // map node X (across) location
                 int Ycord;           // map node Y (down) location
                 SearchArea(i) {
                     Fscore = 0;
                     Gscore = 0;
                     ...
                }
    };
    
    int main()
    {
    SearchArea s[10][10];
    }
    Yeah, you could easilty overflow the stack with a big number. You can dynamically allocate the objects instead with the new operator.
    Also, consider vector<> instead of an array, since it makes things easier

  11. #11
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,438
    i need to look at those examples a bit closer, i am not sure if you are implying a loop in the 2nd one (i think you are) or in the first one a member function ??

    also i see that i cannot explicitly initialise them anyway like > Gscore = 10; because i get the error > ISO Forbids initialisation of variable 'myvariable'; making 'myvariable' static.

    could you please clarify for me??

  12. #12
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    I don't imply anything.

    1st example. Think of this:
    Code:
    int a[2] = {1, 2};
    You create an array of int and you initialize them with the given values.
    now think of this:
    Code:
    myObj a[2] = {myObj(), myObj()};
    You create an array of myObj (which is an object created from a class named myObj) and you initialize them with their constructor. Their constructor is myObj(). Leave this for now if it is confusing.

    2nd example
    When you create an object the constructor is called. So if you do this:
    Code:
    myObj a[10];
    then an array of 10 myObj objects will be created. When each object is created its constructor is called. Its constructor is like a member function that is automatically called when the object is created. So whenever you create a myObj its constructor will be called. The constructor can take parameters, or it can not. In this example's case it take no parameters. The constructor just initializes all member variables to 0.

    Post the code you used to initialize the variables. Generally this is legal for public member variables:
    Code:
    SearchArea a;
    a.Gscore = 10;
    EDIT:
    Think of this:
    Code:
    SearchArea s; //you declare you variable
    Initialize(s, 1, 2, 3, 2, 3, ...); // you call a function which will initialize all member variables of s with the given integers
    Well this is done automatically, without the need of the second line. The "Initialize()" function is always the constructor of SearchArea.
    Last edited by C_ntua; 09-14-2009 at 07:37 AM.

  13. #13
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,438
    thanks, i see the similarity with the array initialise example, and are you saying that my variables are automatically set to 0 whenever i create an object like >

    Code:
    SearchArea Myobject
    so then all the member variables i listed when i declared the class are created for this object and set to 0 by the constructor? that is pretty cool if that is the case. if not its back to the drawing board of understanding!

    That would explain a reason to me why this throws the error i mentioned though >>

    Code:
    class SearchArea
        {
          public:
                 int Fscore;                               //total value to asses select node or not
                 int Gscore = 0;                         //movement cost
                 int Hscore;                                //heurestic             
                 int Walkstate;                           //is it valid terrain
                 int Liststate;                           //is it available? on open or closed list             
                 int STnode;                               //is it a / the search start point 
                 int TGnode;                              //is it a / the search target point             
                 int Xcord;                                 // map node X (across) location
                 int Ycord;                                  // map node Y (down) location
           
          };
    in the example above i get the error ISO Forbids initialisation of variable 'Gscore'

    is this becuase i have not actually created any instances of this class at this point and have just declared a class.
    Last edited by rogster001; 09-14-2009 at 08:06 AM.

  14. #14
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    No, it is not done automatically that way. And I believe you just cannot initialize Gscore by that way. You have to do it inside the constructor. Lets say you have this class:
    Code:
    class SearchArea
    {
    public:
                 int Fscore;                               //total value to asses select node or not
                 int Gscore;                         //movement cost
                 int Hscore;                                //heurestic                    
    };
    You want Fscore to be initialized to 1, Gscore to 0 and Hscore to 100. You will use a constructor for this job. Thus:
    Code:
    class SearchArea
    {
    public:
                 int Fscore;                               //total value to asses select node or not
                 int Gscore;                         //movement cost
                 int Hscore;                                //heurestic      
                 SearchArea()
                 {
                      Fscore = 1;
                      Gscore = 0;
                      Hscore = 100;
                 }              
    };
    Now when you do this:
    Code:
    SearchArea saObj;
    Now the member variables of saObj are 1, 0, 100.
    If you want to change default values every time you call an object you can do this:
    Code:
    class SearchArea
    {
    public:
                 int Fscore;                               //total value to asses select node or not
                 int Gscore;                         //movement cost
                 int Hscore;                                //heurestic      
                 SearchArea(int f, int g, int h)
                 {
                      Fscore = f;
                      Gscore = g;
                      Hscore = h;
                 }       
    };
    Now lets say you want Fscore = 12, Gscore = 13, Hscore = 0. You would make an object like this:
    Code:
    SearchArea saObj(12, 13, 0);
    That is the use of the constructor

  15. #15
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    To be even more clear what you want is this:
    Code:
    class SearchArea
        {
          public:
                 int Fscore;                               //total value to asses select node or not
                 int Gscore = 0;                         //movement cost
                 int Hscore;                                //heurestic             
                 int Walkstate;                           //is it valid terrain
                 int Liststate;                           //is it available? on open or closed list             
                 int STnode;                               //is it a / the search start point 
                 int TGnode;                              //is it a / the search target point             
                 int Xcord;                                 // map node X (across) location
                 int Ycord;                                  // map node Y (down) location
           
          };
    Which is WRONG. The exact equivalent, but CORRECT, would be this:
    Code:
    class SearchArea
        {
          public:
                 int Fscore;                               //total value to asses select node or not
                 int Gscore;                         //movement cost
                 int Hscore;                                //heurestic             
                 int Walkstate;                           //is it valid terrain
                 int Liststate;                           //is it available? on open or closed list             
                 int STnode;                               //is it a / the search start point 
                 int TGnode;                              //is it a / the search target point             
                 int Xcord;                                 // map node X (across) location
                 int Ycord;                                  // map node Y (down) location
                 SearchArea() { Gscore = 0; }
          };

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 01:24 PM
  2. Looking for a way to store listbox data
    By Welder in forum C Programming
    Replies: 20
    Last Post: 11-01-2007, 11:48 PM
  3. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 05:58 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. Replies: 4
    Last Post: 12-12-2002, 01:32 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21