is int a class in c++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bajajv
    New Member
    • Jun 2007
    • 152

    is int a class in c++

    Hi,
    I have a basic doubt.
    Is int a class?
    If we say, int x = new int(10);
    then will it call a constructor of int?

    If yes, then it means that when we do something like this,
    int* x = new int[5];
    _______
    _______
    delete x;

    then it should be a memory leak.
    And the correct way should be
    delete[] x;
    But that is not required, and we continue with delete x;
    how?
    Or, am I wrong somewhere?
    Please clearify.

    Thanks.
  • Meetee
    Recognized Expert Contributor
    • Dec 2006
    • 928

    #2
    Originally posted by bajajv
    Hi,
    I have a basic doubt.
    Is int a class?
    If we say, int x = new int(10);
    then will it call a constructor of int?

    If yes, then it means that when we do something like this,
    int* x = new int[5];
    _______
    _______
    delete x;

    then it should be a memory leak.
    And the correct way should be
    delete[] x;
    But that is not required, and we continue with delete x;
    how?
    Or, am I wrong somewhere?
    Please clearify.

    Thanks.
    Hi,

    The example you have given is the way to declare an array of integers. And as memory is allocated to the pointer you need to delete it using delete [] x.

    Regards

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      First, ints do not have constructors.

      This code won't even compile:

      Originally posted by bajajv
      int x = new int(10);
      since new returns the address of an int initialized with 10. The code should be:

      [code=cpp]
      int* x = new int(10);
      [/code]

      This format of creating fundamental types:

      [code=cpp]
      int a(10);
      double d(2.5);
      [/code]

      was put into C++ for template writers who had templates like:

      [code=cpp]
      template<class T, class U>
      void MyFunction(T arg1, U arg2)
      {
      T var1(arg1);
      U var2(arg2);
      }
      [/code]

      If T and U were classes, then constructors would be called but if T and U were int and double, the template would not compile since fundamental types have no constructors. Hence, the syntax was changed to allow fundamental types to be intialized as though they had constructors.

      The form:

      [code=cpp]
      T var1(arg1);
      [/code]

      is implemented as:

      [code=cpp]
      T var1= static_cast<T> (arg1);
      [/code]

      Finally, if you haave an array of class objects and you delete, then the destructor is call only on element 0. Only when you delete [] is the destructor called on each element of teh array.

      The business of using the [] with the delete of an array has been a long debate. One side maintains that no case to date has been identified where the programmer only wants the destructor called on element 0. The other side maintains that just because no case has ever been identified doesn't not mean that such a case does not exist (you can't prove a negative). So, to be sure C++ doesn't let you down in some bizarre circumstance, the [] are to be used with the delete of arrays if you want your arrays cleaned up completely.

      Comment

      • PieCook
        New Member
        • Jul 2007
        • 9

        #4
        Yes, if I'm not mistaken, int x = new int(10) looks like some Java or C# code.

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          Originally posted by PieCook
          Yes, if I'm not mistaken, int x = new int(10) looks like some Java or C# code.
          That's as may be. But in C++, the new operator returns the address of element 0 of the array. You need to receive that in a pointer declared as int*.

          Comment

          Working...