Have a C++ project due soon and am not sure if I'm using pointers right.

Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

Have a C++ project due soon and am not sure if I'm using pointers right.

I have a spot where I'm worried that, since a silly IDE glitch won't let it run properly, perhaps as I don't have a makefile or put it in wrong package or something (new IDE/compiler).

Out: 10th September 2011 Due: 19th September 2011 at 11:59pm
In this assignment you will write a class two versions of an adjusting array. The first version will work similar to the ArrayList class in Java (or vector class in C++), while the second one will make deletion more efficient.
Part 1: Adjusting Array
Write a class AdjustArray that represents an array of integers. Initially it has a size of 1. At any time, it can be partially full, so it keeps track of its current occupied size, as well as its true size. Add the following to this class:
1. A default constructor. It initializes the array to have all 0’s.
2. An overloaded [] operator as follows: int& operator [](unsigned int i). This returns a reference to the number at index ‘i’. Returning a reference is no different than returning the value at that position, C++ takes care of actually returning a reference to it. If ‘i’ is beyond the size of the array, so be it (the program will likely crash if this case is encountered at run-time).
3. A function “push_back(int m)” that will add ‘m’ to the end of the occupied part of the array. If the array is already full, it must first double the size of the array before adding this number. Double the array by creating a new array that is twice as big, copying all the elements of this array to the new one, and the making the new array as the “current” one. Be sure to free up any memory as necessary!
4. A function “erase(unsigned int i)”. This function deletes the number at position ‘i’ by shifting everything to its right by one position to the left. Note: the size of the occupied part of the array reduces by 1. If the array’s occupancy reduces to less than 50%, make the array half as big similar to how you expanded it.
Write a main function that tests this class in all possible ways (i.e. adding elements, deleting from the middle and either ends, allowing the array to expand and contract, etc.). The main function is worth points!
Part 2: Adjusting Array with Faster Deletion
The above array does not delete an element efficiently (because of all the shifting). Make it more efficient as follows (write a separate class, do not modify the above class).
1. Add an array “deletion” of booleans that maintains whether each position in the array of number is currently occupied, i.e. “deletion[i]= true” if number at index ‘i’ has been deleted, false otherwise.
2. Modify erase so that it simply changes the above array, but does not shift anything.
3. Add a variable that stores the smallest index that has been deleted.
4. Modify the [] operator above as follows:
a. If the index ‘i’ to be accessed is before the first deleted position, return reference directly.
b. Otherwise, remove all “empty” positions to the left of this index ‘i’ by shifting elements to the left. This will move the “true” number to this position and the smallest index that has been deleted will be greater than ‘i’. Now return the reference. (If the occupancy falls below 50%, contract as before)
Your main function does not change at all, and it should work as before.
Extra Credit (5 points): Do step 4(b) in Part 2 without using an extra array. Hint: During shifting, keep track of the position which must shift, and the position it must shift to.

When I was trying to double the size of the array, was I setting the pointer to a temporary variable that will be lost when the method or whatever ends, this making array point to nothing or am I worried about nothing?

Also, how could you change erase to modify the array without shifting?

Re: Have a C++ project due soon and am not sure if I'm using pointers right.

C++ does not allow for dynamically sized arrays on the stack, you must allocate the array on the heap (a.k.a. using "new"), then you will need to delete it when you're done. Even assuming that you could have dynamically sized arrays you would still need to allocate the array on the heap because as soon as push_back() returned the stack will be reclaimed, along with the array and the pointer in your class will point to an invalid memory location.

Re: Have a C++ project due soon and am not sure if I'm using pointers right.

Yes, in push_back:

int newArray[getSize()*2];

If this was legal (it isn't because C++ doesn't allow dynamic sized arrays on the stack), the actual array would go out of scope and be reclaimed after push_back returned.

You're constructor has the correct syntax for using the new operator.

One thing is that you will need to manually delete the old referenced array in push_back, as well as in the destructor when the whole object is being reclaimed. Note: you only need to delete items allocated on the heap. Items allocated on the stack are automatically reclaimed when they go out of scope.

Re: Have a C++ project due soon and am not sure if I'm using pointers right.

I've almost got it now, but I can't figure out some segmentation error that I'm thinking is being created, likely by an indexing error I can't see for some reason, inside the overloaded [] operator method, though there's a very small chance the error could be elsewhere.