Access violation writing location 0x00000001.

Hi Friends,

I am working Visual C++ 2012. I am trying to read a GLOBE file using the following code.

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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281

#include "stdafx.h"
#include "iostream"
#include <stdio.h>
#include "stdlib.h"
#include "string.h"
#include "cmath"
#include <errno.h>

using namespace std;

/* Conversion useful */
#ifndef GRA2RAD
	#define GRA2RAD ((22/7) / 180.0)
#endif
#ifndef RAD2GRA
	#define RAD2GRA (180.0 / (22/7))
#endif

/*! \ struct graminseg
  \ brief Structure to store the coordinates in the form (degree, minute, second) */ 
struct graminseg
  {
    int gra; /*!< sexagesimal degrees */
    int min; /*!< minutes */
    int seg; /*!< seconds*/
  };



int globe_nombre(char *target, int *lon0, int *lat0, double tlon, double lat)
{
  double lon;

  if (tlon >= 180.0)
    lon = tlon - 360.0;
  else
    lon = tlon;

  if (lat >= 50.0 )
    {
      *lat0 = 90;
      if (lon < -90.0)
        {
          strcpy(target, "a10g");
          *lon0 = -180;
        }
      else if (lon < 0.0)
        {
          strcpy(target, "b10g");
          *lon0 = -90;
        }
      else if (lon < 90.0)
        {
          strcpy(target, "c10g");
          *lon0 = 0;
        }
      else if (lon < 180.0)
        {
          strcpy(target, "d10g");
          *lon0 = 90;
        }
      else
        return 0;
    }
  else if (lat >= 0.0)
    {
      *lat0 = 50;
      if (lon < -90.0)
        {
          strcpy(target, "e10g");
          *lon0 = -180;
        }
      else if (lon < 0.0)
        {
          strcpy(target, "f10g");
          *lon0 = -90;
        }
      else if (lon < 90.0)
        {
          strcpy(target, "g10g");
          *lon0 = 0;
        }
      else if (lon < 180.0)
        {
          strcpy(target, "h10g");
          *lon0 = 90;
        }
    }
  else if (lat >= -50.0)
    {
      *lat0 = 0;
      if (lon < -90.0)
        {
          strcpy(target, "i10g");
          *lon0 = -180;
        }
      else if (lon < 0.0)
        {
          strcpy(target, "j10g");
          *lon0 = -90;
        }
      else if (lon < 90.0)
        {
          strcpy(target, "k10g");
          *lon0 = 0;
        }
      else if (lon < 180.0)
        {
          strcpy(target, "l10g");
          *lon0 = 90;
        }
    }
  else if (lat >= -90.0)
    {
      *lat0 = -50;
      if (lon < -90.0)
        {
          strcpy(target, "m10g");
          *lon0 = -180;
        }
      else if (lon < 0.0)
        {
          strcpy(target, "n10g");
          *lon0 = -90;
        }
      else if (lon < 90.0)
        {
          strcpy(target, "o10g");
          *lon0 = 0;
        }
      else if (lon < 180.0)
        {
          strcpy(target, "p10g");
          *lon0 = 90;
        }
    }
  else
    return 0;
  return 1;
}

int rad_to_graminseg(struct graminseg *gms, double rad)
{
  double a;
  a = rad * RAD2GRA;
  if ( a >= 0.0)
    {
      (gms->gra) = (int)floor(a);
      a = (a -(double)(gms->gra)) * 60.0;
      (gms->min) = (int)floor(a);
      a = (a -(double)(gms->min)) * 60.0;
      (gms->seg) = (int)floor(a);
    }
  else
    {
      a = -(a);
      (gms->gra) = (int)floor(a);
      a = (a -(double)(gms->gra)) * 60.0;
      (gms->min) = (int)floor(a);
      a = (a -(double)(gms->min)) * 60.0;
      (gms->seg) = (int)floor(a);
      (gms->gra) = -(gms->gra);
      (gms->min) = -(gms->min);
      (gms->seg) = -(gms->seg);
    }
  return 1;
}

int globe_nrow_gms(struct graminseg *gms, int lat0)
{
  int i;
  i = (120 * (lat0 - gms->gra) - 2 * gms->min - (gms->seg / 30));
  if (lat0 >= 0)
    i--;
  if (i >= 0)
    return i;
  return 0;
}

int globe_ncol_gms(struct graminseg *gms, int lon0)
{
  int i;
  i = ((gms->gra - lon0) * 120 + 2 * gms->min + (gms->seg / 30));
  if (lon0 < 0)
    i--;
  if (i >= 0)
    return i;
  /*if (i >= 10800) return 10799;*/
  return 0;
}

int globe_lee_dato_s(FILE *g, int *dato, int row, int col)
{
  off_t offset;
  offset = 21600 * row + 2 * col;
  if (fseek (g, offset, SEEK_SET))
    {
      return 0;
    }
  if ( fread(dato, sizeof(int), 1, g) != 1)
    {
      return 0;
    }
  return 1;
}


int main(int *alt, double *lat, double *lon, size_t ndat, const char *globe_path)// int argc, _TCHAR* argv[])
{
	try
	{
		int lat0, lon0, nc, nr, nv = 0;
		size_t i;
		int dato;
		char aux[128],file[256], oldfile[256];
		FILE *globe = NULL;
		struct graminseg gmslat, gmslon;

		globe_path="D:/Data/";

		// init some vars
		file[0] = '\0';
		oldfile[0] = '\0';

		for (i = 0; i < ndat; i++)
		{
			
			  /* what is the file to open ? */
			  strcpy(file, globe_path);
			  if (globe_nombre(aux, &lon0, &lat0, lon[i] * RAD2GRA, lat[i] * RAD2GRA))
				strcat(file, aux);
			  else
				{
				  if (globe != NULL)
					fclose(globe);
				  return -1;
				}

			   /* Row and column where the data */
			  rad_to_graminseg( &gmslat, lat[i]);
			  if (lon[i] > (22/7))
				rad_to_graminseg( &gmslon, lon[i] - 2 * (22/7));
			  else
				rad_to_graminseg( &gmslon, lon[i]);
			  nc = globe_ncol_gms(&gmslon, lon0);
			  nr = globe_nrow_gms(&gmslat, lat0);

	  
			  /* Open the file if needed */
			  if (strlen(oldfile) == 0 || strcmp(oldfile,file))
				{
				  if (globe != NULL)
					fclose(globe);
				  if ((globe = fopen(file, "r")) == NULL )
					{
						return -1;
					};
				}


			   if (globe_lee_dato_s (globe, &dato, nr, nc))
				{
				  alt[i] = (int) dato;
				  nv++;
				}
			  else
				  alt[i] = -9999;
				
			  strcpy(oldfile, file);
			  
		}
	}
	catch (exception e)
	{
		throw e;
	}

	/*cout <<"Hello" << endl;
	return 0;*/
}


I am getting the error in the if condition (bold). Kindly check the code and help me out.

Thank you

Vamsi
Last edited on
Sticks a breakpoint on this line:
if (globe_lee_dato_s (globe, &dato, nr, nc))
and examine variables.
e.g. what's the value globe, nr and nc?
what's is returned out of globe_lee_dato_s() in terms of &dato
Here, the Function is returning only 1 or 0, that function is returning 1 and it executing fine.

But when I use breakpoint in that alt[i] = int dato
showing the error

I am unable to insert picture here.
Last edited on
The parameters to your main are not standard. Please explain.
yea i can see it's returning 1 or 0. I guess what i'm asking is what's the value of dato before and after your fread call on line 201?

(and thanks for putting your code in code tags)

(i was trying to say include a breakpoint before your program errors)
Last edited on
Your alt is not a real pointer. Look at its value in the debugger carefully.
Hi all

@mutexe: You are welcome, I tried this before at first, this is my first question, so I searched for it then I changed.

Now,
@dato value is -32702964 from the function.
nr value is 5999
nc value is 0.

What you mean before your program errors?


@keskiverto: Can you please explain the standard things in Visual C++ 2012. I am new to this.


@MiiNiPaa: Can you please explain more. The debugger exits and show the message

Unhandled exception at 0x00205070 in ConsoleApplication1.exe: 0xC0000005: Access violation writing location 0x00000001.

Kindly help me out from this.

Thank you
Vamsi
you could try writing this in C++, it would be nicer than C.
(you're using c++ exception handling after all..)
I have done this in Visual C++ 2012.

Last edited on
i'm saying that you haven't written this in C++. This is 99% C. You are using try/catch C++ exceptions but apart from that it's C. (what actually in that code would throw an exception?)
Even signatures like:
int rad_to_graminseg(struct graminseg *gms, double rad)

are C-like. you dont need to add struct in there.


You could do away with pointers to make it clearer.
Is it essential that you pass integer pointers into functions, only to assign values to the dereferenced values?

strcpy isn't that safe:
http://www.cplusplus.com/forum/beginner/118771/
Last edited on
Ok, can you please explain, as I am new to C++ Programming, I am unable to understand this, even I tried so many notes.

Can you give me some examples.

In the above code,
1
2
3
4
5
6
7
if (globe_lee_dato_s (globe, &dato, nr, nc))
	{
		  alt[i] = (int) dato;
		  nv++;
	}
else
		  alt[i] = -9999;


In the bold section I am getting the error.
Last edited on
MiiNiPaa's point is that the arguments to main() in a C++ program are an integer and a pointer to an array of C strings: int main(int argc, char **argv); You have defined main as taking something else. Normally argc is at least 1 - the first argument is the name of the program. And if you don't pass any other command line arguments, then it will be exactly 1.

You've defined the first argument as int *alt. So that gets set to 1. And the first time you execute line 264, it tries to assign dat0 to an integer at address 1. This is exactly the behavior you're seeing.

Try printing out the parameters to main() . I think you'll find that they are all garbage. The next question is how do you want to set alt, lat, lon, ndat, and globe_path?
@vardhan36397
http://en.wikipedia.org/wiki/Entry_point#C_and_C.2B.2B

There is official C++ standard. Then there is MS Visual C++, a compiler and development environment that implements some parts of the C++ standard and additionally offers some nonstandard features.
Topic archived. No new replies allowed.