Page 1 of 1

C-variable's initial address

Posted: Tue Jun 06, 2006 1:38 pm
by OSMAN
Is there any way of declaring the address of a becoming table or a dynamic-sized variable? Like as I would write:

Code: Select all

unsigned int *pagetables[i][1024] = (unsigned int*)  0x20e000;
Wont work, but:
The point is, I want to create this in a loop, but the compiler wont accept it. So, how can I declare that address ? ( Please don't tell me C lacks this feature [Does it?] )

Re:C-variable's initial address

Posted: Tue Jun 06, 2006 2:18 pm
by Candy
OSMAN wrote: Is there any way of declaring the address of a becoming table or a dynamic-sized variable? Like as I would write:

Code: Select all

unsigned int *pagetables[i][1024] = (unsigned int*)  0x20e000;
You're trying to allocate an array that already exists, now as an array of uint *'s, a two-dimensional at that, and you're assigning a single address to it in the wrong format.

Code: Select all

unsigned int *pagetables;

int pagetable_create() {
   pagetables = (unsigned int *)page_alloc();
   for (int i=0; i<1024; i++) {
      pagetables[i] = (unsigned int)page_alloc();
   }
}

unsigned int page_alloc() {
   static int cpage = 0;
   cpage += 1024;
   return cpage;
}

Re:C-variable's initial address

Posted: Wed Jun 07, 2006 12:12 am
by Solar
As this is in the "General Programming" thread: Beware, what those two are fiddling with here is kernel-space code, i.e. it's more or less legal for them to work with memory that hasn't been malloc()ed before.

More or less. 8)

Re:C-variable's initial address

Posted: Sat Jun 10, 2006 11:50 pm
by Neo
I think the "placement new" is what you are looking for. However this is available in C++ (but the logic could be used in C).
Googling for "placement new" should get you a lot of info on this.

Re:C-variable's initial address

Posted: Thu Jun 15, 2006 9:54 am
by Pype.Clicker
alternatively, declare your array as being "extern" and enforce its location by a symbol assignment in a linker script (e.g. the linker will place the stuff where you want it to be, and the compiler will keep using perfectly clean code).

That may make sense in embedded systems, somehow ...