Should an int pointer in Turbo C be 4 bytes?

In the Turbo C compiler, the size of an int pointer is displayed as 2 bytes when the sizeof () operator is used. Now if I print the address of an int variable, it will be an 8-digit hexadecimal number, which makes the address size 32 bits (or 4 bytes).

So why is the Turbo compiler showing 2 bytes for a pointer?

0


source to share


5 answers


Your program is compiled under a small memory model, which means your data space is at most 64KB. When the program starts, the DS register points to this data space, so pointers only need 16 bits to refer to any location in the data space.

In medium to large memory models, the data space can be larger than 64KB and you will find that your pointers are 32 bits.



See Alok's comments. See gcc for replacement.

+5


source


I have not used Turbo C, but I believe it is an old 16-bit DOS / Windows compiler. 16-bit programs had pointers to near and far. There were 2 bytes next to the pointers and could only point to the current segment. Large pointers were a 2-byte segment and a 2-byte offset that were shifted and added to give 20 addressing bits.



The 8 digits you see represent a 2-byte pointer appended to the shifted value of the current segment. See x86 Memory Segmentation: http://en.wikipedia.org/wiki/X86_memory_segmentation

+3


source


You probably have a 16-bit version of Turbo C and the size int

for that compiler: http://www.cs.technion.ac.il/~nikol/material/types_sizes.pdf

In fact, all Turbo C versions were apparently 16-bit.

As others have said, get yourself a modern C compiler like gcc

or clang

.

+2


source


The true answer to this depends on what setting you are using in Turbo C. Pointers can be 16 or 32 bits, 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 (about 20 years ago) would have had a 16-bit segment portion and a 16-bit offset portion. These values ​​are combined as (segment << 4) + offset

.

There are "models" of code and information spaces that determine whether you have a near or far pointer to data and / or code.

The various models are described on this page: http://www.tti.unipa.it/~ricrizzo/KS/Data/PBurden/chap6.msdos.memory.html (Although he impartially states that addresses can go up to 256MB - it should be 1MB, and of course, since the last 64KB is "BIOS" and the memory between A0000-EFFFF is "memory mapped hardware", in practice you can only use up to 64KB [and if you "flip" A20-gate, you can use 64KB-16 bytes above 1MB as RAM, assuming RAM is above 1MB])

Of course, as others have said, stop using a compiler that is old enough to get a driving license in most of the world. There are other products these days that are much better (no matter what you define as "better" - unless "comes to floppy" is better).

+1


source


There was an era when the computer only ran on DOS. At that time, the operating system was booted in real mode. Therefore, in real mode operating systems, the OS can only use 1 MB of address space. turboc is actually made for real mode, so it is called 16 bit. turboc doesn't know what the RAM is on your computer or what the processor is, be it 16 bit 32 bit or 64 bit. So in turboc the pointer is always 2 bytes .. but if u change the settings in options -> compiler -> and set it to huge. Then it goes all the way up to 1 MB of address space using two registers. 2 bytes, in which case the pointer can or will be 4 bytes

0


source







All Articles