my program keeps crashing

K.I.L.E.R

Retarded moron
Veteran
Code:
void main(void)
{
	double a[5], b=0.0;
	a[0] = 0;
	a[1] = 5;
	a[2] = 10;
	a[3] = 15;
	a[4] = 20;
	a[5] = 25;

	b = a[5]+a[1];
	cout << b << endl;
}

WTF?

AFAIK I am not doing anything wrong with the arrays.

I kept getting that sorry for your inconvenience message for cpp1.cpp. :?

this works though:

Code:
void main(void)
{
	double b;
	double a[2];
	
	a[0]=1;
	a[1]=2;
	a[2]=3;

	b = a[0]*a[1]+a[2];

	cout << b << endl;
}
 
Code:
double a[5];
That's a 5 element array, index range 0 ... 4. Index 5 is out of bound. And when writing to it, anything can happen. In case 2 you're just lucky that it works.

(It could be that your compiler puts the variables in reverse order and a[2] in the srecond case is the same memory position as b, and that's why it doesn't crash.)
 
The stack arrangement for the first one would be this:
Code:
saved instruction pointer
saved base pointer
a[4]
a[3]
a[2]
a[1]
a[0]
b
Setting a[5] would be overwriting the saved base pointer and the saved instruction pointer (since doubles are 8 bytes and the pointers are 4 bytes) causing a crash when main returns.

In the second one the order would be:
Code:
saved instruction pointer
saved base pointer
b
a[1]
a[0]
Setting a[2] in that would actually be setting b
 
Ahhh, I think I understand now. :)

In the first, b = a[5] and a[5] is 4b while b holds 8 bytes.

So I a basically saying that 8b is equal to 4b?

On the second program am I not doing the same?

I'm kind of confused.

I think I have done something here too:
Code:
void main(double speed, double distance, double time)
{
	int formula[3], workingOut[4];

	char b = ' ';

	cout << "enter (s)peed, (d)istance or (t)ime: ";
	cin >> b;

	cout << "speed: ";
	cin >> workingOut[0];
	cout << "distance: ";
	cin >> workingOut[1];
	cout << "time: ";
	cin >> workingOut[2];

	formula[0] = distance/time;
	formula[1] = speed*time;
	formula[2] = distance/speed;

	switch(b)
	{
	case 's':
		cout << formula[0] << endl;
		break;
	case 'S':
		cout << "" << endl;
		break;
	
	case 'd':
		break;
	case 'D':
		break;

	case 't':
		break;
	case 'T':
		break;
	}
}
 
Code:
void main(double speed, double distance, double time)
Wrong declaration of main(). The runtime system that ultimately calls the main() function expects main() to be declared as one of follows:
Code:
int main( void )
int main( int argc, char **argv )
If you feed it any other argument list or return anything other than int, the results are undefined and varies from one system to another. The system certainly isn't smart enough to see where it would get three 'double' arguments from, so if the code even compiles, you will get either three garbage values or a crash at program end or start. If you expected it to read three 'double's from the command-line, the proper code for doing so goes as follows:
Code:
#include <stdio.h>
#include <stdlib.h>

int main( int argc, char **argv )
   {
   double speed, distance, time;   
   if(argc < 4) 
      {
      printf("Need to supply at least 3 arguments\n");
      return EXIT_FAILURE;
      }
    speed = atof( argv[1] );
    distance = atof( argv[2] );
    time = atof( argv[3] );
     
   /* rest of your code goes here */

   return 0;
   }
 
I'm still geting garbage. Let me guess, the arrays can't be doubles, floats or characters?

Code:
void main(void)
{
	float formula[3], workingOut[3], speed=0.0, distance=0.0, time=0.0;

	char b = ' ';

	cout << "enter (s)peed, (d)istance or (t)ime: ";
	cin >> b;

	if (b == 's' || b == 'S' || b == 'd' || b == 'D' || b =='t' || b == 'T')
	{
		cout << "speed: ";
		cin >> workingOut[0];
		cout << "distance: ";
		cin >> workingOut[1];
		cout << "time: ";
		cin >> workingOut[2];

		formula[0] = distance/time;
		formula[1] = speed*time;
		formula[2] = distance/speed;

		switch(b)
		{
		case 's':
			cout << formula[0] << endl;
			break;
		case 'S':
			cout << formula[0] << endl;
			break;
			
		case 'd':
			cout << formula[1] << endl;
			break;
		case 'D':
			cout << formula[1] << endl;
			break;

		case 't':
			cout << formula[2] << endl;
			break;
			
		case 'T':
			cout << formula[2] << endl;
			break;
		}
	}
}
 
There are no particular restrictions on what you can make arrays of in C/C++, but you can't assume that an array will overlap in storage with anything else (it won't unless you make a union or do weird pointer arithmetic). So what you are doing seems to be as follows:
Code:
 speed=0.0, distance=0.0, time=0.0;
OK, zeroing out variables before use is usually good practice.
Code:
   cout << "speed: ";
      cin >> workingOut[0];
      cout << "distance: ";
      cin >> workingOut[1];
      cout << "time: ";
      cin >> workingOut[2];
Any particular reason you assign to the 'workingOut' array instead of directly to 'speed', 'distance' and 'time'?
Code:
      formula[0] = distance/time;
      formula[1] = speed*time;
      formula[2] = distance/speed;
When you reach this code, 'distance', 'time' and 'speed' will all have the value 0.0 because you never assigned anything else to them. So the three elements of 'formula' will get the values:

formula[0] = Not-a-Number (zero divided by zero)
formula[1] = 0.0
formula[2] = Not-a-Number (zero divided by zero)
 
Thanks, now it's working. Why can't I figure these things out for myself?
Am I that stupid? :?

Code:
void main(void)
{
	float formula[3], workingOut[3];

	char b = ' ';

	cout << "enter (s)peed, (d)istance or (t)ime: ";
	cin >> b;

	if (b == 's' || b == 'S' || b == 'd' || b == 'D' || b =='t' || b == 'T')
	{
		cout << "speed: ";
		cin >> workingOut[0];
		cout << "distance: ";
		cin >> workingOut[1];
		cout << "time: ";
		cin >> workingOut[2];

		formula[0] = workingOut[1]/workingOut[2];
		formula[1] = workingOut[0]*workingOut[2];
		formula[2] = workingOut[1]/workingOut[0];

		switch(b)
		{
		case 's':
			cout << formula[0] << endl;
			break;
		case 'S':
			cout << formula[0] << endl;
			break;
			
		case 'd':
			cout << formula[1] << endl;
			break;
		case 'D':
			cout << formula[1] << endl;
			break;

		case 't':
			cout << formula[2] << endl;
			break;
			
		case 'T':
			cout << formula[2] << endl;
			break;
		}
	}
}
 
Try stepping through the code with a calculator.
At each point make sure that the value from a statement is what you expect.

You can tidy up that switch statement by combining the upper and lowercase vase statements
e.g
Code:
     case's':
     case'S':
         ...
         ...
         break;

    case 'd':
    case 'D':
        ...


or you could just use the
toupper or tolower functions.
 
toupper converts a character to uppercase

eg
Code:
char temp1;
char temp2='a';

temp1=toupper(temp2);
// temp1 is now 'A'

tolower converts a character to lower case.

They are standard functions, I think they are defined in stdlib.h.
Check the documentation.

It is also easy enough to roll your own version if needed.

CC
 
Thanks.

Another thing, how do I hadle 4D arrays?

I know 3D arrays are handled like 3d do-ords.

[1][1][1]
x y z

Chess board
3[][][]
2[][][]
1[x/z][][]
1 2 3

The z is behind the x tile but at the same co-ord.

Now if I do 4d arrays:

Chess board
3[][][]
2[][][] +1D time?
1[x/z][][]
1 2 3

:?
 
There might be some that disagree with this, but you will find many books that basically advise against using things like 4D arrays. I would agree.

If I ever found myself looking at a problem and thinking, "OK, a 4D array could be used here"...in the back of my mind, I know there's a better way of doing it.

Personally, I think it comes down to code maintenance. That is to say, how easy is it to debug, extend at some later point in time, or even how easy/difficult would it be for somebody else reading the code to easily understand what's going on.
 
Back
Top