dynamic creation of arrays of pointers to arrays of pointers

I'm trying to write a function that takes a 32bit address and a data to store at this address.

I'm watning to take the 32 bit memory address eg 0x12345678 and split it
into 4 x 2 bytes
12, 34, 56, 78

then each of the 4 entries is at most a 256 entry array.eg
FF, FF, FF, FF

So in this example, 0x12 points to 0x34 in the second array, which
points to 0x56 in the third array, which finally points to 0x78 in the
last array.
This last array holds the actual data.

After successfully doing 0x12345678, say I might get a read for
0x1234AABB. So, the first and second pointers already exist, but I then have to create and write to dynamically created arrays.
The arrays need to have all entries set to NULL so that i know whether to follow the pointers to overwrite a previously entered value or create new arrays and pointers.


It all looks good and simple in the pseudo code I've written up but I'm having trouble coding it.
I'm currently trying to deal with the first entry case, ie all array elements are NULL, but I'm getting confused with the pointers and creation of new arrays.

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
void cpu::store(unsigned int mem_add,unsigned int mem_val)
{
	int first = (mem_address&4278190080)>>24;
	int second = (mem_address&16711680)>>16;
	int third = (mem_address&65280)>>8;
	int fourth= (mem_address&255);
		
	if (A1[first]==NULL)
	{
	
		int** A2=new int*[256];
		for (i=0;i<256;i++)
		{
			A2[i]=NULL;
		}
		
		int** A3=new int*[256];
		for  (i=0;i<256;i++)
		{
			A3[i]=NULL;
		}
		unsigned int* A4=new unsigned int[256];
		for  (i=0;i<256;i++)
		{
			A4[i]=0;
		}
		A1[first]=A2;
		A2[second]=A3;
		A3[third]=A4;
		A4[fourth]=mem_val;
		
	}

}


A1 has been declared as
int* A1[256] ;


Any tips on where I'm going wrong?

I've played with it a bit now and got it compiling, However, the last 4 lines of code are not storing the correct values.
code is now:

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
void cpu::store(unsigned int mem_add,unsigned int mem_val)
{
	int first = (mem_add&4278190080)>>24;
	int second = (mem_add&16711680)>>16;
	int third = (mem_add&65280)>>8;
	int fourth= (mem_add&255);
		
	if (A1[first]==NULL)
	{
		cout<<"here"<<endl;
		unsigned int** A2=new unsigned int*[256];

		for (i=0;i<256;i++)
		{
			A2[i]=NULL;
			
		}
		
		unsigned int** A3=new unsigned int*[256];
		for  (i=0;i<256;i++)
		{
			A3[i]=NULL;
			
		}
		unsigned int* A4=new unsigned int[256];
		for  (i=0;i<256;i++)
		{
			A4[i]=0;
			
		}
		A1[first]=*A2;
		A2[second]=*A3;
		A3[third]=A4;
		A4[fourth]=mem_val;
	}
This whole idea is very dangerous. Why don't you let the OS provide you with the address to store your data? You have a high probablity of overwriting memory that you don't want to overwrite resulting in an un-stable computer and potential crashing of your program or the OS. No one does this sort of recless programming for a very good reason; its dangerous. Just use the new operator as its designed to be used and be happy with it. Or are you attempting to inject data into a program that's already running or even the OS, doesn't sond very legitimate!

Mind you your name may give a clue to your intentions!!!
Last edited on
What I'm trying to do is create a Instruction set simulator for a subset of machine language instructions. I'm to read in from a file, text such as 'm fffffffc = ffc234ab' and i'm required to store and retrieve the data value on the right from the address on the left. The data represents the command in hex. the addresses could potentially range from 0x00000000 to 0xffffffff.
I'm not sure how I'm misusing the new operator? I wouldn't have thought that dynamically created arrays would overwrite any data they weren't meant too, I'm staying with ni their bounds.
As for the name, that's something I've carried for a long time before I was into programming.
Topic archived. No new replies allowed.