You are not logged in.

#1 2013-04-19 06:39:34

milo64
Banned
From: Chiang Mai, Thailand
Registered: 2013-01-18
Posts: 86
Website

Is it possible to make a 2D array (or whatever-dimension) array like..

Is it possible to make a 2D array (or whatever-dimension) array like this...

        collumn 1                   collumn 2
Emulated Address   Real Memory address*
           (int)                            (int *)
+----------------------+----------------------------+
|            0                |              0xA0               | <-- row 1
|            1                |              0xA1               | <-- row 2
|            2                |              0xA2               | <-- row 3
+----------------------+----------------------------+

* A = Address.

is it possible to make an array like that?
if it is, please tell me how to do it...

thanks.

... I'm trying to make an emulator to emulate the simplest memory.


milo64.
Registered Linux User: #555436
My Blog @ http://milo64.blogspot.com/

Offline

#2 2013-04-19 10:03:28

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,332
Website

Re: Is it possible to make a 2D array (or whatever-dimension) array like..

Given your other posts, I'm assuming you mean in C, right?

If so, the answer is yes, but specifically how will depend on a needed clarification of your question.  What you present doesn't really need to be a 2 dimensional array, just one: that looks like a simple list of memory addresses, right?

At the simplest you can declare an array with two dimensions `iny myarray[2][3];` but to make the table you put up there you'd only need `int *myarray[3];`

If you also wanted space allocated somewhere that each element of that list pointed to, you could allocate them separately:

int *myarray[3];
myarray[0] = (int *) malloc(sizeof(int));
myarray[1] = (int *) malloc(sizeof(int));
myarray[2] = (int *) malloc(sizeof(int));

Obviously with many entries this should be in a loop.  Perhaps not as obviously, why would you not just malloc a larger block of memory to start with?

What is the end goal?

EDIT: actually, upon rereading your question, the mallocs are probably irrelevant.  `int *myarray[3]` will get you the array you are looking for.  Just realize that until you point those pointers to memory you 'own' you shouldn't assign to or read from them.

Last edited by Trilby (2013-04-19 10:06:31)


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Online

#3 2013-04-19 11:36:41

Trent
Member
From: Baltimore, MD (US)
Registered: 2009-04-16
Posts: 990

Re: Is it possible to make a 2D array (or whatever-dimension) array like..

If you're trying to imitate memory, I'd probably just use an array of unsigned char. That's essentially what memory is anyway. Use "regular" unsigned instead if you want to speed up int-sized operations at the expense of char-sized ones (but you'll have to scale the "addresses" by sizeof(int) if you still want to address individual chars within the array).

I hereby vow to stop posting before breakfast.

Last edited by Trent (2013-04-19 13:50:19)

Offline

#4 2013-04-19 11:49:31

milo64
Banned
From: Chiang Mai, Thailand
Registered: 2013-01-18
Posts: 86
Website

Re: Is it possible to make a 2D array (or whatever-dimension) array like..

okay, SOLVED thank you...


milo64.
Registered Linux User: #555436
My Blog @ http://milo64.blogspot.com/

Offline

Board footer

Powered by FluxBB