Insights Blog
-- Browse All Articles --
Physics Articles
Physics Tutorials
Physics Guides
Physics FAQ
Math Articles
Math Tutorials
Math Guides
Math FAQ
Education Articles
Education Guides
Bio/Chem Articles
Technology Guides
Computer Science Tutorials
Forums
Intro Physics Homework Help
Advanced Physics Homework Help
Precalculus Homework Help
Calculus Homework Help
Bio/Chem Homework Help
Engineering Homework Help
Trending
Featured Threads
Log in
Register
What's new
Search
Search
Search titles only
By:
Intro Physics Homework Help
Advanced Physics Homework Help
Precalculus Homework Help
Calculus Homework Help
Bio/Chem Homework Help
Engineering Homework Help
Menu
Log in
Register
Navigation
More options
Contact us
Close Menu
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Forums
Homework Help
Engineering and Comp Sci Homework Help
C++ allocating arrays dynamically.
Reply to thread
Message
[QUOTE="Student100, post: 4538520, member: 466030"] [h2]Homework Statement [/h2] The problem is to dynamically allocate (closer to relocating) a dynamic class array in C++ based on the number of user inputs. We can't use vectors, malloc, realloc, or any such functions beyond new or anything simple like asking the user for how many questions they want to input. [h2]Homework Equations[/h2] None[h2]The Attempt at a Solution[/h2] I've got it to where it will print out what I want to see, I'm not losing any information when I print out the array elements. My question is mostly to do with some confusion I have about what’s going on with the pointers during the relocating and repointing ... [CODE] while (addQuestion == 1) { cout << "Please enter the question you would like to ask" << endl; cin >> userNewQuestion; cout << endl; cout << "please enter the anwser to the question you'd like to ask" << endl; cin >> userNewAnwser; cout << "Please enter the dollar amount you'd like the question to be worth" << endl; cin >> addDollarAmount; cout << "Enter another question? 1 for yes, 2 for no: "; cin >> addQuestion; cout << endl; Triva userNewTrivaQuestion(userNewQuestion, userNewAnwser, addDollarAmount); pntTriva myNewDynamicArray; myNewDynamicArray = new Triva[size+1]; myNewDynamicArray[size] = userNewTrivaQuestion; copy(myDynamicTriva, myDynamicTriva+size, myNewDynamicArray); delete [] myDynamicTriva; size++; myDynamicTriva = myNewDynamicArray; }[/CODE] I think it's behaving and starting with the original array, copying it into the temp MyNewDynamicArray that stores the new class member into the extra element, deletes the original dynamic allocation and points the original to the temp. However, I'm confused if when it runs a second time and goes through all the motions again if I'm leaving some dynamically allocated memory floating around somewhere that I shouldn't be? Is this the case, or does the delete make sure to take care of each old dynamical array like I think it should? I'm using VS2010 ultimate, I'm not great with the debugger yet, the extent of my skill with it is to step through/in and add a watch. Beyond that I'm not exactly sure how to check dynamic allocations. [/QUOTE]
Insert quotes…
Post reply
Forums
Homework Help
Engineering and Comp Sci Homework Help
C++ allocating arrays dynamically.
Back
Top