| | |
help with dynamic memory
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Nov 2006
Posts: 11
Reputation:
Solved Threads: 1
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) -
- class Names
- {
- private:
- string title;
- int i;
- int * p;
-
- public:
- void enter_new(); // sets names of actors to the DVD
- }film;
-
-
- void Names::enter_new()
- {char ans;
- cout << "How many movies would you like to type? ";
- cin >> film.i;
- int ii= film.i;
- (film.p)= new int[ii];
-
- if ((film.p) == 0)
- cout << "Error: memory could not be allocated";
- else
- {
- for (int a=1; a<(ii); a++)
- {cout<<"\n Please enter the name: "<<endl;
- cin >> (film.p)[a];
- }
- delete[] p;
- }
>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:
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.
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)
void Names::enter_new() { cout<<"How many movies would you like to type? "; cin>> i; p = new int[i]; for ( int a = 0; a < i; a++ ) { cout<<"\n Please enter the name: "; cin>> p[a]; } }
New members chased away this month: 4
•
•
Join Date: Nov 2006
Posts: 11
Reputation:
Solved Threads: 1
ya i tried that wrote before, it had some wierd looping problem...workin on it.
ya, i have to manage it, its a requirment.
ya i knw, im using nothrow
•
•
•
•
Easier for beginners too because you don't have to worry about managing the memory.
•
•
•
•
Finally, new doesn't return a null pointer if it fails anymore, it throws an exception.
>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.
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
•
•
Join Date: Nov 2006
Posts: 11
Reputation:
Solved Threads: 1
•
•
•
•
I'm glad you mentioned that beforehand.
•
•
•
•
I'm glad you posted the actual code you're using.
AND THAT STILL WASNT MY QUESTION!
•
•
•
•
On a side note, despite the fact that I can make extremely accurate guesses, I'm not psychic.
i asked for help not ur ''smart'' remarks
Last edited by m3an; May 14th, 2007 at 12:49 am.
>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.
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
![]() |
Similar Threads
- Dynamic memory for an array of objects (C++)
- Static and Dynamic Memory Allocations...Advan's & Disadvan's??? (Computer Science)
- regarding dynamic memory allocation (C)
- Dynamic memory allocation homework (C++)
Other Threads in the C++ Forum
- Previous Thread: for use from file
- Next Thread: Is it a problem due to scope or compiler ?
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamiccharacterarray email encryption error file format forms fstream function functions game givemetehcodez graph homeworkhelp iamthwee ifstream input int java lib library lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sorting spoonfeeding string strings struct temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






