Help with dynamic array expanding via function!

Help! I'm trying to expand an array via pointers, but my program crashes when i try to run this. It tells me that it's unable to read the "name" variable of the class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//CLASS
#ifndef TEST_H
#define TEST_H
#include<iostream>
#include<string>

using namespace std;

class Test
{
	private:
		string name;
		int x;
		int y;
	public:
		Test();
		Test(string name, int x, int y);
		~Test();
		void setName(string name);
		void setX(int x);
		void setY(int y);
		string toString()const;
		string getName()const;
		int getX()const;
		int getY()const;
};
#endif;



//CPP FOR CLASS

#include"Test.h"
#include<sstream>

Test::Test()
{
	name = "Default";
	x = 1;
	y = 1;
}


Test::Test(string name, int x, int y)
{
	name = this->name;
	x = this->x;
	y = this->y;
}

Test::~Test()
{

}


void Test::setName(string name)
{
	name = this->name;
}

void Test::setX(int x)
{
	x = this->x;
}

void Test::setY(int y)
{
	y = this->y;
}


string Test::getName()const
{
	return name;
}

int Test::getX()const
{
	return x;
}

int Test::getY()const
{
	return y;
}

string Test::toString()const
{
	stringstream ss;


	ss <<"Name: "<<name <<"\nX: " << x <<"\nY: " <<y << endl;
	return ss.str();
}




//CPP WITH MAINFUNCTION

#include"Test.h"

void expandArray(Test test[],int &capacity)
{
	Test* newTempArr = new Test[capacity * 2];
	for (int i = 0; i < capacity; i++)
	{
		newTempArr[i] = test[i];
		cout << newTempArr[i].toString();
	}
	delete[] test;
	test = newTempArr;
	capacity *= 2;
}

void writeOutArray(Test test[], int capacity)
{

	for (int i = 0; i < capacity; i++)
	{
		cout << test[i].toString();
	}
}


int main()
{
	int capacity = 10;
	Test* testArray = new Test[capacity];
	expandArray(testArray, capacity);
	writeOutArray(testArray, capacity);
	return 0;
}



Please help! I'm stuck!
 
Your setters seem to be backwards:

1
2
3
4
void Test::setName(string name)
{
	name = this->name;
}


Here you assign the value of the member string name to the argument string name (you want to do it the other way around). So it should be:

1
2
3
4
void Test::setName(string name)
{
	this->name = name;
}


You do this in all of your setters and the overloaded constructor.
Oh i've been thinking in the other direction. Thanks for fixing that mistake! But it doesn't solve my problem however.
"solved" it by putting the expanding in the main-function instead, atleast that works for now.
I think I have it figured out:

1
2
3
4
5
6
7
8
9
10
11
12
void expandArray(Test test[],int &capacity)
{
	Test* newTempArr = new Test[capacity * 2];
	for (int i = 0; i < capacity; i++)
	{
		newTempArr[i] = test[i];
		cout << newTempArr[i].toString();
	}
	delete[] test;
	test = newTempArr;
	capacity *= 2;
}


Think about what test is in the above function. test is just a pointer, right? When you pass testArray to it (function expandArray), you are creating another pointer called test which is a copy of testArray. This means that you copy the address stored in testArray to the pointer test. Since test is merely a copy, changing what test points to doesn't change what testArray points to.

1
2
3
4
5
6
7
8
9
10
11
12
13
void foo(int array[])
{
    array = 0; // only this copy will be effected
}

int main()
{
    // assume a is dynamically allocated
    foo(a); // at this point, the pointer named array in foo and a both point
               // to the same location in memory, but &a != &array, so any changes
               // to the value of array itself (that is, the address that is stored in it)
               // will not effect a.
}


You need to have expandArray take a pointer to a pointer, then pass the address of testArray from main:

1
2
3
4
5
6
7
8
9
10
11
void foo(int * array[])
{
    *array = 0; // array will still point to a, but a will now point to nothing
}

int main()
{
    // assume a is dynamically allocated
    foo(&a); // now array in foo points to a which points to an array in memory.
                 // changes to *array will change the value of a itself
}


Hope that clears it up a little bit (I'm in a bit of a rush, so I'll come back later and offer more detail if need be)
Last edited on
Topic archived. No new replies allowed.