2

//The output for this code is observed to be 4 4 2 ? I dont get why is it 2 for ptr3?please help

#include<stdio.h>

int main()
{
char huge *near *far *ptr1;
char near *far *huge *ptr2;
char far *huge *near *ptr3;
printf("%d, %d, %d\n", sizeof(ptr1), sizeof(ptr2), sizeof(ptr3)); 
return 0;
}
Neeraj
  • 239
  • 5
  • 14
  • 1
    What system? What compiler? That's not standard C syntax. – Carl Norum Dec 19 '12 at 07:14
  • 5
    `near`, `far`, and `huge` suggest that it's some sort of ancient, obsolete DOS-based compiler. Borland C, perhaps? –  Dec 19 '12 at 07:15
  • The standard defines `sizeof(char)` to be `1`. You get no other guarantees. – Ed S. Dec 19 '12 at 07:15
  • 1
    In that case, the answer is "go read your compiler documentation". – Carl Norum Dec 19 '12 at 07:15
  • @E F: What made you believe that size `2` for that pointer is something unusual to the point that you even asked this question? Why not `2`? In a way it is like asking why `2 + 2` equals `4`. How does one answer a question like that? – AnT stands with Russia Dec 19 '12 at 07:24

4 Answers4

8

near, far, and huge pointers are non-standard.

In implementations that support them (usually for very old x86 systems), a specific location in memory can be specified by a segment and an offset. A near pointer contains only an offset, leaving the segment implicit. Thus it requires less space than a far pointer.

Modern compilers do not use any of these keywords. I suggest you find yourself a newer compiler; there are plenty of free ones available.

See this question for more information.

Incidentally, printf's "%d" format requires an argument of type int; sizeof yields a result of type size_t, which is a different type. You can change your printf call to:

printf("%d, %d, %d\n", (int)sizeof ptr1, (int)sizeof ptr2, (int)sizeof ptr3);

(The modern way to do that is:

printf("%zu, %zu, %zu\n", sizeof ptr1, sizeof ptr2, sizeof ptr3);

but an implementation that supports near and far pointers isn't likely to be modern enough to support "%zu", which was introduced in C99.)

Community
  • 1
  • 1
Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
  • or rather use %zu ? (provided he updates to C99 compliant compiler...) – ShinTakezou Dec 19 '12 at 07:24
  • 3
    @ShinTakezou: An implementation that supports `near` and `far` pointers is unlikely to support `"%zu"`. – Keith Thompson Dec 19 '12 at 07:25
  • thought of it 1 minutes later :) – ShinTakezou Dec 19 '12 at 07:28
  • 1
    I'm not sure exactly how widespread this is, but smaller embedded platforms (in the pre-smartphone sense) sometimes have very limited choice of compilers, and these compilers could well use the near/far keywords. But yeah, +1, I think you nailed the answer by saying near pointers are smaller. – David Duncan Dec 19 '12 at 07:29
  • @KeithThompson Imagine platforms like AVR AT(x)mega etc - they have kind of `near`/`far` and nevertheless are (partially) C99-compliant... – glglgl Dec 19 '12 at 07:32
1

this far near and huge are the concepts used in old DOS-compilers.

For more you can see this link.

Which will explain everything about how are these implemented internally. Due to this implementation you are getting the size like this.

Omkant
  • 9,018
  • 8
  • 39
  • 59
1

Just to remove a layer of confusion, so you can use the other answer.

Declaration of a near pointer looks like this:

type near* variable;

Or like this:

near type* variable;

Where type is any type. In your example, someone is trying to confuse you because type is itself a pointer type: char far *huge *. However, no matter what it points to, a near pointer occupies 2 bytes.

(I guess someone is trying to confuse you on purpose because there is no chance such constructs appear in real code)

Community
  • 1
  • 1
anatolyg
  • 26,506
  • 9
  • 60
  • 134
1

I think you are using an old compailer (may be turboc c).Latest compiler not support this functionality.

near pointers don't have a selector - they have an implied selector. They can access 64k off the virtual address space on a 286 and earlier and 4Gb of the addresss space on a 386 or later.

far pointers have an explicit selector. However when you do pointer arithmetic on them the selector isn't modified.

huge pointers have an explicit selector. When you do pointer arithmetic on them though the selector can change.

linuxchip
  • 316
  • 1
  • 5