LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-03-2010, 04:29 PM   #1
theKbStockpiler
Member
 
Registered: Sep 2009
Location: Central New York
Distribution: RPM Distros,Mostly Mandrake Forks;Drake Tools/Utilities all the way!GO MAGEIA!!!
Posts: 986

Rep: Reputation: 53
Info on Pointers and Variables Requested


I'm reading a tutorial and it seems like the author is saying that unless a variable goes through an addressing scheme it is actually a constant as far as the compiler is concerned. Is that what they are implying?


"Now, let's delve a little further into the difference between the names ptr and my_array
as used above. Some writers will refer to an array's name as a constant pointer. What do
we mean by that? Well, to understand the term "constant" in this sense, let's go back to
our definition of the term "variable". When we declare a variable we set aside a spot in
memory to hold the value of the appropriate type. Once that is done the name of the
variable can be interpreted in one of two ways. When used on the left side of the
assignment operator, the compiler interprets it as the memory location to which to move
that value resulting from evaluation of the right side of the assignment operator. But,
when used on the right side of the assignment operator, the name of a variable is
interpreted to mean the contents stored at that memory address set aside to hold the value
of that variable.
With that in mind, let's now consider the simplest of constants, as in:
int i, k;
i = 2;
Here, while i is a variable and then occupies space in the data portion of memory, 2 is a
constant and, as such, instead of setting aside memory in the data segment, it is imbedded
directly in the code segment of memory. That is, while writing something like k = i; tells
the compiler to create code which at run time will look at memory location &i to
determine the value to be moved to k, code created by i = 2; simply puts the 2 in the code
and there is no referencing of the data segment. That is, both k and i are objects, but 2 is
not an object.
Similarly, in the above, since my_array is a constant, once the compiler establishes
where the array itself is to be stored, it "knows" the address of my_array[0] and on
seeing:
ptr = my_array;
it simply uses this address as a constant in the code segment and there is no referencing
of the data segment beyond that." It's from page twelve and here is the link if more context is needed..


Thanks is advancehttp://docs.google.com/viewer?a=v&q=...AEIKFzsO2HojPQ

Last edited by theKbStockpiler; 05-03-2010 at 04:31 PM. Reason: where the hell is my link?
 
Old 05-03-2010, 04:41 PM   #2
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 139Reputation: 139
Quote:
Originally Posted by theKbStockpiler View Post
I'm reading a tutorial and it seems like the author is saying that unless a variable goes through an addressing scheme it is actually a constant as far as the compiler is concerned. Is that what they are implying?
What do you mean by "goes through an addressing scheme"?

The gist of what the author's saying in this point is that the address of any variable is both constant and known at compile time. You can use pointers to refer to dynamically allocated space, but any variable you directly declare in your source (including a pointer that points to any dynamically allocated space) has a constant address.
 
Old 05-03-2010, 04:46 PM   #3
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by JohnGraham View Post
What do you mean by "goes through an addressing scheme"?

The gist of what the author's saying in this point is that the address of any variable is both constant and known at compile time. You can use pointers to refer to dynamically allocated space, but any variable you directly declare in your source (including a pointer that points to any dynamically allocated space) has a constant address.

I would use 'offset' rather than 'address'.
 
Old 05-03-2010, 04:51 PM   #4
wpeckham
LQ Guru
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS, Manjaro
Posts: 5,907

Rep: Reputation: 2816Reputation: 2816Reputation: 2816Reputation: 2816Reputation: 2816Reputation: 2816Reputation: 2816Reputation: 2816Reputation: 2816Reputation: 2816Reputation: 2816
Not really. Let me see if I can help, or perhaps confuse you to death.

The point he is trying to make is the difference between the address of a variable and the value placed at that location through assignment. Perhaps leading to the difference between static and dynamic variables, array variables, and indirection.

Once defined, the address of a variable should never change, thus it is a constant. Hard coded values are constants, though in a slightly different sense. The value in the location may change, thus the value of a variable may change although the location the variable points to never does.

Actually things in the real world are a bit more complex as it is certainly possible to redirect a variable to an alternate location, redirect a pointer, or manipulate the machine in other ways at runtime.

What he should be doing is providing illustration and example. The concepts are not that complex, but the language we are using to describe them is ill suited.

Simple case: Consider a variable a bucket, and call it X. There are places where the compiler will see X and refer to the bucket ( X = 1 drops a 1 in the bucket). In another context the compiler will NOT see the bucket when it sees X, but will see the CONTENTS of the bucket ( Y = X drops a 1 in this second bucket named Y. It does NOT drop one bucket in the other, just clones the contents. Man, I wish that worked with beer!).

Now the bucket never really changed. X is still X. The view (bucket or contents of bucket) that the compiler used when you gave it X as a reference changed to perform the function that you intended by passing it the reference. The means you used to pass your intent is called syntax.

For a compiler builder or designer these are critical concepts and differences. They are also critical concepts when you discuss pointers and indirection. For other programmers this is making life WAY more difficult than it needs to be.

-------------
Re: address or offset: Either MIGHT be correct, depending upon the system and compiler, but address IS correct in any case. An address may be base and offset (and base has different meanings on different machines) or it may be an absolute address.

Last edited by wpeckham; 05-03-2010 at 04:59 PM.
 
Old 05-03-2010, 06:29 PM   #5
theKbStockpiler
Member
 
Registered: Sep 2009
Location: Central New York
Distribution: RPM Distros,Mostly Mandrake Forks;Drake Tools/Utilities all the way!GO MAGEIA!!!
Posts: 986

Original Poster
Rep: Reputation: 53
Is it justifcation of zero tolerance between it has to be a variable and constant?

Heading should read "variable OR constant"


If data is in a pointer the application has to run an algorithm to access it, "Addressing scheme". If the data is in a address location the application just retrieves the data, data is in the address location and nothing else needs to be done.I'm confused as how a variable can be a constant. Does all data in an application need to be declared to be a variable? Is Data a constant if it is not retrieved? In the example

int i, k;
i = 2;

i and k are both declared as integers.
2 is then assigned to the address space allocated for/by i.


Why assign 2 to anything if its in the code and does not have to be addressed? I guess i is just an alias for 2 that the preprocessor would take care of. If the preprocessor thing is true I don't understand why the author made such a round about big deal to try to connect it to being either a CONSTANT OR VARIABLE.

It would help to know why putting the application through the extra step to retrieve info is helpful, if you have an address that holds an address that then takes you do data.


Thanks a lot for the help. I would not have thought of the preprocessor thing without the foreign (alien to me) context.

Last edited by theKbStockpiler; 05-03-2010 at 06:32 PM. Reason: Heading should read variable OR constant
 
Old 05-03-2010, 07:06 PM   #6
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
I think the author is putting it this way so you have an idea whats going on behind the scenes with this particular assignment. Maybe he is trying to stress how this is going to be interpreted by the compile and what type of machine code is going to be generated. To me, and its very possible that I am wrong as its been a while since I wrote anything useful, is when you initalize a variable you are setting aside space in memory for that variable. Then when you declare it you are moving that "data" into memory. The data in the memory location can change but the constant that was used cannot. Thus you can re-assigen new data to i but you cannot assign data to 2.
Code:
 

int i,k; 

i = 2; 

i = k; 

2 = k; <--- invalid because 2 is a constant.
I had a college text book that I learned most of my C, and it had an excellent diagram of whats going on here.
 
Old 05-03-2010, 07:12 PM   #7
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by theKbStockpiler View Post
...
Why assign 2 to anything if its in the code and does not have to be addressed? I guess i is just an alias for 2 that the preprocessor would take care of.
...
It is not always that simple. Suppose you have a pretty long string.

If you just say

Code:
#define STR "A pretty long string"
and do not tell the compiler to make the strings unique, then whenever you use STR, a copy of the string is created.

If you declare at, say, global level
Code:
const char * const str = "A pretty long string";
and use str, only one copy of the string exists. To make your life both more enlightening and miserable read, for example, http://en.wikipedia.org/wiki/Const-correctness .
 
Old 05-03-2010, 07:38 PM   #8
ArthurSittler
Member
 
Registered: Jul 2008
Distribution: Slackware
Posts: 124

Rep: Reputation: 31
another attempt to answer

I like WPeckham's answer, but cannot resist adding my two cents' worth.

I like the bucket analogy. That is, any variable needs to have some storage to contain the value.

The declaration

int i,k;

allocates two "buckets". One of the buckets is named i and the other bucket is named k. Each bucket is large enough to contain an integer. The discussion assumes that this is stored in main memory, where it actually makes sense to refer to its address. One of the points here is that the addresses &i and &k are constant, fixed before the program actually runs. This does not mean that the contents are constants. It also does not mean that i exists only as a constant. In fact, the contents of i and k are explicitly declared to be changeable. The storage for the variables is allocated by the compiler as a result of the declaration of the variables i and k.

The quoted reference talks about the expression

i = 2;

and, perhaps confusingly, at this point, mentions that 2 is a constant and that the address of i, &i, is also a constant. This is laying out the groundwork for introducing another kind of variable, the pointer, that may contain the address of a variable.

The compiler recognizes that 2 is a constant at compile time because it starts with a digit. There is no need to allocate storage in an initialized-data page for the constant 2, fill it with the constant 2, and copy that value from that storage into the variable. The exact details of what sequence of machine-code instructions the compiler will emit to implement the assignment are architecture and implementation dependent, but nearly all CPUs provide some method for loading a CPU register with a constant contained in or appended after the machine instruction word within the instruction stream (the code segment). These constants embedded within the instructions are usually called "immediate values" in assembly language.

It turns out that being able to handle the addresses of variables is very handy, perhaps even essential, in creating some fundamentally important data structures, including linked lists. The C language permits allocating data storage at run time. Without pointers, C would be unable to address such dynamically allocated storage. This permits linked lists to grow during program execution (at least, until we run out of memory space).
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Daemons and the Kernel: Info requested theKbStockpiler Linux - General 6 05-04-2010 11:52 PM
Parameter passing: Parameters in General Info requested theKbStockpiler Programming 9 04-27-2010 08:52 PM
C Language Data Structure/ Algorithm Info Requested theKbStockpiler Programming 4 04-05-2010 02:25 AM
JAVA: Reference Variables (Pointers) and Constructors wwnexc Programming 14 03-10-2006 03:31 AM
pointers or variables? lordofring Programming 7 06-29-2005 01:13 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:24 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration