Hi! First, I want to say that this is not any type of school assignment. I'm in the IT field and looking to work on learning C++. I've been following the book from: http://msdn.microsoft.com/en-us/beginner/cc305129.aspx and I'm now trying things on my own.

The program which the code below compiles will sometimes crash out at the beginning of its execution. I'm trying to put in any type of error catcher where the program will know that the program is about to crash, and it'll exit on its own behalf instead of crashing out by Windows Illegal Operation. I hope this makes sense.

#include <iostream>
#include <windows.h>
using namespace std;

int main()
{
               //Initialize variables
	int n = 0;
	int z = 0;
	int *i,j[10];
	double *f,g[10];
	float *k,l[21];
	int *p,m[1];
	int x;

               // Associate pointers...
	i=j;
	f=g;
	k=l;
	p=m;
               // "Splash" Screen
	cout << "\nA just for fun program\n";
	Sleep(1000);

               //Start the real work here
	for(x=1;x<350;x++)
	{
		cout << "\nAttempt #: " << x << '\n';
		Sleep(10);
		n = (rand()%375)+11;
		cout << "Scanning to look cool: ";
		z = i[n]+x;
		cout << z;
		if (!z) 
		{
			cout << "Trying to catch an error here...but I'm not working \nExiting\n";
			break;
		}
		cout << i[n]+x<< ' ' << f+x << ' ' << k+x <<'\n';
	}
	cout << "This looked cool...";
return 0;
}

I don't think "z" is my problem, but I just don't know enough to troubleshoot any further. Can anyone point me in the right direction? Thank you, -MT

Well, error catching is what try-catch blocks are for...

But if you're talking about some pointer-related bug, the right answer is to avoid using pointers directly in the first place. I don't know what your program is trying to do -- why are you deliberately indexing past the end of an array?

No real answer for that. I'm just toying around and referencing the 'book' I'm reading. Looking back at it, you're right, the book's for loop example doesn't go past the end of the array. Just little things I'm playing around with.

While I'm here...Why would one want to avoid using pointers directly? if I had....

int *x,num;

x = &num;
.......
return 0;

Would that still be considered as directly accessing a pointer? Thanks,

Using pointers directly is undesirable because pointers are dangerous. If you have a pointer, the thing it's pointing to could become destroyed, so you have to make sure that hasn't been done. Also, if you're pointing to things manually on the heap, you have to make sure to destroy those things -- so you have have to keep track of who owns what. Keeping track of things like this is a pain. There are lots of ways to avoid using pointers. For starters, use vectors instead of raw C-style arrays. Pass arguments to functions by reference instead of passing pointers. Use the various kinds of smart pointers instead of raw C-style pointers.

Since you are new to C++, you don't need to worry about any of the things I've said! Continue to use pointers so that you can learn the low-level details of the language, and later, you will appreciate the other tools that are provided.

One reason you can't just "catch and handle" pointer errors is that if you overwrite something in the wrong place of memory, it can totally screw with the behavior of your program. You could have overwritten other variables, especially hidden values like the addresses the processor would jump to after it returns from a function. In particular, the mechanism C++ uses for propagating exceptions might have been totally corrupted. That's why you can't rely on the exception system of C++ to handle errors where pointers point to unmodified locations.

The only reasonable way to protect yourself from these kinds of errors is to watch things from a separate process. This is one reason why the web browser Google Chrome has all its tabs in separate processes.

Fantastic, Rashakil! Thanks for your explanation. Just so I can say I have some sort of "understanding" of this---pointers could be dangerous because if the 'variable' I told the pointer to attach to goes away, the rest of the program would act up?

So for example:
if I setup "x" as a pointer, and I somewhere along the way 'get rid' of "num" (for whatever reason), then the pointer itself will become unstable because it can't reference what num was?

---using the code earlier, something like this:
int *x,num;

x = &num;

Thanks again, Rashakil!

I disagree with rashakil. Using pointers properly can greatly increase the efficiency and speed of your code. Just compare C++ to C#. C++ programs run much much faster than C#, because C# doesn't support pointers (except in strange unsafe blocks). The reason your program is crashing is because your pointer i is pointing to an array of size 10, and you are trying to access an index of up to 375. No idea what you are trying to do here, but this is certainly the cause of the error.

Hi skatamatic, thanks for your input. The weird thing is that the program does run, but will occasionally crash 1 or 2 times out of 5 when executed. But, the explanations given in this thread do make sense.

Fantastic, Rashakil! Thanks for your explanation. Just so I can say I have some sort of "understanding" of this---pointers could be dangerous because if the 'variable' I told the pointer to attach to goes away, the rest of the program would act up?

Yes. Here's an example.

int* foo(int x) {
    int y = 3 + x;  // contrived example
    return &y;  // the variable y will no longer exist when the function returns.
}
void bar() {
    int* p = foo(4);
    int k = *p;  // undefined behavior, but probably k will contain 7.
    blah();  // almost certainly overwrites the value p points to.
    int j = *p;  // j is probably not 7.
    // ...
    something(p); // hilarity ensues
    // ...
    // ...
    // possible alternate error:
    delete p;  // oops, forgot whether p was pointing to the heap or stack   
}

I disagree with rashakil. Using pointers properly can greatly increase the efficiency and speed of your code.

You might not even be disagreeing with me depending on your definition of "properly", but I hope you're not implying that pointers are reasonable to use in place of iterators and smart pointer classes.

This question has already been answered. Start a new discussion instead.