Dynamically allocating memory inside class

I am having a problem with dynamically allocating memory using a class. When it compiles and gets to the console, I get a Debug Error. IT says Invalid allocation size 4294967295 bytes.

Another problem i was having is using the For loop. I am using visual studios and it is telling me i need to make int i a pointer. Why is that? Why can't i just declare it as a normal variable inside the function?

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
//Program that would dynamically allocate memory
#include <iostream>
using namespace std;
class dynamic{
public:
	float *arry;
	int value;
	//int getvalue() const {return value;}
	void getArray();
};
int main()
{
	
	dynamic box;
	box.getArray();

	return 0;
}

void dynamic :: getArray()
{
	cout << "What is the size you want the array to be? " << endl;
	arry = new float[value];
	//Get values Enetered
	for (int *i = 0; *i <= value; i++)
	{
		cin >>value[i];

	}
	//Loop that shows values entered
	cout << " The values entered are: " << endl;
	for(int *i = 0; *i <=value; i++)
	{
		cout << value[i];
	}

}


Your class doesn't initialise its members, so this:
 
arry = new float[value];

is a problem because value is an unspecified number.
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
//Program that would dynamically allocate memmory
#include <iostream>
using namespace std;
class dynamic{
public:
	float *arry;
	int value;
	//int getvalue() const {return value;}
	void getArray();
};
int main()
{
	
	dynamic box;
	box.getArray();

	return 0;
}

void dynamic :: getArray()
{
	
	cout << "What is the size you want the array to be? " << endl;
	cin >> value;
	arry = new float[value];
	value = value-1;

	cout <<"What are the values " << endl;
	//Get values Enetered
	for (int *i = 0; *i <= value; i++)
	{
		cin >>value[i];

	}
	//Loop that shows values entered
	cout << " The values entered are: " << endl;
	for(int *i = 0; *i <=value; i++)
	{
		cout << value[i];
	}

}


Thank you. It crashes now without a debug error after it gets to the for loop.
You're accessing out of bnounds. This:
 
for (int *i = 0; *i <= value; i++)

should be:
 
for (int i = 0; i < value; ++i)
Last edited on
It gives me an error if i remove the pointer on i. My compiler says " error C2109: subscript requires or pointer type." I don't know why i have to make it a pointer. It was one of my questions.

Also i see why i have to remove the = sign. It is because i am storing a value that is bigger than the array i am creating. So here is my program now. I took out value = value-1; because it wasn't needed.

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
//Program that would dynamically allocate memmory
#include <iostream>
using namespace std;
class dynamic{
public:
	float *arry;
	int value;
	//int getvalue() const {return value;}
	void getArray();
};
int main()
{
	
	dynamic box;
	box.getArray();

	return 0;
}

void dynamic :: getArray()
{
	
	cout << "What is the size you want the array to be? " << endl;
	cin >> value;
	arry = new float[value];

	cout <<"What are the values " << endl;
	//Get values Enetered
	for (int i = 0; i < value; i++)
	{
		cin >>value[i];

	}
	//Loop that shows values entered
	cout << " The values entered are: " << endl;
	for(int *i = 0; *i < value; i++)
	{
		cout << value[i];
	}

}


It still crashes when i get to the for loop without any debug error of what happened. My visual studios says it is searching for the problem...
Thanks again
It gives me an error if i remove the pointer on i. My compiler says " error C2109: subscript requires or pointer type." I don't know why i have to make it a pointer. It was one of my questions.


A subscript does require an array or pointer type, but i is your index into an array. You're trying to index value which is neither a pointer or an array type. value should not be a member of the class. It should be a local variable in getArray and you should be indexing arry in the same function.

value[i] and i[value] are both equivalent to *(i+value), and since i is a null pointer, both of those dereference a null pointer.
Thank you so much. I understood what i did wrong when you said value which is neither a pointer or an array type.
Here is my code to reflect what i learned.
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
//Program that would dynamically allocate memmory
#include <iostream>
using namespace std;
class dynamic{
public:
	float *arry;
	int value;
	//int getvalue() const {return value;}
	void getArray();
};
int main()
{
	
	dynamic box;
	box.getArray();

	return 0;
}

void dynamic :: getArray()
{
	
	cout << "What is the size you want the array to be? " << endl;
	cin >> value;
	arry = new float[value];

	cout <<"What are the values " << endl;
	//Get values Enetered
	for (int i = 0; i < value; i++)
	{
		cin >>arry[i];

	}
	//Loop that shows values entered
	cout << " The values entered are: " << endl;
	for(int i = 0; i < value; i++)
	{
		cout << arry[i] << endl;
	}

}


Everything is working fine now
Thanks again.
Dont forget to free up your memory. possibly in your class destructor?
Thank you. I put a destructor in my code. Here it is.

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
//Program that would dynamically allocate memmory
#include <iostream>
using namespace std;
class Dynamic{
public:
	float n;
	float *arry;
	int value;
	int place;
	void getArray();
	void anyArray();
	void retrieveNum();
	void highestVal();
	void lowestVal();
	void avgArry();
	~Dynamic();
};
int main()
{
	
	Dynamic box;
	//accept input and dynamically allocate to hold that many number
	box.getArray();
	//Store a number in any element of array
	box.anyArray();
	//retrieve a number from any element of the array
	box.retrieveNum();
	//Return highest value stored in arrays
	box.highestVal();
	//return the lowest value stored in arry
	box.lowestVal();
	//Return the average of the numbers stored in the array
	box.avgArry();

	return 0;
} 
void Dynamic:: avgArry()
{
	n = 0;
	for(int i =0; i < value; i++){
		n +=arry[i];}
	cout << " The sum is " << n << endl;
	cout << "Divided by " << value << endl;
	n /=value;
	cout << "The average of the numbers in the array is: " << n << endl;


}
Dynamic::~Dynamic()
{
	delete [] arry;
	cout << "Destructor activated!! " << endl;
}
void Dynamic:: lowestVal()
{
	n = arry[0];
	for(int i=0; i < value; i++)
	{
		if(n > arry[i])
			n = arry[i];

	}
	cout << "The lowest value in the array is: " << n << endl;
}
void Dynamic:: highestVal()
{
	n = arry[0];
	for(int i=0; i < value; i++)
	{
		if(n < arry[i])
			n = arry[i];

	}
	
	cout << "The highest value in the array is: " << n << endl;
}
void Dynamic:: retrieveNum()
{
	cout << "What place in the array do you want view?" << endl;
	cin >> place;
	cout << "The value stored in place " << place << " is" << endl;
	cout << arry[place-1] << endl;
		
}
void Dynamic:: anyArray()
{
	
	cout << "What number do you want to store in the array " << endl;
	cin >> n;
	cout <<"What place do you want to store the value in the array" << endl;
	cin >> place;
	arry[place-1] =n;

	//Loop that shows values entered
	cout << " The values stored in the are: " << endl;
	for(int i = 0; i < value; i++)
	{
		cout << arry[i] << " ";
	}
	cout << endl;
}
void Dynamic :: getArray()
{
	
	cout << "What is the size you want the array to be? " << endl;
	cin >> value;
	arry = new float[value];

	cout <<"What are the values " << endl;
	//Get values Enetered
	for (int i = 0; i < value; i++)
	{
		cin >>arry[i];

	}
	//Loop that shows values entered
	cout << " The values entered are: " << endl;
	for(int i = 0; i < value; i++)
	{
		cout << arry[i] << " ";
	}
	cout << endl;
}

Last edited on
Topic archived. No new replies allowed.