View Full Version : Segments and offsets
NoRToN
December 31st, 2000, 19:56
How do I get the segment and offset of a variable in C?
The address of operator (&

, does only give one value.
zyn\ker
January 6th, 2001, 05:50
A high-level language like C isn't designed to provide you with low-level information about the underlying architecture. There are architectures out there who don't have something like a segment but only an offset, so what should the language do in such a case?
C is designed so the programmer doesn't have to bother with such architectural-specific quirks.
What the address &-operator returns is for example
-in Win32 programs the virtual address of the variable; a program in a Win32 environment is in "flat memory" space, which means there is no segment to bother with (you are in protected mode; there is of course a segment, but you must not bother with it, it's a task of the OS to handle this).
-in DOS programs which use Real Mode the offset in the Data-Segment (DS specified in the EXE-header or sometimes even at runtime). Also nothing a C-programmer should bother with, it's the task of the compiler to handle this.
In short: You can't even be sure the platform your C-Code is running on HAS a segment, so the language doesn't have any facility to retrieve it. Everything the &-operator returns should not be evaluated by value. It may even be something completely different than the address, it just has to ACT as an address.
Use Assembler if you want to do low-level stuff.
?ferret
January 8th, 2001, 19:59
...also note that most compilers allow you to use inline assembly to accomplish lower level tasks...
e.g. you start in C, add a routine in assembly in the middle of your proggy, go back to C (there will be a keyword to tell the compiler you're switching to assembly)
Powered by vBulletin® Version 4.2.2 Copyright © 2018 vBulletin Solutions, Inc. All rights reserved.