In the Turbo C compiler, the size of an int pointer is shown as 2 bytes when sizeof() operator is used. Now, if I print the address of an int variable, it comes out to be a hexadecimal number of 8 digits, which makes the size of address to be 32 bits (or 4 bytes).
So, why is the Turbo compiler showing 2 bytes size for the pointer?
Your program is compiled under the small memory model, meaning that your entire data space takes up no more than 64K of space. When the program starts, the DS register is pointed at that data space, so pointers only need to be 16 bits to reference any location in the data space.
In the medium and large memory models, the data space could be larger than 64K, and you'd find your pointers to be 32 bits.
Please see Alok's comments. See gcc for a replacement.
I haven't used Turbo C, but I believe it is an old 16-bit DOS/Windows compiler. 16bit programs had near and far pointers. Near pointers were 2 bytes and could only point to the current segment. Far pointers were a 2 byte segment and a 2 byte offset which were shifted and added to give 20 bits of addressing.
The 8 digits you are seeing are the 2 byte pointer added to the shifted value of the current segment. See x86 memory segmentation: http://en.wikipedia.org/wiki/X86_memory_segmentation
You probably have a 16-bit version of Turbo C, and that's the size of int
for that compiler: http://www.cs.technion.ac.il/~nikol/material/types_sizes.pdf
In fact, all versions of Turbo C were 16-bit, apparently.
As the others already suggested, get yourself a modern C compiler, like gcc
or clang
.
The true answer of this depends on which setting in Turbo C you are using. Pointers can be 16 or 32-bit, depending on whether they are "near" or "far" pointers. A far pointer can address "all" of the 1MB memory range that a PC from that era (some 20+ years ago) would have, by having a segment portion of 16 bits and a offset portion of 16 bits. These values are combined as (segment << 4) + offset
.
There are "models" of code and data spaces that determine whether you get a near or far pointer for data and/or code.
This page describes the different models: http://www.tti.unipa.it/~ricrizzo/KS/Data/PBurden/chap6.msdos.memory.html (Although it incorretly states that addresses can reach 256MB - it should be 1MB, and of course since the last 64KB is "BIOS", and memory between A0000-EFFFF is "memory mapped hardware", in practice you can only use up to 64KB [and if you "flip" the A20-gate, you can use 64KB-16 byte above 1MB as RAM, assuming there is RAM above 1MB])
Of course, like others have said, stop using a compiler that is old enough to get a drivingl license in most countries in the world. There are other products that are much better these days (no matter what your definition of "better" is - unless "comes on floppy disks" is defined as better).
There was an era when computer were run on only DOS. At that time the booting of operating system is was real mode booting. So In Real mode operating systems the OS can use only 1MB of address space. the turboc actually made for real mode so it is called as 16 bit. the turboc doesn't know what is the RAM f your computer or what is the processor whether it is 16 bit 32 bit or 64 bit. So in turboc the pointer is always 2 bytes . .but if u change the settings under options>>compiler>>model and set it to huge. Then it goes completely upto 1 MB address space using two registers. of 2 byte and in that case pointer could be or will be 4 bytes
来源:https://stackoverflow.com/questions/14159971/shouldnt-an-int-pointer-in-turbo-c-be-of-size-4-bytes