2

Possible Duplicates:
When to use “new” and when not to, in C++?
When should I use the new keyword in C++?

What is the difference between A a; and A a = new A(); ?

Edited:

A* a = new A();

My mistake.

Community
  • 1
  • 1
Don Lun
  • 2,459
  • 6
  • 25
  • 32
  • 2
    We need the definition of `A`. If `A` has no default constructor, what shall be explain? Both would be invalid, and marking any difference would not make sense. If `A` has a ctor taking a `A*` and a default ctor, or if `A` is `void*`, both are valid. We can't in full extent detail on the semantics. That's what books are for. – Johannes Schaub - litb May 02 '11 at 21:29
  • 4
    @Johannes: you are being pedantic. You have to try to meet the questioner half-way. Lots of questions on SO could be answered, "I have no idea what will happen, it depends on many factors you haven't shown us," but that's not helpful. You're allowed to make reasonable assumptions. – Ned Batchelder May 02 '11 at 21:41
  • 1
    @Ned pedantry is what programming is all about. –  May 02 '11 at 21:43
  • 1
    @Ned I learned to take questions seriously and to stop making assumptions. Assumptions you make are not necessarily obvious to people that don't understand as much as you do about the language. If someone doesn't know the language in that respect and searches google, he/she may read this question literally. – Johannes Schaub - litb May 02 '11 at 21:47
  • 1
    @Johannes: so you optimized your answer for some future searcher, at the expense of the OP? Very odd. Meanwhile you assumed that A was a defined class in the first place. If A had been a macro, this code could do absolutely anything! And if this code had been given literally to the C++ compiler, it would complain that A is undefined. You would help more people if you took questions a little less seriously. – Ned Batchelder May 02 '11 at 21:57
  • @Johannes: You're definitely right (and that's why I deleted my answer), but bear in mind that when you're dealing with a language like C that has a preprocessor, you can pretty much **never** be sure what you're seeing is what's actually happening. @Ned has a great point. – user541686 May 02 '11 at 22:13
  • @Ned Everywere when communicating, there are assumptions made. The whole question is, how much do you assume. For my taste, it clearly crosses the border in this case. – Johannes Schaub - litb May 02 '11 at 22:15
  • 1
    Actually, my point was sarcasm trying to point out the absurdity of the "you haven't told us everything we need to know" push-back... – Ned Batchelder May 02 '11 at 22:16
  • @Johannes, although I agree with Ned that it's kind of pedantic, I did try to add the fact that the default constructor is called to my answer and that none of the code in my answer will compile without a default constructor for A. – Doug T. May 02 '11 at 22:17
  • @Mehrdad, I disagree with the dup, this is subtly different. – Doug T. May 02 '11 at 22:18

3 Answers3

9

When inside a function,

A a

declares a variable on the stack and calls A's default constructor* on a. This variable is automatically cleaned up when the variable goes out of scope.

A a = new A(); 

won't compile, however

A* a = new A(); 

creates a new A on the heap and calls A's default constructor* on the newly allocated memory. Then the expression in turn evaluates to a pointer to the new A, which the variable a is initialized to . You're in charge of managing this memory, so you need to be sure to later delete it with delete:

delete a;

or else you'll have a memory leak

See this question to learn more about the differences between the stack and the heap.

* None of this code will compile if A doesn't have a default constructor. A default constructor is either defined by you or implicitly provided by the compiler. See here for more on default constructors.

Community
  • 1
  • 1
Doug T.
  • 59,839
  • 22
  • 131
  • 193
1

Doug T :

A a declares a variable on the stack.

Incorrect : A a declares a variable and allocated the memory for it, either on the stack or in global memory space depending on what scope it has.

Consider also where in memory

static A a

is located (Global memory space - not stack, not heap).

mattnz
  • 519
  • 2
  • 13
0

The second is invalid. new allocates space and returns a pointer. Use A* a = new A();

ultifinitus
  • 1,683
  • 2
  • 17
  • 31