Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C (http://www.programmingforums.org/forum60.html)
-   -   Compiletime addressing? (http://www.programmingforums.org/showthread.php?t=11307)

darthsabbath Sep 13th, 2006 8:01 PM

Compiletime addressing?
 
From reading about C, I've discovered something that's confusing the heck out of me. The book I'm reading states that addresses are allocated to variables by the compiler, so they're known at compiletime.

Maybe I'm missing something, but this puzzles me. Does the compiler actually hardcode the address of each symbol into the code... i.e., it will load at address 12345 on every machine? I don't see how this is possible... the only way I can resolve this in my head is that the addresses are coded as an offeset... i.e., the program is loaded by the OS into free memory, and the variables are then loaded at an offset from the beginning of the free memory.

Sorry if this is a simple question, but I can't find a satisfactory answer. :-)

Thanks!

DaWei Sep 13th, 2006 8:28 PM

Generally speaking, for the average desktop system, memory is managed. The executive will allocate the memory for the app and work with it as offsets, as you suggest. If you load and debug two programs at once, for instance, you may note that they have the 'same' memory. Less extensively controlled systems, such as some embedded systems, place the programs (often in ROM) at fixed memory addresses. This is done by following the link operation (which often deals with relative addresses) with a locate operation, which places things precisely where one wants them. This is a very loose generalization with many exceptions, but I think it probably answers your question. One COULD build a simple compiler that did everything in one swell foop.

darthsabbath Sep 13th, 2006 8:48 PM

Awesome, thanks for clarifying DaWei.


All times are GMT -5. The time now is 1:07 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC