Re: what wrong ??



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??


"Scot T Brennecke" <ScotBspamhater@xxxxxxxx> дÈëÏûÏ¢ÐÂÎÅ
:eIateTxgFHA.1248@xxxxxxxxxxxxxxxxxxxxxxx
> A2 and A3 are allocated on the stack as pointers to objects of class A.
For A2, you actually
> created a new A object, and assigned its address to the A2 pointer. But
you left A3 uninitialized.
> It is a pointer, but its value was never set to the address of any A
object. Its value remains
> whatever random "garbage" happened to be on the stack at that point.
>
> Your statements at (2) and (3) will not compile. A2 and A3 are pointers,
and you should use the ->
> operator to access their members. Even so, you'll get an access violation
in statement (3), because
> you are trying to access a non-existent object with your A3 pointer.:
> > cout<<A2->a<<endl;//right (2)
> > cout<<A3->a<<endl;//wrong,Unhandled exception:0xC0000005,access
violation??
> > (3)
>
>
> "collay" <mosan1981@xxxxxxx> wrote in message
news:uvDtt0wgFHA.3868@xxxxxxxxxxxxxxxxxxxxxxx
> > #include <iostream.h>
> > class A
> > {
> > public:
> > int a;
> > };
> >
> > void main(void)
> > {
> > A A1;
> > A* A2 = new A;
> > A* A3;
> > cout<<A1.a<<endl;//right (1)
> > cout<<A2.a<<endl;//right (2)
> > cout<<A3.a<<endl;//wrong,Unhandled exception:0xC0000005,access
violation??
> > (3)
> > }
> >
> > what`s wrong?
> > what did the new operator do when it allocated the memory?
> > what is the different between line(2) and line(3)??
> > Thank you!
> > --
> > collay
> >
> >
>
>


.



Relevant Pages

  • Re: Is There Any Reason to Even Use VC++ Anymore?
    ... If, for another reason, the calling function needs to allocate memory, ... It does this by taking a pointer to a ball object ...
    (microsoft.public.dotnet.languages.vc)
  • Re: Pointer to the out of scope local variables
    ... of a pointer, and jumped to the conclusion that you allocated the memory ... In this particular case the funtion buildPoint would allocate ... If the caller ...
    (microsoft.public.vc.language)
  • Re: Memory management and allocation
    ... > As I'm writing a piece of code that basically acts as a server and ... > memory management is a topic that is quite crucial. ... Or can I just allocate the variable ... Nor is it usually necessary to set the pointer to ...
    (comp.lang.c)
  • Re: Virtual Machine implementation problem, Please help me to spot the bug
    ... I kept on getting error messages. ... You don't allocate enough memory here. ... Again conversions between pointer and integer types. ...
    (comp.lang.c)
  • Re: This is getting really weird.
    ... I thought 4 bytes for reference count and 4 for string length. ... > There should be no memory allocation for that line. ... > manager may allocate more space than requested for its own efficiency. ... > that New returned with a pointer to the string constant. ...
    (alt.comp.lang.borland-delphi)

Quantcast