开发者

C Fixed array pointing to parts of an array

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 c开发者_如何转开发har[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


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


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]);
}


You can declare array of pages:

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

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


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜