Sunday 20 November 2011

What is the difference between near and far? in C programming

What is the difference between near and far?

DOS uses a segmented architecture to address your computer’s memory. For each physical memory location, it has an associated address that can be accessed using a segment-offset method. For instance, here is a typical segmented address:

A000:1234

The portion on the left side of the colon represents the segment (A000), and the portion on the right side of the colon represents the offset from that segment. Every program under DOS accesses memory in this manner—although the intricacies of addressing with the segment-offset method are often hidden from the casual C programmer.

When your program is executed, it is assigned a default data segment that is put in the data segment (DS) register. This default data segment points to a 64KB area of memory commonly referred to as near data. Within this near data area of memory, you will find your program’s stack, static data, and the near heap. The near heap is used for allocating global variables and other data elements you need at program start-up. Any data allocated from this area is called near data. For instance, consider the following program, which allocates
32KB of near data from the near heap at program start-up:

/* Note: Program uses the Medium memory model... */
#include <stdio.h>
#include <alloc.h>
#include <string.h>
#include <stdlib.h>
#include <dos.h>
void main(void);
void main(void)
{
char* near_data;
near_data = (char*) malloc((32 * 1024) * sizeof(char));
if (near_data == (char*) NULL)
{
printf(“Whoopsie! Malloc failed!\n”);
exit(1);
}
strcpy(near_data,
“This string is going to be stored in the near heap”);
printf(“Address of near_data: %p\n”, &near_data);
free(near_data);
}

In the preceding example, near_data is a character pointer that is assigned a 32KB block of memory. By default, the 32KB block of memory is allocated from the near heap, and the resulting 16-bit address is stored
in the character pointer near_data.

Now that you are aware of what near data is, you are probably wondering what far data is. Quite simply, it is any data that resides outside of the default data segment (the first 64KB of data memory). Here is an example program that allocates 32KB from the far data area (or far heap, as it is commonly called):

/* Note: Program uses the Medium memory model... */
#include <stdio.h>
#include <alloc.h>
#include <string.h>
#include <stdlib.h>
#include <dos.h>
void main(void);
void main(void)
{
char far* far_data;
far_data = (char far*) farmalloc((32 * 1024) * sizeof(char));
if (far_data == (char far*) NULL)
{
printf(“Whoopsie! Far malloc failed!\n”);
exit(1);
}
_fstrcpy(far_data,
“This string is going to be stored in the far heap”);
printf(“Address of far_data: %Fp\n”, &far_data);
farfree(far_data);
}

In this example, the far character pointer is assigned a 32-bit address reflecting a 32KB area of free memory in the far heap. Notice that to explicitly allocate from the far heap, a far pointer must be used, and hence the far modifier is added to the character pointer definition. Also note that some of the functions (farcoreleft(),farmalloc(), farfree()) are different for allocating from the far heap as opposed to the near heap.

The far heap usually contains much more free memory than the near heap, because the near heap is limited to 64KB. If you compile and run the previous examples on your computer, you will find that the first example (which allocates from the near heap) has approximately 63KB of memory available. The second example (which allocates from the far heap) has approximately 400KB to 600KB (depending on your computer’s configuration) of memory available. Thus, if your program requires a lot of memory for data storage, you should use the far heap rather than the near heap.

Whatever memory model you use (with the exception of the Tiny memory model), you can use the near and far modifiers and their corresponding near and far functions to explicitly allocate memory from the near and far heap. Using near and far data wisely will help your programs run more efficiently and have less risk of
running out of memory.

Note that the concept of near and far data is unique to personal computers running DOS because of the segmented architecture scheme used by DOS. Other operating systems such as UNIX or Windows NT use flat memory models, which impose no near or far limitations.

Cross Reference:

XVIII.11: I get the message DGROUP: group exceeds 64KB during my link. What’s wrong?
XVIII.12: How can I keep my program from running out of memory?
XVIII.13: My program is too big to run under DOS. How can I make it fit?
XVIII.14: How can I get more than 640KB of memory available to my DOS program?

No comments:

Post a Comment