LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Info on Pointers and Variables Requested (https://www.linuxquestions.org/questions/programming-9/info-on-pointers-and-variables-requested-805709/)

theKbStockpiler 05-03-2010 04:29 PM

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

JohnGraham 05-03-2010 04:41 PM

Quote:

Originally Posted by theKbStockpiler (Post 3956103)
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.

Sergei Steshenko 05-03-2010 04:46 PM

Quote:

Originally Posted by JohnGraham (Post 3956114)
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'.

wpeckham 05-03-2010 04:51 PM

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.

theKbStockpiler 05-03-2010 06:29 PM

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.:scratch:

exvor 05-03-2010 07:06 PM

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.

Sergei Steshenko 05-03-2010 07:12 PM

Quote:

Originally Posted by theKbStockpiler (Post 3956214)
...
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 .

ArthurSittler 05-03-2010 07:38 PM

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).


All times are GMT -5. The time now is 11:27 PM.