Czech

g++

Safe pointer. The class for safe memory allocation and deallocation.


07.05.17

Download source (3.6kB)

The class K3dSafePointer safely allocate and deallocate memory of any data types.

#pragma once

#include <iostream>	
#include <vector>
using namespace std ;

typedef vector<void*> TVoidArray;

namespace K3d
{
	template <class _T> class K3dSafePointer
	{
			TVoidArray m_tAddressArray;
			vector<void*>::iterator m_it;			
		public:
			K3dSafePointer(){}
			~K3dSafePointer(){}

			_T* SafeNew(size_t size = 1)
			{
				_T* ptr = NULL;
				try
				{
					ptr = new _T[size];
				}
				catch (...)
				{
					cerr << "Error -- K3dSafePointer::SafeNew() -- Pointer doesn`t allocated !!!" << endl;
				}
				// Add adress to the array
				m_tAddressArray.push_back(ptr);
				return ptr;
			}

			_T* SafeDelete(_T* _ptr)
			{
				if(_ptr != NULL)
				{
					// Find if adress exists in array
					m_it = find(m_tAddressArray.begin(), m_tAddressArray.end(), _ptr );
					if(m_it != m_tAddressArray.end())
					{
						// If adress exists delete pointer
						delete [] _ptr;
						_ptr = NULL;
						// Delete adress from array
						m_tAddressArray.erase(m_it);
					}
				}
				return _ptr;
			}
	};
}	/// End namestpace K3d

1) Allocated adress I add to address array.

m_tAddressArray.push_back(ptr);

2) During deallocation I find address array and if the address exists I delete allocated memory address and erase the integer address from address array.

if(_ptr != NULL)
{
	// Find if adress exists in array
	m_it = find(m_tAddressArray.begin(), m_tAddressArray.end(), _ptr );
	if(m_it != m_tAddressArray.end())
	{
		// If adress exists delete pointer
		delete [] _ptr;
		_ptr = NULL;
		// Delete adress from array
		m_tAddressArray.erase(m_it);
	}
}

3) Test:

#include "K3dSafePointer.h"

using namespace K3d;

int main(int argc, char *argv[])
{
	// Create safe pointer for integer and float
	K3dSafePointer<int> kSpInt;
	K3dSafePointer<float> kSpFloat;
	// Test pointers
	int *pInt = NULL;
	int *pInt2 = NULL;
	int *pInt3 = NULL;
	float *pFloat = NULL;
	float *pFloat2 = NULL;
	// Safe allocation equivalent to "pInt = new int"
	pInt = kSpInt.SafeNew();
	// Safe allocation of int array equivalent to "pInt3 = new int[5]"
	pInt3 = kSpInt.SafeNew(5);
	// Safe allocation equivalent to "pFloat = new float")
	pFloat = kSpFloat.SafeNew();
	// Safe allocation equivalent to "pFloat2 = new float[10]"
	pFloat2 = kSpFloat.SafeNew(10);
	// Test copy pInt to pInt2
	*pInt = 1;
	pInt2 = pInt;
	*pInt = 3;
	*pInt2 = 4;
	// Test int array
	pInt3[0] = 0;
	pInt3[2] = 2;
	pInt3[3] = 3;
	int iTest = pInt3[0];
	iTest = pInt3[2];
	iTest = pInt3[3];
	// Test pointer to float 
	*pFloat = (float) 2.22;
	*pFloat2 = (float) 3.33;
	// Safe delete equivalent to "delete pInt"
	pInt = kSpInt.SafeDelete(pInt);
	// Safe delete equivalent to "delete pInt2". 
	// The pInt2 is the same address like pInt, because I copy pInt to pInt2 (pInt2 = pInt;)
	// Without using safe pointer would be called error (SIGABR double free or corruption)
	pInt2 = kSpInt.SafeDelete(pInt2);
	// Safe delete equivalent to "delete pInt3[]". 
	pInt3 = kSpInt.SafeDelete(pInt3);
	// Safe delete equivalent to "delete pFloat"
	pFloat = kSpFloat.SafeDelete(pFloat);
	// Safe delete equivalent to "delete pFloat[]"
	pFloat2 = kSpFloat.SafeDelete(pFloat2);
	// Test delete already deleted pointer
	// Without using safe pointer would be called error (SIGABR double free or corruption)
	pFloat2 = kSpFloat.SafeDelete(pFloat2);
	return 0;
}




home / g++


Valid XHTML 1.0 Transitional