How Many Bytes Do Pointers Take Up? - Exchangetuts
Maybe your like
Tags:
c
pointers
I am little bit confused about pointers and how many bytes they take up. In my textbook it first says that pointers on 16 bit systems take up 2 bytes, 32 bit systems 4 bytes, 64 bit system 8 bytes and so on. Then 10 lines after, it says that pointers take up that many bytes, that are needed to hold the addresses. Here are my questions :
- So does this mean that if we are lets say on 64 bit system, address will need at most 8 bytes?
- If we are on 16 bit system and pointers take 2 bytes, and address needs more the 2 bytes to be placed then what happens?
GovernmentFX
People also ask
Is a pointer always 4 bytes?The size of a pointer in C/C++ is not fixed. It depends upon different issues like Operating system, CPU architecture etc. Usually it depends upon the word size of underlying processor for example for a 32 bit computer the pointer size can be 4 bytes for a 64 bit computer the pointer size can be 8 bytes.
Is a pointer always 8 bytes in C?As we already know, the size of the pointer in C is dependent only on the word size of a particular system. So, the size of a pointer to a pointer should have the usual values, that is, 2 bytes for a 16-bit machine, 4 bytes for a 32-bit machine, and 8 bytes for a 64-bit machine.
Why do pointers use 4 bytes?Size of a pointer is fixed for a compiler. All pointer types take same number of bytes for a compiler. That is why we get 4 for both ptri and ptrc.
1 Answers
There is no fixed answer; it depends entirely on the architecture, the compiler implementation, and even the type of the pointer itself. Pointers to different types are not guaranteed to have the same size and/or representation.
For example, assume a word-addressed architecture, where the smallest addressable unit of storage is 16 bits wide (or wider). Each word can hold multiple char values; all other types take up a full word or more. On such an architecture, a char * and void * would need some extra bits to offset into the word compared to other pointer types.
Note also that a pointer type may be wider than the number of bits actually required to store an address. The original Macintosh ran on a Motorola 68000 CPU, which had a 32-bit word size, but only 24 bits on the address bus. Pointer types were 32 bits wide, leaving the upper 8 bits unused. Enterprising MacOS programmers took advantage of that to store some data to the uppermost byte of a pointer type, making the most of that precious 128 KB of RAM. Of course, Motorola eventually released a CPU with 32 address lines (the 68020), meaning all that code had to be rewritten.
On modern, commodity desktop and server hardware (read: x86), it's reasonably safe to assume that all pointer types are the same size as the native word size (32- or 64-bit), and that all pointer types have the same size and representation. Just be aware that this doesn't have to be true.
answered Sep 22 '22 08:09 John Bode
Sign in to CommentRelated questions
Is it safe to use variable-length arrays? How to install cmake on Windows 64 bit Behavior of __LINE__ in inline functions is it necessary to call pointer = NULL when initializing? Memory handling with struct epoll_event Convert a char to double [closed] In C/C++, for an array a, I just learned that (void*)&a == (void*)a. How does that work? Difference between int32_t and int_fast32_t [duplicate] Is the preprocessor macro "#define TRUE FALSE'" valid? [closed] How does subtracting the character '0' from a char change it into an int? Difference between pthread_exit, pthread_join and pthread_detach Fastest way to calculate the abs()-values of a complex array Getting base name of the source file at compile time Commenting C code, header and source files [closed] glGenBuffers not defined? Why do I need a redistributable package on unmanaged code? (msvcp100.dll) Defining the function's argument type after the ")", is it a very old standard? How to return matrix (2D array) from function? (C) __attribute__(packed) v/s GCC __attribute__((aligned(x)) int8_t vs char ; Which is the best one?Recent Activity
- Regex Rejecting matches because of Instr
- Using shadowbox disables keyboard shortcuts?
- How do I lock certain SQL rows while running a process on them?
- What is C# analog for C's two-dimensional array access like that: palette[ val & 3 ]?
- C++11 threads: notify_all() or notify_one() when I only have one?
- Best way to distribute an application that uses JNI
Donate For Us
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us WithTag » How Many Bytes In A Pointer
-
Standard Data Types - IBM
-
How Many Bytes Do Pointers Take Up? - Stack Overflow
-
Is A Pointer Always 4 Bytes? - Quora
-
Taking Advantage Of 8-byte Pointers In Your C And C++ Code - IBM
-
What Is A Size Of Pointer In C? | Coding Ninjas Blog
-
Pointer Basics In C - C Programming Tutorial
-
19A. The Memory And Pointers
-
C | Pointer Basics | Question 4 - GeeksforGeeks
-
How Many Bytes Do Pointers Take Up
-
[PDF] Introduction To Pointers - CSE - IIT Kanpur
-
If We Declare A Pointer Like Char *p;how Much Memory Is Allocated ...
-
Pointers In Go - Go 101
-
Data Types And Sizes
-
[PDF] Memory And C++ - Stanford Computer Science