Unhandled exception

hello..im new in c++ so please help me.. i have this error with my coding "Unhandled exception at 0x00f54593 in task 1.exe: 0xC0000005: Access violation writing location 0x00b65d58." me and my lecturer found that this error came from vector coding, this is the code

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <vector>
#include <string>
#include <malloc.h>
using namespace std;

const int m = 57000;
const int n = 2;
int NumExam, NumSlot;

vector< vector<long double> > exam (m, vector<long double>(m,n));
vector< long double> slot (n-1);
vector< vector<long double> > student (m, vector<long double>(m,n));
//vector< vector<string> > student (m, vector<string>(m,n));

//files

ifstream inpStud("Carleton91.stu");
ofstream outData ("output.txt"); //write result

void readstudent(){

long double k, numline;
//assign all data to an array
for (int i= 0; i<m; i++)
{
for (int j= 0; j<n; j++)
{
inpStud >> student[i][j];
k=student[i][j];
}

//count number of exam
if ((k==2) && (i>100)){
numline = i;
break;

}
}

//print data
for (int i= 0; i<numline; i++)
{
for (int j= 0; j<n; j++)
{
outData << student[i][j] << " ";
}
outData << endl;
}
}
int main()
{
readexam();
readslot();
readstudent();
//recordClashes();

inpExam.close();
inpSlot.close();
inpStud.close();
outData.close();
return 0;
}



the thing is i only want it to read my file which have 57000 line but the programming can only read until 16000. if i set it more than 16000 the error will come out..help me please..should i change from using vector to something else?
I'm guessing the problem is because you don't have 24 gigabytes of ram. You are trying to initialize 57000 vectors of 57000 doubles. That's 3,249,000,000 doubles. With each double being 8 bytes, it comes to 25,992,000,000 bytes of data.

16000*16000 doubles 'only' takes 2 gigabytes, which is why you don't have any problems.
Last edited on
hello @yay295..thanks for the reply..maybe your right regarding my laptop its because i only use 1.5 gb ram..

the problem not just on my laptop but also on my supervisor laptop...before this she use her laptop to code her phd thesis just fine...

so i need to try on another computer to see whether its the laptop problem or coding problem... thanks you
You are allocating WAY too much memory, that's just over 48 GBs of RAM you need in order to support two vectors of 57000 vectors of 57000 doubles. How did your supervisor not see this?

I don't understand why you are using doubles, are you trying to index students and exams, or are you trying to get some sort of grade?

In the case of indexing students and exams, switch to int and cut your memory use in half.

It also seems that you have near 4 GBs of RAM on your laptop, because that is how much memory you need for 16000 lines in your case.

Here's the spike in RAM I got with m = 10000:
http://prntscr.com/3zmcit
Last edited on
hello @gaius.. that was part of my coding..this coding is just for read file...im doing the coding for creating exam timetable...

im just start the coding with the read file code.. i try to read exam, slot and student dataset file..for exam and slot its ok but for student its began to error..

its also error with my supervisor laptop..before this she also using the same data and its ok..
this is all my coding for now...

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <vector>
#include <string>
#include <malloc.h>
using namespace std;

const int m = 57000;
const int n = 2;
int NumExam, NumSlot;

vector< vector<long double> > exam (m, vector<long double>(m,n));
vector< long double> slot (n-1);
vector< vector<long double> > student (m, vector<long double>(m,n));
//vector< vector<string> > student (m, vector<string>(m,n));

//files
ifstream inpExam("Carleton91.exm"); //read all data
ifstream inpSlot("Carleton91.slt");
ifstream inpStud("Carleton91.stu");
ofstream outData ("output.txt"); //write result

void readexam(){

long double k=0;
//assign all data to an array
for (int i= 0; i<m; i++)
{
for (int j= 0; j<n; j++)
{
inpExam >> exam[i][j];
k=exam[i][j];
}

//count number of exam
if ((k==2) && (i>100)){
NumExam = i;
break;
}
}

//print data
/*for (int i= 0; i<NumExam; i++)
{
for (int j= 0; j<n; j++)
{
outData << exam[i][j] << " ";
}
outData << endl;
}*/
}


void readslot(){

//assign all data to an array

for (int j= 0; j<n-1; j++)
{
inpSlot >> slot[j];
//NumSlot = slot[j];
}



/*for (int j= 0; j<n-1; j++)
{
outData << slot[j] << " " << NumSlot << " " << NumExam;
}
outData << endl;*/

}


void readstudent(){

long double k, numline;
//assign all data to an array
for (int i= 0; i<m; i++)
{
for (int j= 0; j<n; j++)
{
inpStud >> student[i][j];
k=student[i][j];
}

//count number of exam
if ((k==2) && (i>100)){
numline = i;
break;

}
}

//print data
for (int i= 0; i<numline; i++)
{
for (int j= 0; j<n; j++)
{
outData << student[i][j] << " ";
}
outData << endl;
}
}



int main()
{
readexam();
readslot();
readstudent();


inpExam.close();
inpSlot.close();
inpStud.close();
outData.close();
return 0;
}
What is your program intended to do? It is not obvious from the code, and it is doubtful you need to have all of the data in memory at the same time.

Stop thinking "I need to fix my code" and start thinking "I need to change my algorithm."
@cire Exactly.

You need to rethink this task. Also, in which line of your program do you get the error if it doesn't have to do with the size of your 2d vectors?
this code is just the begining.. i just want the program to read the dataset file that i have....

because of this i cannot proceed with the main problem..

i really need it to read the file....

my supervisor have actually done and solve a same problem but with difference method using c++ and she already have the code but because of something the program have some error..because i dont have any basic using c++ and it hard to fix other person code so she ask me to do from the beginning...

so i need to start a new one from the beginning and she ask me to start with read file..
@gaius.. me and my superviser try using breakpoint and she said that the problem from vector..

if i change m= 16000 and above its run perfectly..but the line of the data have until 57000 line

then this will come out
"Unhandled exception at 0x7748b727 in task 1.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x0041f0d8.

if i click break new tab with the mlock.c will appear and show this

void __cdecl _unlock (
int locknum
)
{
/*
* leave the critical section.
*/
LeaveCriticalSection( _locktable[locknum].lock );
}


if i click continue than another new tab name vector will appear and this will come out and there are yellow arrow at "_Myfirst = this->_Alval.allocate(_Capacit[/b]y);[/b][/u]"

protected:
void _Assign_n(size_type _Count, const _Ty& _Val)
{ // assign _Count * _Val
_Ty _Tmp = _Val; // in case _Val is in sequence
erase(begin(), end());
insert(begin(), _Count, _Tmp);
}

bool _Buy(size_type _Capacity)
{ // allocate array with _Capacity elements
_Myfirst = 0, _Mylast = 0, _Myend = 0;
if (_Capacity == 0)
return (false);
else if (max_size() < _Capacity)
_Xlen(); // result too long
else
{ // nonempty array, allocate storage
_Myfirst = this->_Alval.allocate(_Capacit[/b]y);[/b][/u]
_Mylast = _Myfirst;
_Myend = _Myfirst + _Capacity;
}
return (true);
}
You are not listening to what people are telling you.
You do not have enough memory to allocate 57000 vectors of 57000 doubles.

Why do you need 57000 vectors of 57000 doubles?
You can't possibly have 57000 students with 57000 grades each, if that is what you're trying to represent. It's certainly not clear from your code what you're trying to represent.

Simply repeating that 16000 works and 57000 doesn't is not helpful.
You need to rethink how to solve this problem.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
http://v2.cplusplus.com/articles/jEywvCM9/
It makes it easier to read your code and it also makes it easier to respond to your post.


Maybe you can solve your problem by reading through the file in segments.

Then you can read from line 1 to 10000, write that to a file, clear the vectors and read from line 10001 to line 20000, append those lines to the newly created file, clear the vectors etc.

I really don't know what you're trying to do here so I can't really help other than guess.
@AbstractionAnon u mean like this...thanks for telling me

then this will come out
"Unhandled exception at 0x7748b727 in task 1.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x0041f0d8.


if i click break new tab with the mlock.c will appear and show this

1
2
3
4
5
6
7
8
9
void __cdecl _unlock (
int locknum
)
{
/*
* leave the critical section.
*/
LeaveCriticalSection( _locktable[locknum].lock );
}


if i click continue than another new tab name vector will appear and this will come out and there are yellow arrow at "_Myfirst = this->_Alval.allocate(_Capacit[/b]y);[/b][/u]"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
protected:
void _Assign_n(size_type _Count, const _Ty& _Val)
{	// assign _Count * _Val
_Ty _Tmp = _Val;	// in case _Val is in sequence
erase(begin(), end());
insert(begin(), _Count, _Tmp);
}

bool _Buy(size_type _Capacity)
{	// allocate array with _Capacity elements
_Myfirst = 0, _Mylast = 0, _Myend = 0;
if (_Capacity == 0)
return (false);
else if (max_size() < _Capacity)
_Xlen();	// result too long
else
{	// nonempty array, allocate storage
_Myfirst = this->_Alval.allocate(_Capacit[/b]y);[/b][/u]
_Mylast = _Myfirst;
_Myend = _Myfirst + _Capacity;
}
return (true);
}
i have .txt file that have 57000 line so i just want to program c++ to read that file..i want the program to print what is written on the file..that it..

the way i understand everybody said that

1. i didnt have enough memory to do so
2. i didnt clearly tell the program what i want to do

@Gaius i will try that..thank you
Topic archived. No new replies allowed.