C++ allocating arrays dynamically.

  • Comp Sci
  • Thread starter Student100
  • Start date
  • Tags
    Arrays C++
In summary: Thanks again.In summary, the programmer is trying to solve a problem where they need to dynamically allocate an array of objects. They are not able to do this using the standard C++ library functions, so they create their own. They use a Triva class to hold the user's questions and answers, and allocate space for the Triva class and the user's questions and answers using a dynamic method. The Triva class is then copied and the original Triva class is deleted. The programmer then increments the size of the Triva class and the user's questions and answers are copied into the new Triva class.
  • #1
Student100
Education Advisor
Gold Member
1,653
417

Homework Statement



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.

Homework Equations


None

The Attempt at a Solution



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;

	}

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.
 
Last edited:
Physics news on Phys.org
  • #2
It looks OK ...the delete[] deallocates the old copy, while the new one is dynamically allocated/deallocated inside the loop.
 
  • Like
Likes 1 person
  • #3
UltrafastPED said:
It looks OK ...the delete[] deallocates the old copy, while the new one is dynamically allocated/deallocated inside the loop.

Thanks I appreciate the response, that's what I had thought was happening, but I still felt unsure about it.
 

1. What is dynamic allocation in C++?

Dynamic allocation in C++ refers to the process of allocating memory for variables or data structures at runtime, rather than at compile time. This allows for more flexibility in managing memory and can help to avoid memory wastage.

2. How is an array dynamically allocated in C++?

In order to dynamically allocate an array in C++, the new keyword is used. This allocates a block of memory on the heap and returns a pointer to the first element of the array. The size of the array must be specified in square brackets after the type declaration.

3. How is memory deallocated for a dynamically allocated array in C++?

In order to deallocate memory for a dynamically allocated array in C++, the delete keyword is used. This frees up the memory allocated on the heap and prevents memory leaks. It is important to remember to delete dynamically allocated memory once it is no longer needed.

4. Can a dynamically allocated array be resized in C++?

Yes, a dynamically allocated array can be resized in C++ by using the realloc function. This allows for the array to be expanded or shrunk, depending on the new size specified. However, this function can be inefficient and it is generally recommended to use std::vector for resizable arrays in C++.

5. What happens if memory cannot be allocated for a dynamically allocated array in C++?

If memory cannot be allocated for a dynamically allocated array in C++, the new operator will throw an exception. It is important to handle such exceptions properly in order to prevent unexpected program termination. Additionally, it is good practice to check for nullptr before attempting to use a dynamically allocated array in case memory allocation was unsuccessful.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
949
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Programming and Computer Science
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
17
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Programming and Computer Science
4
Replies
118
Views
6K
Back
Top