heya,
what sort of address is displayed when this instruction for instance is
executed
printf("myvar location is 0x%lx\n", (long) &myvar);
Technically, there are two problems with this line. First,
it converts a pointer to an integer -- the conversion is allowed
by the rules of C, but the result is platform-dependent and need
not be meaningful. In any case, it is likely to blur or maybe
even obliterate any distinction between "logical address" and
"linear address."
The second and lesser problem is that "%lx" expects an unsigned
long argument but a plain signed long is provided. This is highly
unlikely to cause trouble, but it's a venial sin nonetheless.
A better way to display an address is to use the "%p" format
specifier, like this:
printf ("myvar location is %p\n", (void*) &myvar);
This has an important advantage over trying to convert a pointer to
a number: it will display the pointer in a form that makes sense for
the machine running the program. Since you seem to be interested in
the details of that form, it's better to let the machine show it to
you than to beg the question by pre-converting the pointer to a
number.
By the way: Without the (void*) cast, the code would be wrong in
much the same sense that "%lx" is wrong for a signed long. You'll
probably get away with it on most machines, but there's no harm in
being squeaky clean.
is it logical address or linear address, i.e. with logical address i
mean
segment number | relative address within the segment, whereas with
linear address i mean
*physical* starting address of segment | relative address where the
symbol stands for concatenation
It may surprise you to learn that "segment number" is not part of
an address on every computer. Some computers get along fine without
them, while others have addresses that are structured more elaborately
than a mere "segment number" can express. That's (partly) why C is
so afraid to, er, address the matter of converting pointers to numbers
and back again: If "BLUE 36" and "RED 36" are different addresses but
both convert to the integer 36, information is lost in the conversion.
Simple integers may not be an adequate model for addresses on some
machines (CPUs with separate I- and D- and IO-spaces, for example),
so the conversion is hard to define portably -- and C doesn't try.