NeoR NeoR - 1 month ago 14
C Question

Use of type modifiers(near,far,huge) with normal variables

I used type modifiers(far,near,huge) with normal variables rather than pointers and found that these pointer type modifiers are only applicable for the global normal variable but an error is generated when used with a variable local to a block.

int near a,far b,huge c;

int main()
{
int d,e,f;

// int near a,far b,,huge c;
// long int near a,far b,huge c;
// long long int near a,far b,huge c;

//printf("\n size of a=%d ,b=%d ,c=%d ,d=%d ,e=%d ,f=%d",sizeof(a),sizeof(b),sizeof(c),sizeof(d),sizeof(e),sizeof(f));

printf("\n address of a=%u ,b=%u ,c=%u ,d=%u ,e=%u,f=%u",&a,&b,&c,&d,&e,&f);
return 0;
}


why is this allowed with a global variable and not with a local variable. Additionally, what does the variable finally becomes i.e. it becomes a pointer,an integer with greater range or entirely something else.

Answer

Everything that is placed on the stack can't be far modified.

You can place "non-far variables" in the stack, but not "far variables".

You can place "non-far pointers to non-far data" in the stack, but not "far pointers to non-far data**".

You can place "non-far pointers to far data" in the stack, but not "far pointers to far data".

Try this:

far int       var        = 0; /* OK */
far int far* far_var_ptr = &var; /* OK: far pointer to far data*/ 
int far*     var_ptr     = &var; /* OK: non-far pointer to far data*/ */

int main()
{
int far*      var_ptr2    = &var; /* OK: Non-far ptr to far data */
far int far* far_var_ptr2 = &var; /* Fail: far ptr to far data */ 
far int       var2         = 0;     /* Fail: far data */
}

The key is that you can't define far data on the stack. Variables on the stack are:

Placed within a defined range of memory

Its exact location depends on the call stack before: it can't be known at compile time

This is not a far data:

int far* var;

It is an non-modified pointer to far data. The pointer itself is just a number not far modified that points to data in a far segment (platform specific).

This is far data:

far int* var;

And this too:

far int far* var;

The storage (far, near, huge) modifier of a variable (or function) is placed before the variable type.

Comments