Array access at fixed memory address

I am developing some code that uses a two dimensional array as well as an array of structs. It was no problem to set it up in static memory of the system's choosing. Now I must move them to a specific physical address to use non-volatile ram. Is there a set of standard idioms for accessing muli-dimentioned arrays and arrays of structs tied to a hard address? Perhaps it is similar to accessing structures in malloc'ed memory.

Thanks,

gtb

Reply to
goodTweetieBird
Loading thread data ...

Are you programming in C? If so, you'll find that how to do this is compiler dependent. As I remember it though, in C, two dimensional arrays are implemented as follows:

Memory e.g. int twoDArray[2][3] > +-----------------+ | twoDArray[0][0] | (e.g. @ 0x0000) +-----------------+ | twoDArray[0][1] | (0x0000 + sizeof(int)) +-----------------+ | twoDArray[0][2] | etc +-----------------+ | twoDArray[1][0] | +-----------------+ | twoDArray[1][1] | +-----------------+ | twoDArray[1][2] | +-----------------+

Don't take my word for it, just read your compiler's manual (it's been a while since I've used C at that level, so my memory may be not quite right). This should also specify things like alignment and so on; e.g. if your array uses short values of 16 bits but the machine size is 32 bits then you need to consider whether array elements will be aligned on 32 bit boundaries or whether they will be packed.

If it's not C, then the answer will be different. For example I believe that FORTRAN does this the other way round, and you would use the standard representation mechanisms in Ada if you were using that.

Good luck.

Reply to
John McCabe

You would access them (at least if the memory is ordinarily addressable) the same way, regardless if the base address was chosen by the compiler or assigned by you.

There are two ways to approach the problem of getting the proper value into the base pointer. One way is to use compiler- specific directives to tell it the address of the array when you declare the array. This may also involve using linker memory map files. The other way is to take a literal value of where you know it will be, and typecast that into a pointer.

The latter method is indeed quite a bit like using malloc, in fact you could even write your own malloc-like function that returns your pre-chosen address as a pointer type. This could be useful to do, because if you want to be able to build the same code on a PC-type platform for testing, you can have it call malloc instead to get a block of generic memory.

Reply to
cs_posting

I should have stated that it was in ANSI C.

Thank you both.

gtb

Reply to
goodTweetieBird

You will need to setup a seperate section for it so it doesn't get zeroed at startup, look at your linker docs.

Reply to
cbarn24050

True, if the toolchain even knows that there's ram there.

If using the method of initializing the pointer by hand outside of the area the toolchain knows about, that's not an issue.

(Provided of course we aren't talking about a system with an MMU or protection mechanism that will object to your accessing arbitrary memory addresses)

Reply to
cs_posting

There is no universal way. The implementation of the multi dimensional arrays depends on the compiler. For that matter, Stroustrup recommends not using multidimensional arrays or making your own arrays by hand.

Vladimir Vassilevsky DSP and Mixed Signal Design Consultant

formatting link

Reply to
Vladimir Vassilevsky

The size if the array elements depends somewhat on the compiler. The layout of those elements in memory does not.

Robert

** Posted from
formatting link
**
Reply to
Robert Adsett

How about this:

u8 array[3][] = { "fubar", "bla-bla-bla", "1234567890" };

Vladimir Vassilevsky DSP and Mixed Signal Design Consultant

formatting link

Reply to
Vladimir Vassilevsky

If all you want to do is read/write at fixed addresses, instead of doing this:

int a[10][3]; struct s b[10];

try this:

int (*a)[3] =3D (void*)A_NVRAM_ADDRESS; struct s *b =3D (void*)B_NVRAM_ADDRESS;

That will allow you to use indexes to read and write a and b. If you're using the sizeof operator or doing anything else that doesn't treat arrays as pointers, the above won't work.

It's probably better to configure your linking, though.

Reply to
Dingo

That's simply an invalid declaration. What did you mean by this?

--
Best regards,
Andrey Tarasevich
Reply to
Andrey Tarasevich

What about it? GCC refers to it as an incomplete type, PC-Lint uses the term vacuous. Either way I don't think it is valid.

Robert

** Posted from
formatting link
**
Reply to
Robert Adsett

Thanks!

Trying to dissect or put the LH side of the above into words.

Would you say " 'a' is the first of a group of pointers to arrays of

3 ints"?

It seems this declaration would make pointer arithmetic operate properly.

gtb

Reply to
goodTweetieBird

There's a program called 'cdecl' that will do that for you (and the other way around as well).

cdecl> explain int (*a)[3] declare a as pointer to array 3 of int

--
Stef    (remove caps, dashes and .invalid from e-mail address to reply by mail)

     "Congratulations. You're the first human to fail the turing test."
Reply to
Stef

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.