Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 13th, 2006, 7:01 PM   #1
darthsabbath
Newbie
 
Join Date: Dec 2005
Posts: 15
Rep Power: 0 darthsabbath is on a distinguished road
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!
darthsabbath is offline   Reply With Quote
Old Sep 13th, 2006, 7:28 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Sep 13th, 2006, 7:48 PM   #3
darthsabbath
Newbie
 
Join Date: Dec 2005
Posts: 15
Rep Power: 0 darthsabbath is on a distinguished road
Awesome, thanks for clarifying DaWei.
darthsabbath is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:31 AM.

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