Avatar of oggiemc
oggiemc
Flag 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

C++

Avatar of undefined
Last Comment
ambience

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
jkr

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
jkr

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
Minh Võ Công

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Minh Võ Công

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;
jkr

Do you also happen to know the operator precedence rules for '*facepalm*'? Sorry, but that had already been mentioned.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
ambience

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
ambience

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.