I need urgent Help with Access violation reading location.

Hello dear all c++ experts, in fact this is repost and I could not get my answer, I have headache and big problem, since I MUST submit my thesis code by coming week and I have some problems in its debugging. In a line I have the following code line:
memcpy(x[0].slittle,y[0].slittle,sizeof(int)*s1_leng);
And while running the code I get the following error:
Unhandled exception at 0x012a4f5a in RandS.exe: 0xC0000005: Access violation reading location 0x00000000.
Also for more information I have to say that this is an optimisation algorithm and in the definition of variables I have:
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
176
177
178
179
180
181
182
183
184
#include <conio.h>
#include<math.h>
#include<conio.h>
#include<string.h>
#include<time.h>
#include<cmath>
//#include<stdlib.h>
#include<string>
#include<iostream>
#include<fstream>

int *sort_cost;
int *sort_sl;
#define POP_S 60
using namespace std;
//*******************************************************
float alpha_final=0.95;
int s2_leng;
int s1_leng;
int N_pro;
int simu_iteration=30;
int total_simu_time=3;
float order_cost_init=50;
float unit_order_cost=5;
float unit_hcost=2;
float unit_shcost=1;
float rec_time_l=1;
float rec_time_u=2;
float *dem_prob;
float lambda=0.1;
float slittle_l=20;
float slittle_u=115;
float scap_l=45;
float scap_u=235;
float differ_l=25;
int mult=5;
// These are some variables related to optimization algorithm.
//*******************************************************






float mprob=0.1;
int *pop_ind;
int *pop_ind2;
int INF=10000000000;
float alpha;



float *f1,*s2,*f2,*s3;
int **Sp;
int *zero_2p;
int *d;//due dates
int *pt;//zamane hamlo naghl
int *pa;
int *w1;
int *w2;
int *w3;
int ***s;
int **s0;
int **t;
int **SOH;
int **FOH;
int *np;
int rank_max;
float *st;//start time of the job
float **ost;// start time of the operations in a job
float **oft;// finish time of the operations in a job
float *ft;// finish time of the job
float *c;
int P=200;//population size;
//***************structures:*******************
//*********************************************
typedef struct sol {

	int *slittle;
	int s;
	int S;
	int *scap;
	int rank1;
	int rank2;
	float cost;
	float sl;

	double *scost;
	double *ssl;
	int *bit;
	int rank;
	float fit;
	float f1;//flow time
	float f2;//lateness and earliness
	float distance;
	float pen;
};
struct front {
	int *F;
	int num;
};
struct front *FR;
struct front Q;
//**********************************************
//**********************************************
sol *popcurrent, *popnext;
sol temp;
sol *pareto;
//***************prototypes:*******************
//*********************************************
void sort22(sol *x,int z,int z2);
void OX(int *p1,int *p2,int *c1, int *c2, int z);
void y0(sol *xx);
void set_value(sol *x);
void chromcpy(sol*x,sol*y);
void y1(sol*x);
void shift(int*x,int z);
void mutation(sol*x);
void PMX(int *p1,int *p2,int *c1, int *c2, int z);
int selection(int *p1,int *p2,sol*x);
void sort3(sol *x,int z,int z2);
void sort2(sol *x,int z,int z2);
void sort1(sol *x,int z,int z2);
int check_dom(float w,float x,float y,float z);
void preprocess();
void mem_aloc();
void init(sol*);
void init_perm(int*x,int y);
void y(sol*);
void fast_nd_sort(sol *x,int ind);
void crowd_distance_assignment2(sol *x,int);
void fast_nd_sort_2(sol *x,int ind);
//**********************************************
//**********************************************
int main(){srand(time(NULL));
int len;
clock_t msec3,msec4,dur2;
int iter;
int par1,par2;
int lll;
preprocess();
mem_aloc();

FR=(struct front *)malloc(sizeof(struct front)*2*P);

for (int i=0;i<2*P;i++)
{
	FR[i].F=(int *)malloc(sizeof(int)*2*P);
}

	//ifstream ifs("1.txt");
	//for (int i=0;i<Njobs;i++)
	//{
	//	ifs>>popcurrent[0].bit[i];

	//}
	//		y(popcurrent);

			//cout<<popcurrent[0].f1<<" "<<popcurrent[0].f2<<" "<<popcurrent[0].f1+popcurrent[0].f2<<"\n";
			//getch();


	iter=0;
	msec3=clock();
	init(popcurrent);


	fast_nd_sort_2(popcurrent,P);

	if (iter==0)
	{
		//cout<<"\nare hast\n";
		//getch();
		ofstream ofsss("outtt.txt");

	for (int i=0;i<P;i++) if(popcurrent[i].rank==1)
	{
		ofsss<<popcurrent[i].cost<<" "<<popcurrent[i].sl<<"\n";

	}
	ofsss.close();
	}

	//cout<<"\n**fndsrt ended\n"; 


Also about the error line I have:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Void chromcpy(sol*x,sol*y){//copy y to x;

	memcpy(x[0].slittle,y[0].slittle,sizeof(int)*s1_leng);////The error appears here.
	memcpy(x[0].scap,y[0].scap,sizeof(int)*s2_leng);
	memcpy(x[0].scost,y[0].scost,sizeof(int)*P);
	memcpy(x[0].ssl,y[0].ssl,sizeof(int)*P);
	x[0].s=y[0].s;
	x[0].S=y[0].S;
	x[0].f1=y[0].f1;//cost
	x[0].f2=y[0].f2;//service level
	x[0].fit=y[0].fit;
	x[0].rank=y[0].rank;
	x[0].distance=y[0].distance;

}

This is the link of my discussion but I rectified and modified it:
http://www.cplusplus.com/forum/beginner/86819/
Last edited on
I think that ne555 hit it on the head in the other post.

My particular guess is that you're trying to deference a null pointer. Make sure that the x and y arguments that are passed into the function are pointing to correctly allocated memory.

Also, as ne555 said, you're using C++ here so new would be preferable to malloc.
you have an endless row of global variables. Most of them are not initialized. This is a potiential for more than one crash.

Yes, you have to check whether x, y, slittle, and s1_leng contain the expected values, i.e. you need a debugger. Furthermore: That the crash occures at that point doesn't mean that everything before was ok.

So start with initializing all global variables (to 0). Either memset the arrays to 0 or use calloc. That makes it easier to pinpoint the real source of the crash.

http://www.cplusplus.com/reference/cstring/memset/
http://www.cplusplus.com/reference/cstdlib/calloc/

Consider if really each and every array needs to be allocated dynamically
Thanks from all of you, I will check your solutions.
Dear iHutch105, may you please tell me that how can we ensure that they are pointing to the correctly allocated memory. For example when while debugging I check them, they do not have meaning full values and for example each of them have a vague value.
Last edited on
Topic archived. No new replies allowed.