First of all I want to thank you for taking your time to help me out.
I am writing a program for class in which I am required to implement my own dynamic arrays, and am not allowed to use STL. (vectors, etc.)
I have this function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
//resizes array once it hits its maximum
template <class T>
void resizeArray(T*& keys, int &oldMaximum){
oldMaximum*=2;
T* newkeys = new T[oldMaximum];
for(int i = 0; i < oldMaximum/2; i++){
newkeys[i] = keys[i];
}
delete []keys;
keys = newkeys;
}
However, when the function goes out of scope, the variable "keys" is unaffected and remains the same address in memory in the main function (snippet below) from which I call this function. (I've observed it very carefully through gdb in Eclipse)
The only way I can fix this is to return the "newkeys" variable and assign it to the pointer in the main method, but that means if I have two functions within one another I cannot use resizeArray.
Sorry for the mistake, I did mean to write keys = newkeys; however that is not the problem. With that changed the same problem still occurs.
bluecoder, I'm not really sure what you are saying, you simply changed the implementation of my function, so now it is wrong and the pointer problem persists
keys is an array of pointers, and those pointers are arrays. So I am trying to resize an array at the location keys[0].
Later I use
1 2 3 4 5 6 7
//check to see if the arrays for key/name have grown too large
//resize if they have
if(keysArrayCounter==keysArrayMaxLength){
int max = keysArrayMaxLength;
resizeArray(keys, keysArrayMaxLength);
resizeArray(websites, max);
}
to resize the array of arrays also.
Thanks for helping me tackle this, I am about to have office hours in 40 minutes where I will ask about this. I'll post back once I figure it out.
And you're saying keys points to the same place after the resizeArray() call on line 5 of your last snippet? I don't see any reason why that would be the case. resizeArray() is correct and it does indeed modify both its parameters. The only possible explanation I could think of is that the program contains more than one resizeArray() and it's calling the wrong one.
By the way, is there some specific reason why you're not using std::vector?
@pcannons void resizeArray(T*& keys, int &oldMaximum){
Aru you sure that you write "T*& keys"? It's not suitable. What does "*&" mean?
Why don't try use
1 2
template <class T>
void resizeArray(T* keys, int &oldMaximum){
I am using dynamic arrays because this is for a school engineering projects and we are not allowed to use any libraries.
I finally found the error, unfortunately non of the teaching assistants could help.
If you want the array passed properly, every function down to the method call has to be a * & or ** & pointer address. While we know that arrays can only be passed by reference, their pointers are still passed by value. This is why when the function would go out of scope it would simply delete those pointer values while leaving the originals unmodified.