By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,955 Members | 1,756 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,955 IT Pros & Developers. It's quick & easy.

is int a class in c++

bajajv
100+
P: 152
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.
Aug 8 '07 #1
Share this Question
Share on Google+
4 Replies


Meetee
Expert Mod 100+
P: 931
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
Aug 8 '07 #2

weaknessforcats
Expert Mod 5K+
P: 9,197
First, ints do not have constructors.

This code won't even compile:

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

Expand|Select|Wrap|Line Numbers
  1. int* x = new int(10);
  2.  
This format of creating fundamental types:

Expand|Select|Wrap|Line Numbers
  1. int a(10);
  2. double d(2.5);
  3.  
was put into C++ for template writers who had templates like:

Expand|Select|Wrap|Line Numbers
  1. template<class T, class U>
  2. void MyFunction(T arg1, U arg2)
  3. {
  4.     T var1(arg1);
  5.     U var2(arg2);
  6. }
  7.  
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:

Expand|Select|Wrap|Line Numbers
  1.  T var1(arg1);
  2.  
is implemented as:

Expand|Select|Wrap|Line Numbers
  1.  T var1= static_cast<T> (arg1);
  2.  
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.
Aug 8 '07 #3

PieCook
P: 9
Yes, if I'm not mistaken, int x = new int(10) looks like some Java or C# code.
Aug 13 '07 #4

weaknessforcats
Expert Mod 5K+
P: 9,197
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*.
Aug 14 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.