C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old February 5th, 2013, 10:22 PM
rundmc123 rundmc123 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 1 rundmc123 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 33 m 54 sec
Reputation Power: 0
Memory & arrays - Heap error during cout

Getting an error on heap when I try to cout the return value of myVector[index]. Any ideas?

Header file:
Code:
#pragma once
	#define INITIAL_CAPACITY 100
	#define CAPACITY_BOOST 40


	//Encapsulates the C-array
	template <typename DATA_TYPE>
	class Vector
	{
	public:
	//Default / init-constructor hybrid
	Vector(int initialCapacity = INITIAL_CAPACITY)
	{
		Size=0;
		Capacity = initialCapacity;

		//Allocate the encapsulated C-array
		Data= new DATA_TYPE[Size];
	}

	//MUST HAVE A COPY-CONSTRUCTOR THAT PERFORMS deep-copy
	Vector(const Vector& copyFrom)
	{
		//Necessary to prevent assignment operator from crashing
		//because it will attempt to Delete[] Data whe the Data pointer is garbage.
		Data=NULL;
		//Use assignment operator to perform the deep copy
		*this = copyFrom;
	}

	
	//The class MUST have a destructor
	~Vector()
	{
		//Deallocate memory that our class has allocated
		delete[] Data;
	}
	//MUST have an assignment operator that performs deep copy
	Vector& operator =(const Vector& copyFrom)
	{
		//0. Delete the old memory
		delete[] Data;
		//1. Copy size and Capacity
		Size = copyFrom.Size;
		Capacity = copyFrom.Capacity;
		//2. Allocate Memory
		Data = new DATA_TYPE[Capacity];
		//3. Copy elemenets
		for(int i=0; i < Size; i++)
			Data[i]= copyFrom.Data[i];

		//All assignment operators should return *this
		return *this;
	}

	//Get accessors to return the values of Size and Capacity
	int GetSize() const
	{
		return this->Size;
	}

	int GetCapacity() const
	{
		return Capacity;
	}

	void Insert(int insertAt, const DATA_TYPE& newElement)
	{
		//**ASSIGNMENT**
		//1. Determine if we have enough capacity for extra element(reallocate)
		Size=GetSize();
		if(Size>=Capacity)
		{
			Capacity += CAPACITY_BOOST;
		}
		//Use a function to check bounds.
		/*
		if((insertAt > Capacity)||(insertAt < 0))
		{
			throw "Index is out of bounds";
		}
		*/
		//2.Move the tail
		for (int i=Size+1; i > insertAt; i--)
			Data[i]=Data[i-1];

		//3.Insert element
		Data[insertAt]= newElement;
        
	}
	//Inserts a new element at the end fo the Vector and increments the size
	void Add(const DATA_TYPE& newElement)
	{
    
		Insert(Size, newElement);
		Size++;
	}

	void Remove(int index)
	{
		//**ASSIGNMENT**
		//Resize. Shrink vector when you have too much capacity
		//TEST EVERYTHING
	}

	// Index operator
	DATA_TYPE operator[] (int index) const
	{
		// Check the boundsn and throw an exception
		//if ( (index < 0) || (index >= Size) )
			//throw "Error";

		return Data[index];
	}
    

	private:
	//The count of actually used C-array elements
	int Size;
	//The count of the allocated C-array elements
	int Capacity;
	//The encapsulated C-array (pointer)

	DATA_TYPE* Data;
	};


Main:
Code:
#include <iostream>
#include "vector.h"
using namespace std;

#define TEST_CAPACITY 100
#define TEST_SIZE 10

template<typename DATA_TYPE>

void main()
{
	//myVector is initialized using the default constructor
	Vector<int> myVector;
	
	//Populate myVector with some test values
	for (int i=0; i< TEST_SIZE; i++)
		myVector.Add(i);

	for(int i = 0; i < TEST_SIZE; i++)
	{
		cout << myVector[i];
	}
	system("pause");
}

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Memory & arrays - Heap error during cout


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.

© 2003-2014 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap