Thread: Question regarding constructors and memory.....

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    93

    Arrow Question regarding constructors and memory.....

    Hello.

    I have a general question regarding constructors and memory.

    As I believe, when you compile and run a program a certain amount of memory is put aside for the program and all the variables and content of that program correct?

    Today I was taught that I should always initialize my content with a constructor. We exercised this through a program creating and manipulating fractions.


    Default constructor
    Code:
    Fraction () {init(0,1)};
    Overloaded constructor
    Code:
    Fraction(int n, int d){init(n,d);}
    Today I was taught that we need a default constructor and an overloaded constructor.

    Now my question that I missed out on asking during the lecture was, why exactly do we need a default constructor to initialize?

    I thought that it is not really needed because compiling and running a program will set aside "clean memory" for that process.

    I was told that if we did not, than garbage (junk memory) would be contained in those variable slots? Correct?

    Am I confusing myself here?

    I would appreciate any feedback to guide me in understanding this new concept

    Thanks in advance.

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    I believe it depends on the compiler (correct me if I'm wrong). initialization or zeroing out of memory takes time. If they choose not to add that overhead when developing the compiler, it will be ultimately faster. Programmers should ALWAYS initialize their own variables.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    In general, you cannot assume that a variable will be initialized to a specific value unless you do it yourself. There are some instances where the rules of the language stipulate that the value be zero-initialized, but member variables are not one of them.

    BTW, you don't necessarily need a default constructor and an overloaded constructor here. For this particular class your instructor might be saying that the requirements dictate that both constructors are necessary. Nothing in the language or the information you provided so far indicates that this is true, though.

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> ...but member variables are not one of them.
    There are rules for zero-initializing member variables...

    >> In general, you cannot assume...
    Very true, since there are compilers that don't follow the rules - and the rules changed

    comp.lang.c++.moderated - Default initialization of fundamental types

    gg

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Now my question that I missed out on asking during the lecture was, why exactly do we need a default constructor to initialize?I thought that it is not really needed because compiling and running a program will set aside "clean memory" for that process.
    It's not only about initializing your member variables. If you define any constructor for your class, then the compiler won't supply a default constructor. That means you cannot create objects like this:

    Code:
    MyClassName obj;
    If you try that, you will get an error that essentially says, "you are trying to call a constructor that takes no arguments, but there isn't one."

    Then, the reason you don't just define the defalut constructor like this:

    Code:
    MyClassName(){}
    is to make sure you don't have junk values in the memory set aside for the member variables.

    I thought that it is not really needed because compiling and running a program will set aside "clean memory" for that process.
    I've never heard anyone say that, I've never seen anyone post that, and I've never read that anywhere. Although, in Java the member variables are initialized to 0, \u000, or null.

    It's kind of analogous to the right and left associativity rules and operator precedence. You can certainly memorize them and set up your expressions in accordance with those rules, or you can can concentrate on learning more important things and just use parentheses to force the precedence you want, which also makes it absolutely clear what is happening to anyone reading the code.
    Last edited by 7stud; 05-25-2005 at 02:43 AM.

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    In this case you could actually get by with a single constructor that did the job of both by using default arguments:

    Code:
    class Fraction
    {
        int _n;  // Numerator
        int _d;  // Denominator
        void init(int n,int d) { _n = n; _d = d; }
    public:
        Fraction(int n = 0, int d = 1) {init(n,d);}
    };
    
    ...
    
    Fraction f1;       // Uses default arguments... 0/1
    Fraction f2(5,8);  // Uses user supplied arguments... 5/8
    Fraction f3(6);    // Uses user supplied first argument, default second argument... 6/1
    Last edited by hk_mp5kpdw; 05-25-2005 at 05:37 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Quote Originally Posted by Codeplug
    >> ...but member variables are not one of them.
    There are rules for zero-initializing member variables...

    >> In general, you cannot assume...
    Very true, since there are compilers that don't follow the rules - and the rules changed

    comp.lang.c++.moderated - Default initialization of fundamental types

    gg
    Excellent information. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question about class members and constructors
    By Megidolaon in forum C++ Programming
    Replies: 5
    Last Post: 01-30-2009, 03:01 PM
  2. question about constructors and exceptions
    By Elkvis in forum C++ Programming
    Replies: 3
    Last Post: 12-05-2008, 06:15 PM
  3. Shouldn't I free the memory I malloced in constructors?
    By manugarciac in forum C++ Programming
    Replies: 10
    Last Post: 04-26-2007, 07:03 PM
  4. Inheritance and Dynamic Memory Program Problem
    By goron350 in forum C++ Programming
    Replies: 1
    Last Post: 07-02-2005, 02:38 PM
  5. Replies: 8
    Last Post: 10-12-2004, 11:41 AM