Array with variable dimension in class or structure

Amateur question:

I have a structure:

struct ActivePoint {
int totq;
int valc[20];
int level;
double objval;
bool integer;
float allocate;
};

with an array: valc. This array has dimension 20, however it should have a dimension that is set at the time of construction (so the dimension should not be 20, but should be "d".

I tried to do so by using a class:

class ActivePoint {
public:
ActivePoint(int ic);
int totq;
int* valc;
int level;
double objval;
bool integer;
float allocate;
};

ActivePoint::ActivePoint(int ic) {
totq = level = 0;
objval = 0;
valc = new int[ic];
integer = false;
allocate = 0;
}

But for some reason, when using multiple ActivePoints (not in an array!), the valc turns out to be a complete mess.

Any idea what I can do about this?

If in the class, I change int* valc to int valc[20], everything does work (but again I cannot vary the array dimension at the start)

Thanks!


Timo
Last edited on
These are the same:
1
2
int* valc = new int[ic];
int valc[20];


In both cases valc is the address of element 0, which is an int making valc an int*.

The correct approach is to allocate the array in the constructor using the new operator.

If you do this, then ic is your array dimension and this can be different for every ActivePoints object. Now if you have some hard-coded 20's floating around, then you will have problems.

You should be able to change the size of the array bu writing a method that allocates a new array of the new length, copies the current valc array into the new array, deletes valc, and then assigns the address of the new array to valc.

In short, any problems you are having are not in the code sample you posted.
Thanks for the response. I did not manage to solve it yet...

I added:
void activepoint::setdim(int ic){
valc = NULL;
valc = new int[ic];
}

and in the destructor added valc = NULL as well.

Still doesn't work...

I do the following (simplified):

subproblem.valc[1] = 10;
test1 = subproblem.valc[1];

subproblem.valc[1] = 20;
test2 = subproblem.valc[1];

Does not give me the correct results.

What do I need to change in my class to be sure that everything should work fine?

Thanks!
I guess I faile to see the problem. I took your code and did this:
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
class ActivePoint {
public:
ActivePoint(int ic); 
~ActivePoint();
int totq;
int* valc;
int level;
double objval;
bool integer;
float allocate;
};

ActivePoint::ActivePoint(int ic) { 
totq = level = 0; 
objval = 0;
valc = new int[ic];
integer = false;
allocate = 0;
}
ActivePoint::~ActivePoint()
{
	delete [] valc;
	valc = 0;
}
int main()
{
	ActivePoint A(10);
	ActivePoint B(20);

	A.valc[1] = 50;
	B.valc[1] = 100;

	cout << A.valc[1] << " " << B.valc[1] << endl;

}


and got the correct displays.

However, all of your class data members are public. They need to be private.

You see, that valc member needs to be managed by ActivePoints member functions only. If it's not then anywhere in the program someone could alter valc incorrectly and crash the program.

Next, your activepoint::setdim needs to delete the current valc before placing a new address inside it. Otherwise, you can't delete the previous allocation. This is a memory leak.

Next, your ActivePoints constructor need to validate that the ic used as an argument is not zero or some negative number. I wouild llet this go until later and just be sure to use valid ic arguments.
Thanks very much!

I protected everything now, apparently there was a part of the problem that screwed up the activepoints.

Thanks again!


Timo
Topic archived. No new replies allowed.