Pointer help

closed account (E079216C)
Hi...

I'm trying doing a exercise. This (strange) exercise's called "A pointer data manager class". also called "Unusual exercise" in my opinion.
Generally speaking, this exercise requires the output class must have a feature that everyone can access a pointer in many different methods. It's awful! I've never seen this kind of exercise before.

My teacher gave no hints, only try the (*void) pointer.

I feel this exercise is too hard. But with the help of my friends, this class is almost done but maybe there are some remaining errors still appear.

This is the class :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class tinyDllPointerManagerArray{public :
tinyDllPointerManagerArray(unsigned int _nSize){Allocate(_nSize);}
~tinyDllPointerManagerArray(){Clear();}

bool Allocate(unsigned int _nSize){if(!_nSize)return false;
nSize = _nSize;data = new void*[nSize];ClearPos();return true;}

inline tinyDllPointerManagerArray * ClearPos(){for(int i = 0;i < 10;i++)nLevel[i]=0;nCurPos = 0;return this;}
inline tinyDllPointerManagerArray * Clear(){if(nSize)delete data;return this;}

inline void *Get(bool bClearPosition = false){int nPosition = GetIndex();
if(bClearPosition)ClearPos();return data[nPosition];}

inline int GetSize(){return nSize;}

int GetIndex(bool bCheck = true){if(!nSize || !nCurPos)return -1;
int nPosition = nLevel[0];int nGrow = 10; // Layered
for(int i = 1; i < nCurPos;i++){
nPosition+= nLevel[i] * nGrow;nGrow *= 10;}
if(nPosition >= nSize && bCheck)return -1; //Invalid Index value
return nPosition;}

tinyDllPointerManagerArray *Pos(unsigned int nPos){if(nSize && nCurPos < 10){
nLevel[nCurPos] = nPos;
if(GetIndex() == -1)nLevel[nCurPos];else nCurPos++;}
return this;}

inline int operator = (int nPosition) {Pos(nPosition);return nPosition;}

void **data;
private :
unsigned int nLevel[10];
unsigned int nSize; //Size of data
unsigned int nCurPos; // Current level
};

Usage :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main(){int nPos;
tinyDllPointerManagerArray ptMgr = tinyDllPointerManagerArray(10000); //10000 units
char *strEx = "Hello World !!!!!!";
int nPos = ptMgr.Pos(50)->Pos(25)->GetIndex(false);
//Position : 50 + 10 * 25 = 300 (Valid)
ptMgr.data[nPos] = strEx;

nPos = ptMgr.Pos(100)->Pos(3)->GetIndex(true);
//Position : 100 + 3 * 10 = 300 (Valid)
ptMgr.data[nPos] = strEx;

nPos = ptMgr.Pos(12)->Pos(10)->Pos(40)->Pos(80)->GetIndex(false);
//Position : 12 + 10 * 10 + 40 * 100 + 80 * 1000 = 84112 // Not valid
ptMgr.data[nPos] = strEx; //Crash !!!
printf("Successful !!!");
return 0;
}


In my point, It's still too complex.
Does anyone have any suggestion about this code?
Help !!!!!!!!!!!!!!!!!!!!!
Last edited on
This class needs some of improvements.

nLevel[nCurPos];
It does nothing. Redundant. Or you forgot something?
Name's too long.
You should have a variable which stores index instead of an array. It seems too complex.
Why did you define an operator? It also may be redundant.
Edit :
Example for Pos() (reference)
1
2
3
4
5
6
//Public : nIndex
nPos([...]){// Check...
int nGrow = 10;
for (i=0;i<nCurPos;++i )nGrow*= 10;
nIndex += nPos * nGrow;
}


Hope all goes well.
Last edited on
closed account (E079216C)
@Jackson Marie (26)
Thanks you.
But, I don't understand your algorithm. Can you explain more?
Topic archived. No new replies allowed.