Instance variable and pointer variable

Discussion in 'C++' started by Prasanth, May 16, 2010.

  1. Prasanth

    Prasanth Guest

    I am novice to c++,
    I have the following code :

    class A{
    int c;
    };

    What is the difference between the declarations

    A a;
    A *a = new A();

    While creating any object compiler checks for whether there is enough
    memory or not. cane someone please elaborate in detail
    what happens in the perspective of the memory allocations in both the
    cases.... ??? And in what situations we use this.. ??
     
    Prasanth, May 16, 2010
    #1
    1. Advertising

  2. Prasanth

    Rolf Magnus Guest

    Prasanth wrote:

    > I am novice to c++,
    > I have the following code :
    >
    > class A{
    > int c;
    > };


    Note that this class's only member is not accessible.

    > What is the difference between the declarations
    >
    > A a;
    > A *a = new A();
    >
    > While creating any object compiler checks for whether there is enough
    > memory or not. cane someone please elaborate in detail
    > what happens in the perspective of the memory allocations in both the
    > cases.... ??? And in what situations we use this.. ??


    Doesn't your C++ book describe this in detail already?
     
    Rolf Magnus, May 16, 2010
    #2
    1. Advertising

  3. Prasanth

    Paul N Guest

    On 16 May, 15:11, Prasanth <> wrote:
    > I am novice to c++,
    > I have the following code :
    >
    > class A{
    >              int c;
    >
    > };
    >
    > What is the difference between the declarations
    >
    > A a;
    > A *a = new A();
    >
    > While creating any object compiler checks for whether there is enough
    > memory or not. cane someone please elaborate in detail
    > what happens in the perspective of the memory allocations in both the
    > cases.... ??? And in what situations we use this.. ??


    Here's an example to illustrate the difference:

    A *x; // global variable

    void fun() {
    A a;
    A *b = new A();
    x = b;
    }

    In this case, a is created when the function starts, and is destroyed
    again when the function finishes. On the other hand, the one pointed
    at by b is created when the function gets to the "new", but continues
    to live until it is explicitly destroyed by using "delete". I've added
    a global variable just so that there is a way for other parts of the
    program can find the created object. Better methods are available.

    Hope that helps.
    Paul.
     
    Paul N, May 16, 2010
    #3
  4. Prasanth

    red floyd Guest

    On 5/16/2010 7:11 AM, Prasanth wrote:
    > I am novice to c++,
    > I have the following code :
    >
    > class A{
    > int c;
    > };
    >
    > What is the difference between the declarations
    >
    > A a;
    > A *a = new A();
    >
    > While creating any object compiler checks for whether there is enough
    > memory or not. cane someone please elaborate in detail
    > what happens in the perspective of the memory allocations in both the
    > cases.... ??? And in what situations we use this.. ??


    Your answer can be found in FAQ 5.2

    http://www.parashift.com/c -faq-lite/how-to-post.html#faq-5.2
     
    red floyd, May 16, 2010
    #4
  5. Prasanth

    Helge Kruse Guest

    Helge Kruse, May 17, 2010
    #5
  6. Prasanth

    Prasanth Guest

    On May 16, 7:59 pm, Paul N <> wrote:
    > On 16 May, 15:11, Prasanth <> wrote:
    >
    >
    >
    >
    >
    > > I am novice to c++,
    > > I have the following code :

    >
    > > class A{
    > >              int c;

    >
    > > };

    >
    > > What is the difference between the declarations

    >
    > > A a;
    > > A *a = new A();

    >
    > > While creating any object compiler checks for whether there is enough
    > > memory or not. cane someone please elaborate in detail
    > > what happens in the perspective of the memory allocations in both the
    > > cases.... ??? And in what situations we use this.. ??

    >
    > Here's an example to illustrate the difference:
    >
    > A *x; // global variable
    >
    > void fun() {
    > A a;
    > A *b = new A();
    > x = b;
    >
    > }
    >
    > In this case, a is created when the function starts, and is destroyed
    > again when the function finishes. On the other hand, the one pointed
    > at by b is created when the function gets to the "new", but continues
    > to live until it is explicitly destroyed by using "delete". I've added
    > a global variable just so that there is a way for other parts of the
    > program can find the created object. Better methods are available.
    >
    > Hope that helps.
    > Paul.


    Thanks for the answer...

    Thanks,
    Prasanth
     
    Prasanth, May 17, 2010
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gerry Sutton
    Replies:
    1
    Views:
    542
    Peter Otten
    Apr 16, 2005
  2. Replies:
    10
    Views:
    704
    Chris Torek
    Feb 4, 2005
  3. jimjim
    Replies:
    16
    Views:
    844
    Jordan Abel
    Mar 28, 2006
  4. David Garamond
    Replies:
    5
    Views:
    249
    Ara.T.Howard
    Jun 8, 2004
  5. Leon Bogaert
    Replies:
    19
    Views:
    329
    Robert Klemme
    Mar 23, 2008
Loading...

Share This Page