Learning c++ pointers

laserpp

Distinguished
Nov 29, 2008
137
0
18,630
Hey my program is crashing and not sure why. Starting to learn about pointers and dynamically allocating memory. The program is suppose to set an array and then use a method to resize it to make it larger or smaller and copy the elements from the old array to the new one.

Main
Code:
//****************************************************
// File: MyArrayMain.cpp
//
// Purpose: Runs all the methods.
//
// Written By: Jonathan Salina 
//****************************************************


#include "stdafx.h"
#include "MyArray.h"


int _tmain(int argc, _TCHAR* argv[])
{
	//Creates an instance of MyArray
	MyArray ar;

	//Setting all the index to a value
	ar.Set(0,50);
	ar.Set(1,25);
	ar.Set(2,5);
	ar.Set(3,70);
	ar.Set(4,100);
	ar.Set(5,2000);
	ar.Set(6,500);
	ar.Set(7,8);
	ar.Set(8,70);
	ar.Set(9,99);
	
	//Testing everything
	cout << ar.Get(0) << endl << ar.Get(4) << endl << ar.Get(2) << endl << ar.Size() << endl;

	ar.Set(10,150);

	cout << endl;

	cout << ar.FindFirst(70) << endl;

	cout << ar.FindLast(70) << endl;

	ar.Initialize();

	cout << ar.Get(0) << "    " << ar.Get(1) << "    " << ar.Get(2) << "    " << ar.Get(3) << "    " << ar.Get(4) << "    " << ar.Get(5) << "    " << ar.Get(6) << "    " << ar.Get(7) << "    " << ar.Get(8) << "    " << ar.Get(9) << "    " << endl;

	ar.Initialize(100);

	cout << ar.Get(0) << "    " << ar.Get(1) << "    " << ar.Get(2) << "    " << ar.Get(3) << "    " << ar.Get(4) << "    " << ar.Get(5) << "    " << ar.Get(6) << "    " << ar.Get(7) << "    " << ar.Get(8) << "    " << ar.Get(9) << "    " << endl;

	ar.Resize(15);

	cout << ar.Size() << endl;

	
	ar.Set(10,6);
	ar.Set(11,5);
	ar.Set(12,2);
	ar.Set(13,1);
	ar.Set(14,1000);

	cout << ar.Get(0) << "    " << ar.Get(1) << "    " << ar.Get(2) << "    " << ar.Get(3) << "    " << ar.Get(4) << "    " << ar.Get(5) << "    " << ar.Get(6) << "    " << ar.Get(7) << "    " << ar.Get(8) << "    " << ar.Get(9) << "    " << ar.Get(10) << "    " << ar.Get(11) << "    " << ar.Get(12) << "    " << ar.Get(13) << "    " << ar.Get(14) << "    " <<endl;

	
	ar.~MyArray();


	return 0;
}

Methods
Code:
//****************************************************
// File: MyArray.cpp
//
// Purpose: Holds all the methods and what they do.
//
// Written By: Jonathan Salina 
//****************************************************


#include "stdafx.h"
#include "MyArray.h"
#include <fstream>
#include <iostream>

//****************************************************
// Method: MyArray
//
// Purpose: Default constructor that sets all values to 0.
//****************************************************

MyArray::MyArray(){
	size = 10;
	a = new int[size];


}



//****************************************************
// Method: MyArray(int size)
//
// Purpose: Default constructor that dynamically allocate memory for the array .
//****************************************************

MyArray::MyArray(int size){
	a = new int[size];
}

//****************************************************
// Method: Set
//
// Purpose: Sets the value of the array.
//****************************************************

void MyArray::Set(int index, int value)
{
	if (index < 0 || index > size)
		cout << "Not a valid index";
	else
		a[index] = value;
}

//****************************************************
// Method: Get
//
// Purpose: Gets the value from the specific index.
//****************************************************

int MyArray::Get(int index)
{
	return a[index];
}

//****************************************************
// Method: Size
//
// Purpose: Shows the size of the array.
//****************************************************

int MyArray::Size()
{
	return size;
}

//****************************************************
// Method: FindFirst
//
// Purpose: Finds the first occurence of the specific value.
//****************************************************

int MyArray::FindFirst(int value)
{
	for (int i = 0; i<size; i++)
	{
		if (a[i] == value)
			return i;
	}

}

//****************************************************
// Method: FindLast
//
// Purpose: Finds the last occurence of the specific value.
//****************************************************

int MyArray::FindLast(int value)
{
	for (int i = size; i>=0; i--)
	{
		if (a[i] == value)
			return i;
	}
}

//****************************************************
// Method: Initialize
//
// Purpose: Initializes all elements to 0.
//****************************************************
void MyArray::Initialize()
{
	for (int i = 0; i<size; i++)
	{
		a[i] = 0;
	}
}

//****************************************************
// Method: Initialize
//
// Purpose: Initializes all elements to the specific value.
//****************************************************
void MyArray::Initialize(int value)
{
	for (int i = 0; i<size; i++)
	{
		a[i] = value;
	}
}


//new methods for v2.0

//****************************************************
// Method: ~MyArray
//
// Purpose: Deconstructor.
//****************************************************
MyArray::~MyArray(){
	delete[] a;
}


//****************************************************
// Method: Resize
//
// Purpose: Copies all the elements from the old array and stored then in the newer array.
//****************************************************
void MyArray::Resize(int value)
{
	if (value > size){
		int* temp = new int[value];
		for (int i=0; i<value; i++){
			temp[i] = a[i];
		}

		delete [] a;
		a = temp;
	}
	else{
		int* temp = new int[value];
		for (int i=0; i<value; i++){
			temp[i] = a[i];
		}

		delete [] a;
		a = temp;

	}

	size = value;
}

//****************************************************
// Method: Show
//
// Purpose: Shows all elements of the array on the screen.
//****************************************************
void MyArray::Show()
{
	for(int i=0; i > size; i++)
		cout << a[i] << endl;
}


//****************************************************
// Method: Show
//
// Purpose: Shows all elements of the array on the given output file stream.
//****************************************************
void MyArray::Show(ostream& open)
{
	ofstream o("textfile.txt", ios::out);
	

	for(int i=0; i > size; i++)
		o << a[i] << endl;
}

method prototypes
Code:
//****************************************************
// File: MyArray v2.0.h
//
// Purpose: Hold the method prototypes.
//
// Written By: Jonathan Salina
//****************************************************


#include <string>
#include <iostream>
#include <fstream>

using namespace std;

class MyArray
{
private:
	//array
	int size;
	int *a;

public:
	//Method prototypes
	MyArray();
	~MyArray();
	MyArray(int size);
	void Set(int index, int value);
	int Get(int index);
	int Size();
	int FindFirst(int value);
	int FindLast(int value);
	void Initialize();
	void Initialize(int value);

	//new methods for v2.0
	void Resize(int value);
	void Show();
	void Show(ostream& o);

};
 

Ijack

Distinguished
Indeed. You are setting the default size of the array as 10, but then (in the Set routine) testing if the index is greater than size. This means that, in the default case, where size = 10, you allow access to a[10], but the highest valid value is a[9].

It's the old "out by one" classic C error with arrays. I suspect there are other errors also, but that one is a killer.
 

laserpp

Distinguished
Nov 29, 2008
137
0
18,630
Forgot to delete that line, that is suppose to come after the resize method is called. Everything prints out on the console right, the array gets resized and the values are copied to the new array but it still gives me a error =/
 

TRENDING THREADS