views:

87

answers:

4

I'm doing a project where I struck against this situation:

typedef unsigned char Page[16384];
unsigned char Memory[16384*64]={...values...};
void foo(Page* page);

Now as you can see Memory is composed of "Pages", now I would like to pass a Page to a function but a Page should be a POINTER to values of Memory (so indexes of a Page should be pointers to values of Memory). What I mean should be understand watching this:

Page tmp;
Memory[16400]=50;
tmp=(unsigned char[16384])&Memory[16384];//This is not possible but is what I would like to do
printf("Value should be 50: %d\n",tmp[15]);

Any suggestion?

Update 1: I need that Memory is a big array and not array of arrays (while actually they are the same if I'm not wrong), it's a project requirement (for university).

Update 2: Obviusly is a sample code, I used a macro for 16384 (and also for 16384*64), sizes can't change.

Update 3: I would like to preserve Page as unsigned char[16384] because I would like to use it to pass values that should be stored (so they aren't stored anywhere) but I think it's impossible in this way so I'll follow Tyler McHenry suggestion

Update 4: As Tyler McHenry stated, Update 3 doesn't make sense, so I'm explaining what I mean: I would like to use Page to pass values to functions that must store those values to a "swap file" (is a system memory simulator), so actually there are some cases where a Page will not be a reference to anything (just an array of values), but the problem in this way is that a page should be a pointer to part of Memory OR just a simple array of 16384 bytes, so 1 type for 2 things is a problem.

An example could be this:

typedef char Page[16384];
void WritePageToSwapFile(char* swapfilePath,Page* page);
...somewhere in a function...
Page myNewPage;
myNewPage[5]=23;
WritePageToSwapFile("swapfile",&myNewPage);
...somewhere else, in another function...
Page tmp;
Memory[16400]=50;
tmp=(unsigned char[16384])&Memory[16384];//This is not possible but is what I would like to do
WritePageToSwapFile("swapfile",&tmp);

I don't think I can do both things with the same type

+4  A: 

Just have Page be a unsigned char* instead of an unsigned char[16384], and you will have exactly what you want. The only caveat is that you will have to remember externally (e.g. using a constant PAGE_SIZE or something) how large a page is, since you will not be able to use sizeof(Page) to get this information.

e.g. (also making use of the PAGE_SIZE constant to eliminate the magic number 16384 sprinkled throughout your code...)

/* In C, this has to be a define, since I'm going to use it as part of an
 * array size specifier. In C++ it could be a constant integer */
#define PAGE_SIZE 16384

typedef unsigned char* Page;
unsigned char Memory[PAGE_SIZE*64]={...values...};

Page tmp;
Memory[PAGE_SIZE+15]=50;
tmp=&Memory[PAGE_SIZE];
printf("Value should be 50: %d\n", tmp[15]); // Prints 50

/* Print out all of the values in page "tmp" */
for (int i = 0; i < PAGE_SIZE; ++i) {
  printf("%d\n", tmp[i]);
}
Tyler McHenry
On some architectures/compilers, this will bloat the size of the array beyond what is desired.
San Jacinto
@San Jacinto Uh, explain how? The only array in question is `Memory`, and I'm not suggesting that he change that. The `Page` variables are simply aliases into the `Memory` array.
Tyler McHenry
Nope, nope. You were right. my mistake. This is the 2nd time in 10 minutes I read a question WAY too fast. Carry on.
San Jacinto
if the OP wants to select a byte at offset `o` in the page `n`, it is enough to do `Memory[n*PAGESIZE + o]`; of course 0 is the first page.
ShinTakezou
+4  A: 

Arrays as function parameters are equivalent to pointers. Just pass offset pointer like Memory + 16384 to the function.

Nikolai N Fetissov
+3  A: 

You can declare array of pages:

Page Memory[64] = {{first page}, {2nd page} ... };

Still, this array will be isomorphic to flat-array.

el.pescado
+1  A: 

If you change the type of tmp to Page *, you can do this:

Page *tmp;
Memory[16400]=50;
tmp = (Page *)&Memory[16384];
WritePageToSwapFile("swapfile", tmp);

...is that what you want?

caf
That's interesting, going to test
Fire-Dragon-DoL
Here we go! That's the right syntax! thanks a lot... unfortunately it's too late, but I'm happy to know this, hopefully I can use this again
Fire-Dragon-DoL