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?
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.
//Program that would dynamically allocate memmory
#include <iostream>
usingnamespace 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 = newfloat[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.
//Program that would dynamically allocate memmory
#include <iostream>
usingnamespace 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 = newfloat[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;
}