You just cheat and make the array global... fixed... via a namespace or just in the top of your MAIN (Namespace would span multiple source files though easy enough) and just work it like that.
I personally have a little class I wrote for dynamic arrays - but its single array MyArray->ar[?]
But I just make that array single array as long as one dimension, and have the values be not integers - but pointers to another instance of the same class for the other dimension. Sounds hardthan it is:
// Note the ar property of this JFC_AR class is defained as an INT
// so I type cast as a pointer because the datatype size is the
// same - that's how this works - but its EASY.
JFC_AR *Tiles = new JFC_AR();
Tiles->Size_Set(100);// HowManyDown (y)
int i;
for i=0;i<100;i++){
TY->ar[i]=(int)new JFC_AR();
((JFC_AR*)TY->ar[i])->Size_Set(100);//HowmanyAcross (x)
};
// Example to Set a value
((JFC_AR*)TY->ar[y])->ar[x]=MyNewValue;
//Example to Get a Value
MyInt=((JFC_AR*)TY->ar[y])->ar[x];
My Header file - jfc_ar.h
/*============================================================================
| _________ _______ _______ ______ _______ Jegas, LLC |
| /___ ___// _____/ / _____/ / __ / / _____/ JasonPSage@jegas.com |
| / / / /__ / / ___ / /_/ / / /____ |
| / / / ____/ / / / / / __ / /____ / |
|____/ / / /___ / /__/ / / / / / _____/ / |
/_____/ /______/ /______/ /_/ /_/ /______/ |
| Under the Hood |
==============================================================================
Copyright(c)2008 Jegas, LLC
============================================================================*/
#pragma once
#define cnAllocationSize 100
//============================================================================
class JFC_AR{
//=============================================================================
public:
JFC_AR();
~JFC_AR();
int *ar;
int Size_Get(void);//See pvt_Size for direct Access - Don't Modify - treat like readonly property!
bool Size_Set(int p_Size);
int Limit_Get(void);
bool Limit_Set(int p_Limit);
int AllocationSize_Get(void);
bool AllocationSize_Set(int p_AllocationSize);
bool Sort_Bubble(bool p_Ascending, bool p_SortEntireLimit);
bool Flatten(void);
bool AppendItem(void);
bool AppendItem(int p_i);
bool AppendItem(void* p_ptr);
bool InsertItem(int p_Position); // slow
bool DeleteItem(int p_Position);// Slow
bool DeleteAll(void);// Slow
int FoundItem(int p_Value);// Return -1 if not found, otherwise -
// returned value is the INDEX in the array (Zero Based)
int pvt_Size;//Recommended for READONLY! Use Size_Set(newsize) to Modify!!!!
int pvt_Limit;//Recommended for READONLY! Use Limit_Set(newlimit) to modify!!!!
int pvt_AllocationSize;//Recommended for READONLY! Use AllocationSize_Set(newaloocsize) to modify!!!!
};
//=============================================================================
//============================================================================
//============================================================================
//============================================================================
My jfc_ar.cpp file
/*============================================================================
| _________ _______ _______ ______ _______ Jegas, LLC |
| /___ ___// _____/ / _____/ / __ / / _____/ JasonPSage@jegas.com |
| / / / /__ / / ___ / /_/ / / /____ |
| / / / ____/ / / / / / __ / /____ / |
|____/ / / /___ / /__/ / / / / / _____/ / |
/_____/ /______/ /______/ /_/ /_/ /______/ |
| Under the Hood |
==============================================================================
Copyright(c)2008 Jegas, LLC
============================================================================*/
#include <memory.h>
#include "jfc_ar.h"
//============================================================================
// Begin JFC_AR
//=============================================================================
//----------------------------------------------------------------------------
JFC_AR::JFC_AR(){
//----------------------------------------------------------------------------
this->ar=0;
this->pvt_Size=0;
this->pvt_Limit=0;
this->pvt_AllocationSize=cnAllocationSize;
};
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
JFC_AR::~JFC_AR(){
//----------------------------------------------------------------------------
if(this->ar!=0){ delete [] this->ar; };
};
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
int JFC_AR::Size_Get(void){
//----------------------------------------------------------------------------
return this->pvt_Size;
};
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
bool JFC_AR::Size_Set(int p_Size){
//----------------------------------------------------------------------------
bool bResult=(p_Size>-1);
if(bResult){
if(p_Size==0){
this->Limit_Set(0);
}else{
if(p_Size>pvt_Limit){
int NewLimit = this->pvt_AllocationSize * (p_Size / this->pvt_AllocationSize);
if((NewLimit==0) || ((p_Size % this->pvt_AllocationSize)!=0)){
NewLimit+=this->pvt_AllocationSize;
};
this->Limit_Set(NewLimit);
}else{
// Shrinking?
if(this->pvt_Size>p_Size){
int i=0;
// TODO: Make this wiping done via memset function.
for(i=p_Size;i<this->pvt_Size;i++){
this->ar[i]=0;
};
};
};
};
this->pvt_Size=p_Size;
};
return bResult;
};
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
int JFC_AR::Limit_Get(void){
//----------------------------------------------------------------------------
return this->pvt_Limit;
};
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
bool JFC_AR::Limit_Set(int p_Limit){
//----------------------------------------------------------------------------
bool bResult=((p_Limit>-1) && ((p_Limit % this->pvt_AllocationSize)==0));
if(bResult){
if(p_Limit==0){
if(this->ar !=0){
delete [] this->ar;
};
this->ar=0;
this->pvt_Limit=0;
this->pvt_Size=0;
}else{
int *arnew = new int [p_Limit];
//memset(arnew, char(0), p_Limit * sizeof(int));
int i=0;
// TODO: Remove this for loop with memset function
for(i=0;i<p_Limit;i++){
arnew[i]=0;
};
if(this->ar !=0){
//memcpy(arnew, this->ar, this->pvt_Size * sizeof(int));
// TODO: implement memcpy routine
for(i=0;i<this->pvt_Size;i++){
arnew[i]=ar[i];
};
delete [] this->ar;
};
ar=arnew;
this->pvt_Limit = p_Limit;
};
};
return bResult;
};
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
int JFC_AR::AllocationSize_Get(void){
//----------------------------------------------------------------------------
return this->pvt_AllocationSize;
};
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
bool JFC_AR::AllocationSize_Set(int p_AllocationSize){
//----------------------------------------------------------------------------
bool bResult=(this->pvt_Size==0) && (this->pvt_Limit==0) && (p_AllocationSize>0);
if(bResult){
this->pvt_AllocationSize=p_AllocationSize;
};
return bResult;
};
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// Need At least two items for Sort to be successful.
bool JFC_AR::Sort_Bubble(bool p_Ascending, bool p_SortEntireLimit){
//----------------------------------------------------------------------------
int SortRange=0;
if(p_SortEntireLimit){
SortRange=this->pvt_Limit;
}else{
SortRange=this->pvt_Size;
};
bool bResult=(this->ar!=0);
if(bResult && (this->pvt_Size>1)){
int t; int s; int iTemp;
if(p_Ascending){
// Redundant to avoid constant if p_Ascending then this else that
for(s=0; s<SortRange; s++){
for(t=0; t<SortRange-1; t++){
if( (this->ar[t]) >
(this->ar[t+1])
){
iTemp=this->ar[t+1];
this->ar[t+1]=this->ar[t];
this->ar[t]=iTemp;
};
};
};
}else{
// REdundant to avoid constant if p_Ascending then this else that
for(s=0; s<SortRange; s++){
for(t=0; t<SortRange-1; t++){
if( (this->ar[t]) <
(this->ar[t+1])
){
iTemp=this->ar[t+1];
this->ar[t+1]=this->ar[t];
this->ar[t]=iTemp;
};
};
};
};
};
return bResult;
};
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
bool JFC_AR::Flatten(void){
//----------------------------------------------------------------------------
bool bResult=(this->pvt_Limit>0);
if(bResult){
int t; int s; int iTemp;
if(this->pvt_Limit>1){ // Then sort Descending - kinda - Put ZERO's at end.
for(s=0; s<pvt_Limit; s++){
for(t=0; t<pvt_Limit-1; t++){
if( ((this->ar[t]) < (this->ar[t+1])) &&
(this->ar[t]==0)
){
iTemp=this->ar[t+1];
this->ar[t+1]=this->ar[t];
this->ar[t]=iTemp;
};
};
};
};
int p_iNewSize=-1;
while(this->ar[++p_iNewSize]!=0);
this->Size_Set(p_iNewSize);
};
return bResult;
};
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
bool JFC_AR::AppendItem(void){
//----------------------------------------------------------------------------
bool bResult=this->Size_Set(this->pvt_Size+1);
return bResult;
};
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
bool JFC_AR::AppendItem(int p_i){
//----------------------------------------------------------------------------
bool bResult = this->AppendItem();
if(bResult){
this->ar[this->pvt_Size-1] = p_i;
};
return bResult;
};
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
bool JFC_AR::AppendItem(void* p_ptr){
//----------------------------------------------------------------------------
bool bResult = this->AppendItem();
if(bResult){
this->ar[this->pvt_Size-1] = (int)p_ptr;
};
return bResult;
};
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
bool JFC_AR::InsertItem(int p_Position){
//----------------------------------------------------------------------------
bool bResult=(p_Position>-1) && (p_Position<this->pvt_Size);
if(bResult){
if(p_Position==(this->pvt_Size-1)){
bResult=this->AppendItem();
}else{
bResult=this->Size_Set(this->pvt_Size+1);
if(bResult){
int i=0;
for(i=this->pvt_Size-1;i>p_Position;i--){
ar[i]=ar[i-1];
};
ar[p_Position]=0;// New Position Starts off with a zero.
};
};
};
return bResult;
};
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
bool JFC_AR::DeleteItem(int p_Position){//Slow
//----------------------------------------------------------------------------
bool bResult=(p_Position>-1) && (p_Position<this->pvt_Size);
if(bResult){
int i=0;
for(i=p_Position;i<this->pvt_Size-1;i++){
ar[i]=ar[i+1];
};
bResult=this->Size_Set(this->pvt_Size-1);
};
return bResult;
};
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
bool JFC_AR::DeleteAll(void){
//----------------------------------------------------------------------------
bool bResult=(this->Limit_Set(0));
return bResult;
};
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
int JFC_AR::FoundItem(int p_Value){// Return -1 if not found, otherwise -
//----------------------------------------------------------------------------
int iResult=-1;
if(this->Size_Get()>0){
int i=-1;
do{
i++;
}while((i<this->Size_Get()) && (p_Value != this->ar[i]));
if(p_Value == this->ar[i]){
iResult=i;
};
};
return iResult;
};
//----------------------------------------------------------------------------
//============================================================================
// End JFC_AR
//=============================================================================
Best of luck to ya