Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

size of pointers and architecture

Tags:

By conducting a basic test by running a simple C++ program on a normal desktop PC it seems plausible to suppose that sizes of pointers of any type (including pointers to functions) are equal to the target architecture bits ?

For example: in 32 bits architectures -> 4 bytes and in 64 bits architectures -> 8 bytes.

However I remember reading that, it is not like that in general!

So I was wondering what would be such circumstances?

  • For equality of size of pointers to data types compared with size of pointers to other data types
  • For equality of size of pointers to data types compared with size of pointers to functions
  • For equality of size of pointers to target architecture
like image 228
AKL Avatar asked May 19 '19 14:05

AKL


People also ask

What are the size of pointers?

On 32-bit machine sizeof pointer is 32 bits ( 4 bytes), while on 64 bit machine it's 8 byte.

Are pointers 4 or 8 bytes?

Note that all pointers are 8 bytes.

What is the size of a character pointer?

The size of the character pointer is 8 bytes. Note: This code is executed on a 64-bit processor.

Are pointers different sizes?

Generally yes, All pointers to anything, whether they point to a int or a long or a string or an array of strings or a function, point to a single memory address, which is the same size on a machine.


2 Answers

No, it is not reasonable to assume. Making this assumption can cause bugs.

The sizes of pointers (and of integer types) in C or C++ are ultimately determined by the C or C++ implementation. Normal C or C++ implementations are heavily influenced by the architectures and the operating systems they target, but they may choose the sizes of their types for reasons other than execution speed, such as goals of supporting lower memory use (smaller pointers means less memory used in programs with lots of pointers), supporting code that was not written to be fully portable to any type sizes, or supporting easier use of big integers.

I have seen a compiler targeted for a 64-bit system but providing 32-bit pointers, for the purpose of building programs with smaller memory use. (It had been observed that the sizes of pointers were a considerable factor in memory consumption, due to the use of many structures with many connections and references using pointers.) Source code written with the assumption that the pointer size equalled the 64-bit register size would break.

like image 192
Eric Postpischil Avatar answered Oct 26 '22 17:10

Eric Postpischil


It is reasonable to assume that in general sizes of pointers of any type (including pointers to functions) are equal to the target architecture bits?

Depends. If you're aiming for a quick estimate of memory consumption it can be good enough. But not if your programs correctness depends on it.

(including pointers to functions)

But here is one important remark. Although most pointers will have the same size, function pointers may differ. It is not guaranteed that a void* will be able to hold a function pointer. At least, this is true for C. I don't know about C++.

So I was wondering what would be such circumstances if any?

It can be tons of reasons why it differs. If your programs correctness depends on this size it is NEVER ok to do such an assumption. Check it up instead. It shouldn't be hard at all.

You can use this macro to check such things at compile time in C:

#include <assert.h> static_assert(sizeof(void*) == 4, "Pointers are assumed to be exactly 4 bytes"); 

When compiling, this gives an error message:

$ gcc main.c  In file included from main.c:1: main.c:2:1: error: static assertion failed: "Pointers are assumed to be exactly 4 bytes"  static_assert(sizeof(void*) == 4, "Pointers are assumed to be exactly 4 bytes");  ^~~~~~~~~~~~~ 

If you're using C++, you can skip #include <assert.h> because static_assert is a keyword in C++. (And you can use the keyword _Static_assert in C, but it looks ugly, so use the include and the macro instead.)

Since these two lines are so extremely easy to include in your code, there's NO excuse not to do so if your program would not work correctly with the wrong pointer size.

like image 38
klutt Avatar answered Oct 26 '22 16:10

klutt