943,791 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 2805
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
May 13th, 2007
0

help with dynamic memory

Expand Post »
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


C++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
m3an is offline Offline
11 posts
since Nov 2006
May 13th, 2007
0

Re: help with dynamic memory

You're using strings, why not use vectors too? Much easier.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
May 13th, 2007
0

Re: help with dynamic memory

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

Quote ...
im having trouble with using dynamic memory and classes together
Quote ...
,
Last edited by m3an; May 13th, 2007 at 6:19 pm.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
m3an is offline Offline
11 posts
since Nov 2006
May 13th, 2007
0

Re: help with dynamic memory

>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:
C++ Syntax (Toggle Plain Text)
  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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
May 13th, 2007
0

Re: help with dynamic memory

ya i tried that wrote before, it had some wierd looping problem...workin on it.

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

Quote ...
Finally, new doesn't return a null pointer if it fails anymore, it throws an exception.
ya i knw, im using nothrow
Reputation Points: 10
Solved Threads: 1
Newbie Poster
m3an is offline Offline
11 posts
since Nov 2006
May 13th, 2007
0

Re: help with dynamic memory

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
May 14th, 2007
0

Re: help with dynamic memory

Quote ...
I'm glad you mentioned that beforehand.
That wasnt my problem, read the post again smrt..

Quote ...
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!

Quote ...
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.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
m3an is offline Offline
11 posts
since Nov 2006
May 14th, 2007
0

Re: help with dynamic memory

anyone please help. how do i use dynamic memory and classes together in my code?
Reputation Points: 10
Solved Threads: 1
Newbie Poster
m3an is offline Offline
11 posts
since Nov 2006
May 14th, 2007
0

Re: help with dynamic memory

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
May 14th, 2007
0

Re: help with dynamic memory

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.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
desijays is offline Offline
21 posts
since Mar 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: for use from file
Next Thread in C++ Forum Timeline: Is it a problem due to scope or compiler ?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC