troubleshooting Question

C++ dynamic creation of objects

Avatar of oggiemc
oggiemcFlag for Ireland asked on
C++
7 Comments3 Solutions308 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
Join our community to see this answer!
Unlock 3 Answers and 7 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 3 Answers and 7 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros