This code works so far.. but when I increase "i" above 30 it crashes.
Two diffrent error occur:
- Heap damaged
- acces violation
What this code should do:
In the end I want to be able to increase the size of the array by 1 everytime the user "cin >> yes".. and that should be a loop that can go on for ever...
I think the fact that it works uo to 30 is only because Im lucky to have free space there..
Hope you guys can help me! :)
#include <iostream>
usingnamespace std;
int main()
{
int size = 1;
double* temperatur = newdouble[size];
double* temperatur_save = newdouble[size];
for (int i = 0; i < 30; i++)
{
temperatur[i] = i;
//save temperatur array in temperatur_save
temperatur_save = temperatur;
//Create a new array with size depending on "size"
temperatur = newdouble[size];
//store the values from temperatur_save in the new (bigger) temperatur array
temperatur = temperatur_save;
//Craete a new array with suze depending on "size"
temperatur_save = newdouble[size];
//Display values
cout << temperatur[i];
//Increase "size" by one
size++;
}
system("pause");
}
If you want to increase the size of the array you will have to create a new array and copy the contents in via a loop. Alternatively you can use the vector type and just use the vector member vectorvariable.pushback()
What this code should do:
In the end I want to be able to increase the size of the array by 1 everytime the user "cin >> yes".. and that should be a loop that can go on for ever...
The .pushback member function will do this but behind the scenes it really allocates a more efficient amount of space.
If constantly allocating memory and copying arrays it becomes inefficient. So programs would possibly increase the size of the array to twice its current size.
This code works so far.. but when I increase "i" above 30 it crashes.
Two diffrent error occur:
- Heap damaged
- acces violation
You need to make sure you do not write off the end of an array, it can be potentially dangerous to your system.
in your case you would create an array of initial size 30 than create a new array when you reach your arrays max size, then copy the contents then delete[] the old array as you must return the memory once you are finished with it.
I've come accros vectors while trying to google a solution for my problem..
I do not really understand how to use them yet..
also this is a homework for school.. we were introduced to dynamic arrays last lesson.. so I think it should be possible using those.. :/
I will try to do it with vectors, if there is no way to do it with dynamic arrays but I dont thiink that this is what im supposed to do..
First call of line 19 makes the pointer 'temperatur_save' to point to memory that was allocated on line 9 and you no longer have any pointer to refer the memory allocated on line 10.
Line 22 calls newdouble[1];. That would be insufficient on the next iteration, but line 28 discards the memory (without proper deallocation).
I've come accros vectors while trying to google a solution for my problem..
I do not really understand how to use them yet..
also this is a homework for school.. we were introduced to dynamic arrays last lesson.. so I think it should be possible using those.. :/
If this is the case using vectors would probably be considered cheating. But in saying that I would suggest you research arrays and dynamic memory allocation first if you value your computer.
#include <iostream>
usingnamespace std;
int main()
{
int size = 1;
double* temperatur = newdouble[size];
double* temperatur_save; // don't allocate memory for this pointer.
for (int i = 0; i < 40; i++)
{
temperatur[i] = i;
//save temperatur array in temperatur_save
temperatur_save = temperatur;
//Create a new array with larger size
temperatur = newdouble[size + 1];
//Copy the saved values to this array
for ( int j = 0; j < size; j++ )
temperatur[j] = temperatur_save[j];
//Free unused memory
delete [] temperatur_save;
//Display values
cout << temperatur[i];
//Increase "size" by one
size++;
}
// don't use the risky, platform dependent system commands.
cin.get();
}