Re: what wrong ??



[1] Yes. The standard behavior for the "new" operator is to allocate on the heap. It can actually
be overridden to behave differently; but if you don't change the default behavior, you'll get heap
memory.
[2] In the case of A1 and A2, you are allocating new A class objects (one on the stack, one on the
heap), and thereby invoking the A constructor for each. A2 is not an A class object as A1 is,
however. A2 is a pointer to an A class object. Same with A3. But since you did not tell the
compiler to create an A object for A3 to point to, no memory was allocated for an A object, and no
constructor will be called for that case. Stack memory was allocated for both A2 and A3 also. But
this memory is simply the 4 bytes each required to hold a pointer.

"collay" <mosan1981@xxxxxxx> wrote in message news:OPw3znxgFHA.2444@xxxxxxxxxxxxxxxxxxxxxxx
> Thank you!
> Yes,I have already known that I should use "->" operator.
> yet I have another two questions:
> [1]use "new" operator will allocate the memory space in heap area,I think.Am
> I right?
> [2]use"A A1" or "A* A2 = new A",it will allocate memory and invoke the class
> A`s constructor.
> but what is the action when I use "A* A3"? Does the compiler only
> allocate the memory and do not invoke the constructor??


.



Relevant Pages

  • Re: when can pass by value be dangerous?
    ... the object's constructor function is not called. ... > used as an argument allocates memory and frees that memory when it is ... Suppose the object has a pointer inside it to another, ... pointers to the SAME sub-object instead of to separate copies of that ...
    (comp.lang.cpp)
  • Re: Why it is not good code for constructor
    ... How can C's destructor free the memory? ... responsible for freeing it, but C::~C will not be called, and a function-try ... handler cannot access the pointer, while a catch handler in local scope will ... now if new intcompletes ok, but the constructor for A throws, the ...
    (microsoft.public.vc.language)
  • Re: what wrong ??
    ... A`s constructor. ... allocate the memory and do not invoke the constructor?? ... > created a new A object, and assigned its address to the A2 pointer. ... >> what did the new operator do when it allocated the memory? ...
    (microsoft.public.vc.debugger)
  • Re: strcpy and the copy constructor
    ... > Since you delcare Name as a char array the default copy constructor will ... it you change name to pointer then all bets are off. ... The defualt constructor gives the same crash. ... It appears as though the memory for the ...
    (comp.lang.cpp)
  • Re: Creating an array of objects
    ... If you can put the arguments into static/global variables, ... > But how can I use a constructor with arguments instead of the default ... >> pointer. ...
    (microsoft.public.vc.language)

Quantcast