Solved

Unhandled exception at 0x7c92a958 in...Access violation writing location 0x00030fb0.

Posted on 2008-10-20
3
1,334 Views
Last Modified: 2011-10-03
Code runs fine for awhile and then breaks in first for loop between Counter2 = 150 and Counter2 = 450 then throws the error:

Unhandled exception at 0x009810ab in Sim HW5.exe: 0xC0000005: Access violation writing location 0x00000000.

Any ideas why?

// Sim HW4.cpp : Defines the entry point for the console application.
//
 
 
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <cmath>
 
using namespace std;
 
int _tmain(int argc, _TCHAR* argv[])
{
	int Counter = 0;
	int Counter2 = 0;
	int Counter3 = 0;
 
	const int n = 100000;
	
	int fj[20] = {0};
	memset(fj,0,20 * sizeof(int));
	
	double Divisor = 1.0;
	double Z0 = 1;
	double Zi = 0.0;
	double ZiMinusOne = 0.0;
	double Ui = 0.0;
	double TwoPowerThirtyOne;
	TwoPowerThirtyOne = pow((double)2,31);
	double v[100000];
	memset(v,0,100000 * sizeof(double)); 
 
	while (Counter < n+1){
		if (Counter == 0)
			ZiMinusOne = Z0;
		else
			ZiMinusOne = Zi;
 
		Divisor = (floor( 16807*ZiMinusOne/(TwoPowerThirtyOne-1)));
		Zi = 16807*ZiMinusOne - (Divisor) * (TwoPowerThirtyOne-1);
 
		Ui = Zi/(TwoPowerThirtyOne-1);
 
		v[Counter] = Zi;
 
		Counter++;
		}
 
	for (Counter2 = 0; Counter2 < n-1; Counter2++){
		cout << Counter2 << endl;
		for (Counter3 = 0; Counter3 < 20; Counter3++){
			if (v[Counter2] >= Counter3*TwoPowerThirtyOne/20 && v[Counter2] <= (Counter3+1)*TwoPowerThirtyOne/20)
				fj[Counter2]++;
		}
	}
/*
	int i, j;
	int k = 20;
	double ChiSq = 0;
	for (Counter2 = 0; Counter2 < k; Counter2++){
		cout << "fj" << Counter2 << ": " << fj[Counter2] << endl;
		ChiSq += k/n*(pow((double)(fj[Counter2]-n/k),2));
	}
*/
	system ("PAUSE");

Open in new window

0
Comment
Question by:ltdanp22
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 6

Expert Comment

by:RishadanPort
ID: 22763360
Could you give us a rough idea of what exactly your code is doing?
0
 
LVL 14

Accepted Solution

by:
wayside earned 500 total points
ID: 22763412
      const int n = 100000;
       
        int fj[20] = {0};
...

        for (Counter2 = 0; Counter2 < n-1; Counter2++){
                for (Counter3 = 0; Counter3 < 20; Counter3++){
                        if (v[Counter2] >= Counter3*TwoPowerThirtyOne/20 && v[Counter2] <= (Counter3+1)*TwoPowerThirtyOne/20)
                                fj[Counter2]++;                 // <-!!!!!!!!!!!!!!
                }
        }


fj has only 20 elements, Counter2 goes up to 99999 (n-1).

So you are inserting elements far beyond the end of the fj[] array by using fj[Counter2]. At some point this causes sufficient stack corruption to crash the program.

0
 

Author Comment

by:ltdanp22
ID: 22764655
THANK YOU! Wayside I could kiss you. Too bad for you I'm not more attractive. Have a great day. :)
Dan
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question