is the compiler displaying logical or linear address value?

E

elvira_wang

heya,

what sort of address is displayed when this instruction for instance is
executed
printf("myvar location is 0x%lx\n", (long) &myvar);

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


thanks
 
I

Ian Collins

heya,

what sort of address is displayed when this instruction for instance is
executed
printf("myvar location is 0x%lx\n", (long) &myvar);
Why cast to long?
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
Entirely platform dependant.
 
E

Eric Sosman

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

=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=

heya,

what sort of address is displayed when this instruction for instance is
executed
printf("myvar location is 0x%lx\n", (long) &myvar);

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
You have to look at the documentation for your platform to answer this.
C doesn't specify the implementation, and this is implementation
specific.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top