help with dynamic memory

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Nov 2006
Posts: 11
Reputation: m3an is an unknown quantity at this point 
Solved Threads: 1
m3an m3an is offline Offline
Newbie Poster

help with dynamic memory

 
0
  #1
May 13th, 2007
its part of a dvd program. i want all the objects created and manipulated by the program to be stored in a dynamic array that might grow during the execution of the program. I want to program to store the info in a dynamic array that i can call anywhere to list the info.im having trouble with using dynamic memory and classes together, ...please help, thanks


  1.  
  2. class Names
  3. {
  4. private:
  5. string title;
  6. int i;
  7. int * p;
  8.  
  9. public:
  10. void enter_new(); // sets names of actors to the DVD
  11. }film;
  12.  
  13.  
  14. void Names::enter_new()
  15. {char ans;
  16. cout << "How many movies would you like to type? ";
  17. cin >> film.i;
  18. int ii= film.i;
  19. (film.p)= new int[ii];
  20.  
  21. if ((film.p) == 0)
  22. cout << "Error: memory could not be allocated";
  23. else
  24. {
  25. for (int a=1; a<(ii); a++)
  26. {cout<<"\n Please enter the name: "<<endl;
  27. cin >> (film.p)[a];
  28. }
  29. delete[] p;
  30. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,846
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 753
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: help with dynamic memory

 
0
  #2
May 13th, 2007
You're using strings, why not use vectors too? Much easier.
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 11
Reputation: m3an is an unknown quantity at this point 
Solved Threads: 1
m3an m3an is offline Offline
Newbie Poster

Re: help with dynamic memory

 
0
  #3
May 13th, 2007
i guess, easier for programmers, im just a beginner, elobrate a bit please?ill try with pointers tho, but im still confused abt my original question

im having trouble with using dynamic memory and classes together
,
Last edited by m3an; May 13th, 2007 at 6:19 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,846
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 753
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: help with dynamic memory

 
0
  #4
May 13th, 2007
>i guess, easier for programmers, im just a beginner
Easier for beginners too because you don't have to worry about managing the memory.

>im having trouble with using dynamic memory and classes together
Compare and contrast:
  1. void Names::enter_new()
  2. {
  3. cout<<"How many movies would you like to type? ";
  4. cin>> i;
  5.  
  6. p = new int[i];
  7.  
  8. for ( int a = 0; a < i; a++ ) {
  9. cout<<"\n Please enter the name: ";
  10. cin>> p[a];
  11. }
  12. }
You're not just working with the film object in a member function, you're working with any possible object of the Names class. Also, you want the memory to persist until the object is destroyed, which means not deleting it until the destructor is called. Finally, new doesn't return a null pointer if it fails anymore, it throws an exception.
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 11
Reputation: m3an is an unknown quantity at this point 
Solved Threads: 1
m3an m3an is offline Offline
Newbie Poster

Re: help with dynamic memory

 
0
  #5
May 13th, 2007
ya i tried that wrote before, it had some wierd looping problem...workin on it.

Easier for beginners too because you don't have to worry about managing the memory.
ya, i have to manage it, its a requirment.

Finally, new doesn't return a null pointer if it fails anymore, it throws an exception.
ya i knw, im using nothrow
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,846
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 753
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: help with dynamic memory

 
0
  #6
May 13th, 2007
>ya, i have to manage it, its a requirment.
I'm glad you mentioned that beforehand.

>ya i knw, im using nothrow
I'm glad you posted the actual code you're using.

On a side note, despite the fact that I can make extremely accurate guesses, I'm not psychic.
Last edited by Narue; May 13th, 2007 at 8:36 pm.
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 11
Reputation: m3an is an unknown quantity at this point 
Solved Threads: 1
m3an m3an is offline Offline
Newbie Poster

Re: help with dynamic memory

 
0
  #7
May 14th, 2007
I'm glad you mentioned that beforehand.
That wasnt my problem, read the post again smrt..

I'm glad you posted the actual code you're using.
and my codes is very long...and i added nothrow after i posted
AND THAT STILL WASNT MY QUESTION!

On a side note, despite the fact that I can make extremely accurate guesses, I'm not psychic.
think again! u hvnt answered any of my questions physc...

i asked for help not ur ''smart'' remarks
Last edited by m3an; May 14th, 2007 at 12:49 am.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 11
Reputation: m3an is an unknown quantity at this point 
Solved Threads: 1
m3an m3an is offline Offline
Newbie Poster

Re: help with dynamic memory

 
0
  #8
May 14th, 2007
anyone please help. how do i use dynamic memory and classes together in my code?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,846
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 753
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: help with dynamic memory

 
0
  #9
May 14th, 2007
>That wasnt my problem, read the post again smrt..
I guess I'm not smart enough. Please point out where you said a dynamic array was required. The closest you got was saying "I want", which clearly says that you have a choice. Therefore, I suggested a better choice.

>and my codes is very long
Then make it shorter for posting. There's a sticky at the top of this forum that tells you how to post like an intelligent human being.

>and i added nothrow after i posted
Then you should say "thanks" and leave it because my comment was accurate when you posted. As I said, I'm not psychic, so I don't know what changes you made after you posted.

>think again! u hvnt answered any of my questions physc...
BS. Maybe you should take some lessons in reading for comprehension, because the fourth post in this thread answers your question fully. If it doesn't, you asked the wrong question.

Now, on a side note, I'm going to ask you to use proper spelling and grammar in your posts and proofread them before hitting the "Submit Reply" button. Your silly abbreviations are perilously close to breaking our Keep-It-Clean rule.
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 21
Reputation: desijays is an unknown quantity at this point 
Solved Threads: 1
desijays desijays is offline Offline
Newbie Poster

Re: help with dynamic memory

 
0
  #10
May 14th, 2007
you are trying to access a private member with your object in the main function.

cin >> film.i;

is not supposed to work cos its film is trying to access a member( i ) that has been declared private in the class.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC