zeroing out default constructor?

This is a discussion on zeroing out default constructor? within the C++ Programming forums, part of the General Programming Boards category; what do they mean by zeroing out default constructor? what it does? why its good? and how do i do ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    84

    zeroing out default constructor?

    what do they mean by zeroing out default constructor? what it does? why its good? and how do i do it?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,457
    Perhaps they meant "zeroing out in the default constructor" or something like that, i.e., implementing the default constructor to initialise member variables to zero.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    84
    Quote Originally Posted by laserlight View Post
    Perhaps they meant "zeroing out in the default constructor" or something like that, i.e., implementing the default constructor to initialise member variables to zero.
    well i thought this is the case but it always gives me an error when i try to do so

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Like this?

    Code:
    class X
    {
        int a, b;
    public:
        X(): a(0), b(0) {}
    };
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,457
    Quote Originally Posted by ExDHaos
    well i thought this is the case but it always gives me an error when i try to do so
    What did you try?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    84
    Quote Originally Posted by laserlight View Post
    What did you try?
    zeroing out like anon did

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    84
    Code:
    	Races(): raceIDnu(0), racePowe(0), raceName(0), raceEner(0), racePowP(0), raceAttr[0](0), raceAttr[1](0),
    		raceAttr[2](0), raceAttr[3](0), raceResE[0](0), raceResE[1](0), raceResE[2](0), raceResP[0](0),
    		raceResP[1](0), raceResP[2](0), raceResM[0](0), raceResM[1](0), raceResM[2(0){}
    ------ Build started: Project: Helios RPG - Beta, Configuration: Release Win32 ------
    Compiling...
    Main.cpp
    c:\documents and settings\eng\my documents\visual studio 2008\projects\project1\helios rpg - beta\helios rpg - beta\Races.h(27) : error C2958: the left bracket '[' found at 'c:\documents and settings\eng\my documents\visual studio 2008\projects\project1\helios rpg - beta\helios rpg - beta\races.h(27)' was not matched correctly
    c:\documents and settings\eng\my documents\visual studio 2008\projects\project1\helios rpg - beta\helios rpg - beta\Races.h(25) : error C2059: syntax error : '['
    Build log was saved at "file://c:\Documents and Settings\eng\My Documents\Visual Studio 2008\Projects\Project1\Helios RPG - Beta\Helios RPG - Beta\Release\BuildLog.htm"
    Helios RPG - Beta - 2 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,457
    Ah, so you are trying to zero initialise an array member variable. You should be able to use value initialisation, if I am not wrong:
    Code:
    Races() : raceIDnu(0), racePowe(0), raceName(0), raceEner(0), racePowP(0), raceAttr(), raceResE(), raceResP(), raceResM() {}
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    84
    Quote Originally Posted by laserlight View Post
    Ah, so you are trying to zero initialise an array member variable. You should be able to use value initialisation, if I am not wrong:
    Code:
    Races() : raceIDnu(0), racePowe(0), raceName(0), raceEner(0), racePowP(0), raceAttr(), raceResE(), raceResP(), raceResM() {}
    hehe, again i made mistake because arrays but still theres a problem, now it gives me a runtime error/bug
    Unhandled exception at 0x7855b690 in Helios RPG - Beta.exe: 0xC0000005: Access violation reading location 0x00000000.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,457
    Please show the code.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    May 2009
    Posts
    84
    Quote Originally Posted by laserlight View Post
    Please show the code.
    Code:
    #include <string>
    using namespace std;
    
    #define MAX_RACES 3
    #define SK charRace->racePower
    #define IDr charRace->raceIDnu
    #define MANA_S 1
    #define SPIRIT_S 2
    #define ANCIENA_S 3
    
    class Races
    {
    public:
    
    	int raceIDnu;
    	int racePowe;
    	string raceName;
    	string raceEner;
    	string racePowP;
    	float raceAttr[4];
    	float raceResE[3];
    	float raceResP[3];
    	float raceResM[3];
    
    	Races() : raceIDnu(0), racePowe(0), raceName(0), raceEner(0), racePowP(0), raceAttr(), raceResE(), raceResP(),
    		raceResM(){}
    
    	
    	void raceInfo (int iValue[], string sString[], float dValue[])
    	{
    		raceIDnu = iValue[0];
    		racePowe = iValue[1];
    	    raceName = sString[0];
    	    raceEner = sString[1];
    	    racePowP = sString[2];
    		raceAttr[0] = dValue[0];
    		raceAttr[1] = dValue[1];
    		raceAttr[2] = dValue[2];
    		raceAttr[3] = dValue[3];
    	    raceResE[0] = dValue[4];
    		raceResE[1] = dValue[5];
    		raceResE[2] = dValue[6];
    	    raceResP[0] = dValue[7];
    		raceResP[1] = dValue[8];
    		raceResP[2] = dValue[9];
    	    raceResM[0] = dValue[10];
    		raceResM[1] = dValue[11];
    		raceResM[2] = dValue[12];}
    
    }racesDB[MAX_RACES], *charRace;
    
    void LoadRaces()
    {
    	int iValue_0[] = {1, MANA_S};
    	string sValue_0[] = {"Haijemarian", "Spirit", "SP"};
    	float fValue_0[] = {1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5};
    	racesDB[0].raceInfo(iValue_0, sValue_0, fValue_0);
    
    	int iValue_1[] = {2, SPIRIT_S};
    	string sValue_1[] = {"a", "a", "a"};
    	float fValue_1[] = {1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5};
    	racesDB[1].raceInfo(iValue_1, sValue_1, fValue_1);
    
    	int iValue_2[] = {3, ANCIENA_S};
    	string sValue_2[] = {"a", "a", "a"};
    	float fValue_2[] = {1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5};
    	racesDB[2].raceInfo(iValue_2, sValue_2, fValue_2);
    }
    latter i want to rep;ace the arrays with vectors

  12. #12
    Registered User
    Join Date
    May 2007
    Posts
    147
    latter i want to replace the arrays with vectors
    ...maybe not....


    First off, there's no need to 'zero' out the strings. They'll be handled already by the simple fact that they're strings from STL, and their default initialization is already such. (In fact, supplying zero in this manner causes an error in my IDE).

    Also, the initial state of a vector will be 'empty' if you choose to use them, and you'll need to push values into the array to 'get them started' - but, you can't do that with the initialization syntax, you'll need to push those floats in the constructor.

    An array of floats can be quite simple, and if the bounds of the array are very clear in your code, the speed of a simple array over a vector (for initialization and simple storage) is hard to resist in this context. It's one of the rare contexts in which arrays can make more sense than vectors (fixed length arrays, private members that the owning object can 'check' for bounds, etc).

  13. #13
    Registered User
    Join Date
    May 2009
    Posts
    84
    Quote Originally Posted by JVene View Post
    ...maybe not....


    First off, there's no need to 'zero' out the strings. They'll be handled already by the simple fact that they're strings from STL, and their default initialization is already such. (In fact, supplying zero in this manner causes an error in my IDE).

    Also, the initial state of a vector will be 'empty' if you choose to use them, and you'll need to push values into the array to 'get them started' - but, you can't do that with the initialization syntax, you'll need to push those floats in the constructor.

    An array of floats can be quite simple, and if the bounds of the array are very clear in your code, the speed of a simple array over a vector (for initialization and simple storage) is hard to resist in this context. It's one of the rare contexts in which arrays can make more sense than vectors (fixed length arrays, private members that the owning object can 'check' for bounds, etc).
    oh that's great if u right, tbh i wanted to use vectors only because people said its better to use them

  14. #14
    Registered User
    Join Date
    May 2007
    Posts
    147
    It generally is better to use vectors.

    The trick is to recognize a situation where a simple array MIGHT be a better choice.

    Let's analyze a little more about why (and why not).


    You have a few entries in each array (4 at most now, but even if it were 8 or 10, it's a small quantity).

    The storage is part of an object; you're not dynamically allocating the array, you're using a fixed size within the object, which itself will be destroyed, taking the array(s) with it.

    The arrays should be private members (they're not in your case, yet). By making them private members your object can control access in such a way that reaching beyond the limit of the array may be prevented.


    Counter that with the use of vectors.

    The vector is going to start out with zero entries. You can choose to reserve a few space, but that will invoke dynamic memory allocation (taking time), and you'll have to initialize with push_back to create sufficient entries in the vector.

    When the owning object is destroyed, it will take some time for the vector to be destroyed (a delete will have to be called on the dynamic space allocated).



    This is a time/convenience compromise. In most circumstances, the array's safety and it's own kind of convenience would win out, and be a better choice. If the size of the array were not fixed, or if the size were larger, or if the storage of the array was dynamically allocated (off a pointer) instead of part of a large class, the vector is clearly superior.

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,457
    Quote Originally Posted by JVene
    and you'll have to initialize with push_back to create sufficient entries in the vector.
    This is not true since one could just initialise the vectors to be of the desired size.

    EDIT:
    The problem of array bounds still applies here. For example, the raceInfo() member function assumes that the arrays referenced by its pointer parameters have at least three or four elements, but a mistake by the caller could then lead to array out of bounds access.

    std::tr1::array can help alleviate this problem by allowing you to have fixed size arrays that know their own size, with no or almost no overhead over using arrays directly.
    Last edited by laserlight; 05-17-2009 at 12:26 PM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. default constructor
    By rahulsk1947 in forum C++ Programming
    Replies: 1
    Last Post: 06-10-2009, 12:52 PM
  2. Creating array of objects w/o default constructor
    By QuestionC in forum C++ Programming
    Replies: 19
    Last Post: 05-02-2007, 08:03 PM
  3. template class default constructor problem
    By kocika73 in forum C++ Programming
    Replies: 3
    Last Post: 04-22-2006, 09:42 PM
  4. constructors
    By shrivk in forum C++ Programming
    Replies: 7
    Last Post: 06-24-2005, 09:35 PM
  5. Need help in classes
    By LBY in forum C++ Programming
    Replies: 11
    Last Post: 11-26-2004, 03:50 AM

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