C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 24th, 2012, 09:20 AM
miz656 miz656 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 17 miz656 New User: is a brand new recruit and a unknown entity at this point. 
Time spent in forums: 3 h 31 m 51 sec
Reputation Power: 0
Reference operator, &

I a few questions.

1. When you use this operator, is it just a way for the compiler to find out where the variable is?

example

int var = 5;
printf("%d",&var);

to access the variable, the compiler needs to knwo where it's at. So is using the & operator do so?

2. How does the compiler assign what its memory address is? Does it do it random or in a certain sequence?

Learning pointers right now, seems confusing. Answering these questions may make it easier to understand. So, thanks again!

Reply With Quote
  #2  
Old December 24th, 2012, 11:39 AM
zedeneye1 zedeneye1 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 52 zedeneye1 User rank is Lance Corporal (50 - 100 Reputation Level)zedeneye1 User rank is Lance Corporal (50 - 100 Reputation Level)zedeneye1 User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 14 h 36 m 53 sec
Reputation Power: 1
compiler doesn't assign anything, the processor does...

Reply With Quote
  #3  
Old December 24th, 2012, 01:54 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is online now
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,127 dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 18 h 16 sec
Reputation Power: 1949
All data is physically stored in memory. Memory is organized as a contiguous array of bytes -- well, words actually, with different computer systems potentially having their own proprietary word lengths, but the common convention now is to use bytes which are 8 bits in length -- , each located at its own unique memory address. A memory address is an integer value that can be loaded into a Memory Address Register which the hardware can then use to access that particular memory location.

A pointer is simply a variable that contains a memory address. That's all that a pointer is. The rest is just learning the syntax of working with pointers. In C++, a reference is simply working with pointers without the pointer syntax.

However, from your example, you're asking about the address operator, which I guess some books might call a "reference operator". All it does is to return the address of the variable.

Quote:
1. When you use this operator, is it just a way for the compiler to find out where the variable is?

example

int var = 5;
printf("%d",&var);

to access the variable, the compiler needs to knwo where it's at. So is using the & operator do so?

So then you're asking how to display the variable's address? You're close, but the flag for a memory address is "%p", which displays it in hexadecimal; memory addresses are much more meaningful in hex. And, yes, you need to use the address operator to get the address, otherwise you would display the value that's stored at that variable location.

Quote:
2. How does the compiler assign what its memory address is? Does it do it random or in a certain sequence?

Basically, as the compiler analyzes the code, it builds tables in which to hold information about the identifiers that it finds. Then at a particular stage of the process, as it generates code, it sets up segments to hold variables and code, etc, such as TEXT for code and literals, DATA for static variables, the HEAP, and the STACK -- different compilers can use different names. Static variables (AKA "global variables") go into a segment of read/write memory, such that they are placed one after the other, though possibly with some padding between them in order to optimize accessing them, with each location's address being its offset from the start of that memory segment; at compile time, the compiler has no idea where in memory the program will end up being loaded, so all addresses are offsets relative to some starting point. Auto variables (AKA "local variables") are set up in the code as offsets from a reference point in the function's stack frame so that they can be created when that function is called.

Now actually, when you compile a source file, you generate an object file that contains the object code and tables of what global variables it declared and what global variables and functions it expects to find in another object file. It is the linker that then reads all those tables and strings the object codes together and builds the global variable memory segment and the global initialization segment and generates the executable file. But even now, no actual memory addresses have been allocated or assigned; nobody can possibly know exactly where in memory any of the program's variables will reside.

Now you run the program. The OS' loader program reads the executable file's header for how much memory it needs (the linker had generated that header) and requests a block of memory of that size from the OS. All the addresses, both in the code and data, had been resolved as offsets from a starting memory address. That block of memory that the loader requested has a starting address, so now all the addresses in the executable can be fixed. Now we know the addresses. But they could be different each time we run the program, because subsequent executions can have different starting addresses.

But your example evokes an additional wrinkle. The loader fixes the addresses of the global variables, but not of the local variables; it cannot know where they will be because they are created and destroyed dynamically on the stack as you call and return from the functions that contain those local variables. You can play around with that by outputting a function's local variable addresses and then call it repeatedly from different functions and after having passed through differing numbers of functions (eg, the function is func, so call func from main, then main->func1->func, then main->func2->func1->func, etc).

Also, even though the compiler will assign offsets to the variables pretty much in the order in which they appear in the code, it might create them in the reverse order of what you might expect. Play around with it and see for yourself.

I really simplified that down for you. Hope it made sense and helped to answer your questions.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Reference operator, &

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap