Link to home
Create AccountLog in
Avatar of oggiemc
oggiemcFlag for Ireland

asked on

C++ dynamic creation of objects

Hello all,

I have a couple of questions reagarding the new keyword in C++. First, can someone tell exactly what is the difference between the following two code segments:

Account a = Account(35.00, 34234324); 
    Account *ptrA = &a;

Account *a = new Account("John", 10.50, 123456);

Open in new window


Secondly, Ive read that the following segment of code will lead to a memory leak after the b = a assignment is made. My question is, will the second segment of code also lead to a memory leak?

Thanks

int main()
 {
   Account *a = new Account("John", 10.50, 123456);  
   Account *b = new Account("Derek", 12.07, 123457);

   a->display();
   b->display();

   b = a;

   a->display();
   b->display();
 }

Open in new window


int main()
  {
    Account a = Account(35.00, 34234324); 
    Account *ptrA = &a; 1

    CurrentAccount b = CurrentAccount(50.0, 12345, 200.0);
    CurrentAccount *ptrB = &b; 2

    cout << "Displaying ptrA:" << endl;
    ptrA->display(); 3
    cout << "Displaying ptrB:" << endl;
    ptrB->display(); 4


    ptrA = ptrB; 6
    cout << "Displaying ptrA again:" << endl;
    ptrA->display();  7
  }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
BTW, you might want to check out smart pointers (http://en.wikipedia.org/wiki/Smart_pointer) to avoid such leaks. The Wikipedia article has both examples and further links.
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
first:  static variable
Account a = Account(35.00, 34234324);
Account *ptrA = &a;

a have alloc memory in stack and it will automatically free when the function finish.
ptrA = &a : it only create a pointer not a new object.

second: dynamic variable

Account *a = new Account("John", 10.50, 123456);
 create a object in heap memory an return a pointer.
 you can access object through pointer.
and when you not need object you have to free it by statement:
delete a;
Do you also happen to know the operator precedence rules for '*facepalm*'? Sorry, but that had already been mentioned.
There is NO memory leak in your 3rd snippet. It is perfectly OK

Account a = Account(35.00, 34234324);
Account *ptrA = &a;  

// ptrA only takes the address of a. a exists on the stack.
// Another way to say that is that ptrA does not "own" a it just points to it.

    CurrentAccount b = CurrentAccount(50.0, 12345, 200.0);
    CurrentAccount *ptrB = &b; 2

// ptrB only takes the address of b. b exists on the stack.

    ptrA = ptrB; 6

// ptrA now points to b

    cout << "Displaying ptrA again:" << endl;
    ptrA->display();  7

// no need to delete, actually it would probably crash if you did so because
// the objects exist on stack.
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account