Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I'm a python programmer who sees a lot of C++ code but doesn't know the language and there seems to be two ways to initalize a class. I was hoping that someone can tell me the difference.

class Point {
        int x, y;
     public:
        int get_x();
        int get_y();
        Point(int, int);
}

// ok we are going to leave the methods and constructors unimplemented for brevity
// now here's the part that puzzles me
Point p(5, 5); // ..or
Point p = new Point(5, 5);

What's the diffrence between the two?

share|improve this question

2 Answers 2

up vote 20 down vote accepted

One of those variables lives on the stack, the other on the heap. The correct syntax is this:

Point p(5, 5); 
Point* p = new Point(5, 5);

The first one creates an object on the stack. When p goes out of scope, it'll be removed automatically. The second, creates a point object on the heap. Until you remove it manually, it'll remain in memory.

In the second case, p is a pointer to the Point object, while in the first case p is the point object.

Python essentially does only the second version and doesn't do the first. Python is garbage collected, so it'll also remove the point object from memory if it is no longer referenced.

However, I should mention (see comments) that the prefered method to create objects on the heap is to use smart pointers. See DeadMG's answer for more details.

share|improve this answer
7  
The "Don't answer his question just tell him more about smart pointers!" crowd is back and louder than ever. –  Stargazer712 Jul 19 '12 at 20:20
5  
Really? Why not? They are much more important than raw new in every conceivable fashion and you must know them to use heap memory safely –  DeadMG Jul 19 '12 at 20:23
7  
The question is explicitly about the differences between the two, not about the best practices in C++. Why don't we just start talking about RAII as well while we're here. I am not condoning using raw pointers. I am answering the question –  Oleksi Jul 19 '12 at 20:31
7  
+1 for answering the question that was asked instead of going off on a diatribe. –  Dunk Jul 19 '12 at 20:41
7  
@DeadMG, a smart pointer is an abstraction, and abstractions always "leak". The OP does not know C++, or what a pointer is. He is looking at some code, and he is trying to understand what it does. Talking about smart pointers in this context does not help him understand the code he is looking at, and is likely to confuse him even more. –  Dima Jul 19 '12 at 20:50
Point p(5, 5); // ..or

Creates an automatic variable "on the stack". This means that it's lifetime and memory is fixed to it's scope.

Point* p = new Point(5, 5);

Creates a memory leak, double delete, or heap corruption in your program. This means that it almost certainly will crash your program, cause it to corrupt user data, or other fun side effects. Which is why actual C++ programmers use alternatives, such as...

auto p = make_unique<Point>(5, 5);

Creates a Point "on the heap". In this case, it's lifetime and memory is fixed to the lifetime of the owning unique_ptr<Point>. This ownership can be transferred by move semantics. Unfortunately, make_unique is not provided as Standard (a known defect) but it's relatively easy to roll your own. It is a simple factory function.

auto p = std::make_shared<Point>(5, 5);

Also creates a Point "on the heap". In this case, it's lifetime and memory is fixed to the lifetime of each shared_ptr<Point> that points to it. When they are all destroyed, the Point will be cleaned up. shared_ptr<Point> can be copied, unlike unique_ptr<Point>. This is very similar to Python's behaviour. However, do not forget that unlike in Python, there will be no machinery to break reference cycles.

share|improve this answer
13  
-1 - For incorrectly stating that Point* p creates a memory leak, double delete or heap corruption in your program. It only does that if you don't know what you are doing. –  Dunk Jul 19 '12 at 20:43
5  
It's not that extremely common at all really. You need to hang out with better programmers. –  James Jul 19 '12 at 21:15
3  
The OP just wants a little clarification about C++ syntax, not a silly rant about some guy's One True Way to code. –  Jim In Texas Jul 19 '12 at 21:43
6  
DeadMG, it seems as though every time there is a question about C++, you're always here to tell us about smart pointers. What is amazing is that the content of the question is irrelevant! You just tell people about smart pointers and move on. So now let me say it: We know. We get it. RAII is the best thing since sliced bread. Just answer his question and move on. After a while it just starts looking pathetic. –  Stargazer712 Jul 19 '12 at 21:58
2  
Yes. Answer the question first. Then add a follow up about better technique. The OP is obviously not a C++ programmer he does not need to wade threw the minutia first. –  Loki Astari Jul 20 '12 at 19:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.