Click here to Skip to main content
15,897,891 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
class Point
{
private:
		int x;
		int y;

public:
Point()
{
	x=y=0;
}
Point(int _x,int _y)
{
	x=_x;
	y=_y;
}
void  Show()
{
	cout<<"X ="<<x<<" ,Y ="<<y<<endl;
}
};
void main()
{
	Point* x=new Point();
	x->Show();
	Point X2;
	X2.Show();
		
	Point* y=new Point(1,2);
	y->Show();
	Point Y2(1,2);
	Y2.Show();
getch();
}

what is different between this two way (use new key word or without it)??
Posted

With "new", you create an object on heap, and the pointer to its object on stack. Without "new" new, you create the object itself on stack. The work with objects on stack and on heap lie deep in the fundamentals of the technology, you will need to understand it all extremely well, to do any programming at all.

—SA
 
Share this answer
 
Comments
Kenneth Haugland 5-Apr-13 9:29am    
HE made both objects on the heap and stack with his little snipplet didnt he?
Sergey Alexandrovich Kryukov 5-Apr-13 9:33am    
No, please look again. He has x, y (heap), X2, Y2 (stack). I also thought there were only on heap at first glance...
—SA
CPallini 5-Apr-13 9:42am    
5.
Sergey Alexandrovich Kryukov 5-Apr-13 10:11am    
Thank you, Carlo.
—SA
With new you allocate a piece of memory from the heap you get a pointer to that memory. That implies that you are responsible for deleting that memory some time later.

The syntax
C++
Point x2;

allocates a piece of memory on the stack and refer to it with the x2 variable. When your function returns the memory will automatically be freed. (And hence you should never return the address of such a stack object!).

Allocation on the stack is faster than allocation on the heap. However the size of the stack is usually much smaller than the memory of the heap. Hence you should allocate huge chunks of memory from the heap.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 5-Apr-13 9:36am    
Sure, a 5.
—SA
nv3 5-Apr-13 9:40am    
Thanks!
CPallini 5-Apr-13 9:42am    
5.
nv3 5-Apr-13 10:21am    
Thank you!
In the first case (new) you are dynamically allocating memory (on the heap) for the object and you have to explicitely delete it (I remark it since you did not delete it in your code).
In the second case (without new) you are allocating statically memory (on the stack).
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 5-Apr-13 9:35am    
Sure, a 5. I just did not find it appropriate to explain all aspects. If you start with it, you will probably need to explain many other things, how to use it; what happens if you have memory leaks, destructors, and a lot more.
—SA
josda1000 8-Apr-13 11:05am    
This is getting a 5 for specifying the deletion necessity.
The variable x is declared as a pointer to an object, so must be initialised with the new operator, so it points to a new instance of the Point class.

The variable X2 is declared as a local instance of the object and automatically initialised, so it can be used as coded.

The same with y and Y2. If you step through your code with the debugger you will see what happens at every stage.
 
Share this answer
 
Comments
CPallini 5-Apr-13 9:44am    
My 5, however "so must be initialised with the new operator" is incorrect:
int i, * p; i = 5; p = &i;
Richard MacCutchan 5-Apr-13 9:55am    
True, but I deliberately did not go into that as OP was asking for an explanation of the given code sample.
Minor differences from other solutions, but x and y are created on the heap, X2 and Y2 are created on the stack. When this program exits, x and y will leak memory and the class destructors will not execute. X2 and Y2 destructors will execute and there will be no memory leaks with them.
 
Share this answer
 
Comments
josda1000 8-Apr-13 11:06am    
Getting a 5 for talking about memory leaks.
H.Brydon 8-Apr-13 11:35am    
Thank you, will return the favor when I can.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900