Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Troubleshooting
Research
Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

C++ dynamic creation of objects

Avatar of oggiemc
oggiemcFlag for Ireland asked on
C++
7 Comments1 Solution308 ViewsLast Modified:
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);

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();
 }

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
  }
ASKER CERTIFIED SOLUTION
Avatar of jkr
Commented:
This problem has been solved!
Unlock 1 Answer and 7 Comments.
See Answers