Pointer Out Of Scope

Hello,

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.

Could someone explain this to me?

I've googled it and even have found an answer almost identical to mine:
http://stackoverflow.com/questions/3371262/resize-array-template

From within the main method:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int main(int argc, char* argv[]){
        int **keys, keyArrayCounter=1, keyArrayMaxLength=INITIALMAX;
	int keylength=0; //length of individual keys, 0 for first run
	string *websites;
        ifstream fileStream(inputfilename.c_str());
	istream cinStream(cin.rdbuf());

....

while(cinStream.good() && newchar != ' '){
	if(counter == tmp_keylength){
		resizeArray(keys[0], tmp_keylength);
	}
	char *newchar_ptr = &newchar;
	keys[0][counter] = atoi(newchar_ptr);
	counter++;
	newchar = cinStream.get();
} newchar = cinStream.get(); //escapes the sentinel character

...

}


How do I get keys to be modified permanently?
Last edited on
@pcannons

function body line 13: newkeys = keys; //it's wrong
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
template <class T>
void resizeArray(T*& keys, int &oldMaximum){
	int m_oldMaximum = oldMaximum;

	T* newkeys = new T[oldMaximum];

	for(int i = 0; i < oldMaximum; i++){
		newkeys[i] = keys[i];
	}

		newkeys = keys;
}
pcannons: Um... On line 13 did you mean to write keys = newkeys;? It's the only part that doesn't make sense.
Hello,

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
Last edited on
Ah, okay. Now I see what you mean.
However, when the function goes out of scope, the variable "keys" is unaffected and remains the same address in memory in the main function
Of course. You're passing keys[0] to the function, not keys.
Sorry for the lack of clarity,

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){

It works correctly in my pc.
It's not suitable. What does "*&" mean?
How do you know it's not suitable if you don't know what it means?
Hi Helios,

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.
But in your original code, the formal parameter keys was of type 'T *&', thus a change to it would reflect on the actual parameter.
Topic archived. No new replies allowed.