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.
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:
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.
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.
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)
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
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.
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.