Write access violation

I got a code like this
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
  #include <iostream>
#include <string>
#include <fstream>

#define MAX_SUBJECTS 10

using namespace std;

struct Subjects
{
	string id, w1, w2, w3, w4, w5, w6, w7, w8;
};

void readCheckin(ifstream &in, const string path, Subjects s[], size_t &index)
{
	in.open(path);
	if (!in)
	{
		cout << "Can't open!\n";
	}

	string heading;

	getline(in, heading, '\n');
	while (getline(in, s[index].id, ','))
	{
		getline(in, s[index].w1, ',');
		getline(in, s[index].w2, ',');
		getline(in, s[index].w3, ',');
		getline(in, s[index].w4, ',');
		getline(in, s[index].w5, ',');
		getline(in, s[index].w6, ',');
		getline(in, s[index].w7, ',');
		getline(in, s[index++].w8, '\n');
	}
	in.close();
}
void Checkin(ofstream &out, const string path, Subjects s[], size_t &index)
{


	system("cls");
	string ID;
	cout << "Enter the ID to check-in: ";
	cin >> ID;

	system("cls");
	for (int i = 0; i < index; i++)
	{
		if (s[i].id == ID)
		{
			int c;
			cout << "1. W1\n" << "2. W2\n" << "3. W3\n" << "4. W4\n" << "5. W5\n" << "6. W6\n" << "7. W7\n" << "8. W8\n";
			cout << "Choose: ";
			cin >> c;

			system("cls");
			cout << "Enter 0 (absented) or 1 (attended)\n";
			string W1, W2, W3, W4, W5, W6, W7, W8;
			switch (c)
			{
			case 1:
				cin >> W1;
				s[i].w1 = W1;
				break;
			case 2:
				cin >> W2;
				s[i].w2 = W2;
				break;
			case 3:
				cin >> W3;
				s[i].w2 = W2;
				break;
			case 4:
				cin >> W4;
				s[i].w4 = W4;
				break;
			case 5:
				cin >> W5;
				s[i].w5 = W5;
				break;
			case 6:
				cin >> W6;
				s[i].w6 = W6;
				break;
			case 7:
				cin >> W7;
				s[i].w7 = W7;
				break;
			case 8:
				cin >> W8;
				s[i].w8 = W8;
				break;
			}
		}
	}

	out.open(path);
	if (!out)
	{
		cout << "Error\n";
	}
	out << "Student ID,1,2,3,4,5,6,7,8\n";
	for (int i = 0; i < index; i++)
	{
		out << s[i].id << "," << s[i].w1 << "," << s[i].w2 << ","
			<< s[i].w3 << "," << s[i].w4 << "," << s[i].w5 << ","
			<< s[i].w6 << "," << s[i].w7 << "," << s[i].w8 << endl;
	}
	out.close();
}

int main()
{
	string CLC1[]{ "E:/cth008clc1.csv", "E:/tth026clc1.csv", "E:/ctt010clc1.csv", "E:/kth001clc1.csv", "E:/cth001clc1.csv" };
	Subjects CTT008clc1[MAX_SUBJECTS], TTH026clc1[MAX_SUBJECTS], CTT010clc1[MAX_SUBJECTS], KTH001clc1[MAX_SUBJECTS], CTH001clc1[MAX_SUBJECTS];
	ifstream ctt008clc1, tth026clc1, ctt010clc1, kth001clc1, cth001clc1;
	ofstream out1, out2, out3, out4, out5;
	size_t ctt008{}, tth026{}, ctt010{}, kth010{}, kth001{}, cth001{};

	for (int i = 0; i < CLC1->size(); i++)
	{
		switch (i)
		{
		case 0:
			readCheckin(ctt008clc1, CLC1[i], CTT008clc1, ctt008);
			break;
		case 1:
			readCheckin(tth026clc1, CLC1[i], TTH026clc1, tth026);
			break;
		case 2:
			readCheckin(ctt010clc1, CLC1[i], CTT010clc1, ctt010);
			break;
		case 3:
			readCheckin(kth001clc1, CLC1[i], KTH001clc1, kth001);
			break;
		case 4:
			readCheckin(cth001clc1, CLC1[i], CTH001clc1, cth001);
			break;
		}
	}

	int c;
	cout << "1. 18clc1\n" << "2. 18clc2\n" << "3. 18clc3\n" << "4. 18clc4\n" << "5. 18clc5\n";
	cout << "Choose class: ";
	cin >> c;

	system("cls");
	if (c == 1)
	{
		int s;
		cout << "1. CTT008\n" << "2. TTH026\n" << "3. CTT010\n" << "4. KTH001\n" << "5. CTH001\n";
		cout << "Enter subject: ";
		cin >> s;
		switch (s)
		{
		case 1:
			Checkin(out1, CLC1[0], CTT008clc1, ctt008);
			break;
		case 2:
			Checkin(out2, CLC1[1], TTH026clc1, tth026);
			break;
		case 3:
			Checkin(out3, CLC1[2], CTT010clc1, ctt010);
			break;
		case 4:
			Checkin(out4, CLC1[3], KTH001clc1, kth001);
			break;
		case 5:
			Checkin(out5, CLC1[4], CTH001clc1, cth001);
			break;
		}
	}

}

This code is for reading .csv files to do a check-in for the student, one of the files is:
1
2
3
4
5
6
7
8
9
10
11
Student ID,1,2,3,4,5,6,7,8
1653054,0,0,0,0,0,0,0,0
1653080,0,0,0,0,0,0,0,0
1753017,0,0,0,0,0,0,0,0
1753097,0,0,0,0,0,0,0,0
1753099,0,0,0,0,0,0,0,0
1753103,0,0,0,0,0,0,0,0
1753120,0,0,0,0,0,0,0,0
1753128,0,0,0,0,0,0,0,0
1753129,0,0,0,0,0,0,0,0
1753131,0,0,0,0,0,0,0,0

Each columns is the student's ID and the rest is the 8 weeks of the course, and each 0. If the student absented, remain 0, if he/she attended that course, change to 1.
When I compiled it, what I saw was a file name iosfwd opened, and at the line:
1
2
3
4
static _CONSTEXPR17 void assign(char& _Left, const char& _Right) noexcept
{// assign an element
    _Left = _Right;
}

The compiler told me: Exception thrown: write access violation.**_Left** was 0xCCCCCCCC. occurred.
What is it? I'm quite new to C++ so the iosfwd thing appeared quite freaking me out!
When I compiled it...

That seems unlikely. This error happens when you RUN the program, not when you compile it.

"Write access violation" means that you are attempting to write to a memory address that you are not allowed to write to. You're trying to write to a memory address that the OS knows that it never gave to your process.

0xCCCCCCCC happens to be a "magic number" used (under some circumstances) by the MS C++ compiler/runtime to indicate a variable that has been created but has never been assigned any value.

So the first suspicion here is that you have a pointer, which has been created but never assigned a value by you, and as such it contains the value 0xCCCCCCCC. You then try to use that pointer, which is pointing at memory location 0xCCCCCCCC; you go to memory location 0xCCCCCCCC because that's where it's pointing, and try to write to that memory, and BOOM - the OS stops you.

You are probably passing that bad pointer to various library functions, and it gets all the way to this "assign" function before someone actually tries to write to the forbidden memory. But the mistake is yours; somewhere before that, you made the mistake and passed the bad pointer.


If you look back in the call stack (which should be presented at the point of error) you'll find where the execution left your source code. That's where you pass that bad pointer.

Topic archived. No new replies allowed.