Warning : This is ment for advanced QB users or advanced C/C++ users wanting to expand their knowelge and make their own libraries. QB Begineers beware, this would be a frighting scare!

Level One:
It begins

I would just like to begin this tutorial with a note

*note: Pigs do not, and I repeat DO NOT FLY!*

hehehe... now that's cleared up lets begin.

Computers memory is found on the Mother board. There is ussally 4 slots where you can insert 4MG/8MG/16MG/or even new 32MG RAM Chips [Ed: Updated!now you can have 128MG and 64MG RAM! Prehaps more by the time your reading this]. Those 4 slot's memory is combinied and the computer can use them. There are many diffrent ways of accessing them including DPMI,EMS,XMS and more but for the sake of begineers I'll start off with good old 16-bit (or realmode).

Oh yeah if I were you (If you were a QB Begineer) I would read the Hex tutorial before I touch this.

ahh ... oh sorry, what were we talking about agian? oh yes segments and offsets.

[To more than begineers : this is a 16-bit tutorial, there is a bigger FFFFFFFF limit to pointers for 32-bit. FFFF is only the limit to 16-bit]

Well lets look at the whole computers memory as a peice of paper. The paper is seprated into FFFF (65535, you really should have read the Hex tutorial = ) ) diffrent segments. These segments are 64K big, and to point to each diffrent byte you need to have the pointer. We write bytes in the memory like this :

Segment:Offset

Pretty simple eh? Thought you'd agree. Now one famous segment is the VGA Memory Segment, which is A000. So to point to the first byte of the VGA memory you simply right the memory like this

A000:0000

(note how both the segment and the pointer are both 16-bit)

and the last byte of the VGA memory is FFFF because that's the most a 16-bit number can be, ofcourse there is almost no limit for 32-bit pointers but then things would be more compilcated and I would'nt be able to finish this tutorial in time for tea. = )

So the last byte of the VGA memory would be:

A000:FFFF

``` _______________________
|_|_|_|_|_|_|_|_|_|_|_|_|<--- Lets look more closely at this one
|_|_|_|_|_|_|_|_|_|_|_|_|
|_|_|_|_|_|_|_|_|_|_|_|_|	  Segment 1A78
|_|_|_|_|_|_|_|_|_|_|_|_|	 ______________
|_|_|_|_|_|_|_|_|_|_|_|_|	|0000|0001|0002|<-These numbers are offsets
|_|_|_|_|_|_|_|_|_|_|_|_|	|0003|0004|0005|  when we write
|_|_|_|_|_|_|_|_|_|_|_|_|	|0006|0007|0008|
|_|_|_|_|_|_|_|_|_|_|_|_|	|0009|000A|000B|
|_|_|_|_|_|_|_|_|_|_|_|_|	|000C|000D|000E|
|_|_|_|_|_|_|_|_|_|_|_|_|	|000F|0010|0011|
|_|_|_|_|_|_|_|_|_|_|_|_|	 .... .... ....
```
Pretty simple system right? No.

In reality segments overlap. Since you can only access about 1088K of memory in real mode, there can only be 1088K worth of segments and offsets to address the memory in. If each segment actually contained 64K of memory then YIX! lets see we've got 4 Gigs of memory to address! Too much for comfort *They thought*. Segments overlap each other each 16 bytes. And there is more than 2^20 bytes worth of memory to address. If segments overlap then you can access the same memory in a diffrent segment! A000:FFFF can also be accessed at A001:FFEF and A002:FFDF and A003:FFCF. Why? Because segments overlap!

To find if which memory you are accessing. You use this formuale:

```Memory=Segment*16+Offset
```
Or in Hex
```Memory=Segment*10H+Offset.
```
So A000:FFFF would be
```A0000+FFFF=AFFFF
```
and A001:FFEF would be
```A0010+FFEF=AFFFF
```
... the same memory! In reality this is a really shitty system. When you get to 32-bit memory addressing it gets better, there is no segments and offsets. You access memory with a 2 byte pointer. So the video memory (in 32-bit) would start at 000A0000, and end at 000AFFFF. Upto 4 Gigs can be accessed with the 32-bit system, it's a better system in reality but we'll get on to it later. At the momment as far as your concerned segments and offsets are the norm = )

Level Two:
Segments in QB

As most people know, you could easily find out where a variable's/array's memory location is. To find the Segment:Pointer you do this :
```-8<---------------------------------------------------------------------------
DIM variable%			'You don't really need this, but just to show
'you what variable% I'm using. And besides,
'after you allocate it once, you don't have
'to waste clock-time allocating it somewhere
'else

Segment = VARSEG(variable%)	'gives us the segment.
Pointer = VARPTR(variable%)	'gives us the offset

PRINT "variable%'s memory location is ";
PRINT HEX\$(Segment);":"HEX\$(Pointer)	'This writes it in Hex.
------------------------------------------------------------------------->8---
```
There you go. The same can be done with an array.
```-8<---------------------------------------------------------------------------
DIM variable%(0)		'You don't really need this, but just to show
'you what variable% I'm using. And besides,
'after you allocate it once, you don't have
'to waste clock-time allocating it somewhere
'else

Segment = VARSEG(variable%(0))	'gives us the segment.
Pointer = VARPTR(variable%(0))	'gives us the offset.

PRINT "variable%'s memory location is ";
PRINT HEX\$(Segment);":"HEX\$(Pointer)	'This writes it in Hex.
------------------------------------------------------------------------->8---
```
Like the scissors? hehehe. Well anyway, you do get the point right? good.