0

Dynamic memory allocation?

Hello, My program basically reads a binary file and stores it in an array. The program work's while the size of my array is small. But the file I am working is very large and hence the compiler just stops working due to the runtime issue. My lead to this problem is to allocate dynamic memory to my array. But I have only seen dynamic memory being allocated to local variables. Is there a method I could apply dynamic memory to my numbers[] array itself? This could fix my problem. Any thoughts? #include <iostream> #include <fstream> using namespace std; int main() { int size; fstream file; file.open("this.bin", ios::binary | ios::in |ios::ate); size = file.tellg(); size = size/4; int numbers[size]; file.seekg(0, ios::beg); file.read(reinterpret_cast<char *>(numbers), sizeof(numbers)); for(int i=0; i<size; i++){ cout << numbers[i]; } file.close(); file.close(); return 0; }

18th Mar 2017, 7:13 PM
Safal Basnet
Safal Basnet - avatar
1 ответ
0
You can use std::vector<int> as a dynamic array. You can resize it as you want.
18th Mar 2017, 10:31 PM
hdo
hdo - avatar