C-variable's initial address

Programming, for all ages and all languages.
Post Reply
OSMAN

C-variable's initial address

Post 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?] )
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:C-variable's initial address

Post 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;
}
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:C-variable's initial address

Post 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)
Every good solution is obvious once you've found it.
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

Re:C-variable's initial address

Post 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.
Only Human
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:C-variable's initial address

Post 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 ...
Post Reply