logo
Send your CVContact

/

logo
Background image

Blog

How much does it Pointer weight?

Wojciech Stróżyński 23.01.2024

Known. The pointer weighs 4 bytes on a 32-bit architecture and 8 bytes on a 64-bit architecture. But are these the only possibilities? What can be the size of the pointer?

To think about how much the pointer weighs, maybe first let’s think about what the pointer is. And what value it represents.

Physics of the pointer

Depending on whether our program runs directly on hardware (microcontrollers with small real-time systems) or on a larger machine, such as a home Linux PC, the pointer will physically mean something else. For small microcontrollers, the pointer will most often simply be the address in memory where the data actually is located. In the second case, it will be an address in memory mapped by the MMU for a specific process running on the operating system.

On systems with direct memory access, its size corresponds to the number of bits used to address physical memory. On 32-bit systems, these are 4 bytes that address 232, or 4294967296 bytes. This is 4096 MB, or 4GB of memory.
For larger machines, its size corresponds to the amount of memory that can be addressed by a single process running on the system. That’s why we couldn’t allocate more than 4GB of memory to the process on older computers. The process was simply not able to address it.
64-bit systems abolish this limitation. Or do they just deter them once again?

However, let’s go to the example and use the following code:

int main() {
    std::cout        < sizeof(char) < " sizeof(char)" < "n"
        < sizeof(char*) < " sizeof(char*)" < "n"
        < sizeof(int) < " sizeof(int)" < "n"
        < sizeof(int*) < " sizeof(int*)" < "n"
        < sizeof(long) < " sizeof(long)" < "n"
        < sizeof(long long) < " sizeof(long long)" < "n"
        < sizeof(void(*)()) < " sizeof(void(*)())" < "n"
        < sizeof(int(*)(int)) < " sizeof(int(*)(int))" < std::endl;
}

ideone.com

We will get this result:

1 sizeof(char)
8 sizeof(char*)
4 sizeof(int)
8 sizeof(int*)
8 sizeof(long)
8 sizeof(long long)
8 sizeof(void(*)())
8 sizeof(int(*)(int))


Such results of the application compiled will give the 64-bit system should not surprise anyone.

char takes one byte, but the pointer on it already has 8 bytes. Similarly, all other pointers. Even pointers on free functions „weigh” exactly the same amount – 8 bytes.

And that’s probably what we could end up with, because it covers 99% of the cases of the size of the pointers we’re ever going to work with.

But this one percent remains.

 

Embedded

The embedded world is often governed by different rules. The same is true here. As I wrote earlier, the size of the pointer depends on how wide (how many bit) is access to memory. A smaller pointer will not be able to address all available memory. The bigger one is going to be a waste – and we don’t like it.

For example:

  • popular in the 1980s, the Z80 operated on 16-bit pointers (in C)
  • the original 8086 chip used 16, 20, or 32-bit pointers
  • AVRs use 16-bit SRAM access data rails

As you can see in just a few of the examples above, you can see that the size of the pointers may vary depending on the platform on which we write the code.

 

Indicator per method

There is at least one more aspect to consider. Pointers on class methods and structures.

Consider the following code:

struct A
{
    void run() const { }
};

int main() {
    std::cout        < sizeof(&A::run) < " sizeof(&A::run)" < std::endl;
}

ideone.com

What do you think? What will be the result of executing such code?
Well, it will be:


16 sizeof(&A::run)

So. 16 bytes. But that’s not the only answer. This value depends on the compiler, and so in Visual C++ for example, the pointer size per class method can be 4, 8, 12, or 16 bytes depending on the platform, compiler, and class implementation.

As you can see, the world of pointers on methods of structures or classes is a wild world. Much depends on the compiler or the class itself – whether the inheritance is single or multiple. Or virtual? All this can increase the size of the pointer per class method. If you want to learn more about pointers on methods I encourage you to read this article on codeproject.com

 

A summary of the

Even the seemingly simple question of how big the pointers in C++ can cause trouble for many of us, because who among us once cared about how much the index weighs? I hope that I have brought the nature of the pointer a little closer to those of you who have not yet felt it.

logo
ContactSend CV