I have a feeling my logic is off here.....crashes randomly depending on input

//Exc_6.cpp - practice with arrays


using namespace std;

void fillArray(double array[], int size);
void showArray(const double array[], int size);
void reverseTheArray(double array[], double reveseArray[], int size);

int main()
	int size;
	cout << "Please enter the size of an array: ";
	cin >> size;
	double **array;
	double **reverseArray;
	array = new double *[size];
	reverseArray = new double *[size];
	fillArray(*array, size);
	cout << "Displaying array." << endl;
	showArray(*array, size);
	cout << "Reversing array." << endl;
	reverseTheArray(*array, *reverseArray, size);
	cout << "Result of reversed array." << endl;
	showArray(*reverseArray, size);
	//delete array;
	//delete reverseArray;

void fillArray(double array[], int size)
	double temp;
	for (int i = 0; i < size; i++)
		cout << "Input a double number: ";
		cin >> temp;
		array[i] = temp;

void showArray(const double array[], int size)
	for (int i = 0; i < size; i++)
		cout << "Array slot " << i+1 << " contains " << array[i] << endl;

void reverseTheArray(double array[], double reverseArray[], int size)
	int count;
	count = size;
	for (int i = 0; count > 0; count--, i++)
		reverseArray[i] = array[count];
7 Years
Discussion Span
Last Post by MasterGberry

>> I have a feeling my logic is off here.....crashes randomly depending on input

Seems to me it should crash EVERYWHERE, REGARDLESS of input.

Nowhere do you have the following:

new double[...]

Yet you fill in the array with doubles. That's a seg fault. There's no memory for double, only pointers to doubles.

I think your syntax is off. I see no reason for a double**. I see no reason for an array of double*. I think you want two double* objects, each pointing to an array of doubles.


The problem is basically simplified to this:

double **array = new double* [size];

What you have done is declare a array as a pointer to a pointer of arrays. That is effectively a double array like dynamic version of double A[10][20]; . However, you did not initialize the pointers to the arrays.

What you most likely want is this:

double *array=new double[size];
double *reverseArray=new double[size];
// etc....
delete [] array;
delete [] reverseArray;

Note the rule that if you do X=new Something[Size]; you delete with delete [] X , and if you use X=new Something; then you delete with just delete X; . Almost every piece of memory that is allocated with a new should be deleted somehow. delete Note added after posting : Looks like Verion got there first, (obviously I type too slow ;) but note we are saying the same thing in slightly different ways.

Edited by StuXYZ: n/a


I kept messing around with things and got really close to this but it got messed up for some reason...i am putting this down on a note, cuz this is the 2nd time i made this mistake. Thanks very much for the help guys. I really appreciate the line by line coding

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.