Greedo
Greedo

Reputation: 5543

What really dictates pointer size in VBA?

Conventional wisdom:

In the latest version of VBA (VBA7),use the LongPtr type to represent pointers, which works on both 32 and 64-bit systems. In old versions of VBA, use Long to represent pointers (assuming a 32-bit host, but this is a safe assumption as all 64-bit hosts use the latest version of VBA).

And what exactly is LongPtr? Well according to the VBA language reference:

LongPtr (Long integer on 32-bit systems, LongLong integer on 64-bit systems)

Those two pieces of information confirm one fact: On 32-bit systems, pointers take 4 bytes and should be stored in a Long, on 64-bit systems, pointers take 8 bytes and should be stored in a LongLong

and by extension, LongPtr looks a bit like this under the hood:

#If Win64 Then
    typedef LongPtr As LongLong
#Else
    typedef LongPtr As Long
#End If

i.e. pointer size is linked directly to host bitness. In my experience this interpretation has always worked fine.


Problem

As pointed out in this question, pointers tend to be the same size as the bitness of the architecture. E.g. for a 32 bit program, pointers are 4 bytes, for a 64 bit program, pointers are 8 bytes. However there is no reason for this necessarily to be the case; a program with a 64-bit memory size may still use 32 bit pointers to navigate it.

That makes sense of you think about it. Just because my 64-bit program has used 8 byte chunks to split up its memory, doesn't mean I need 2^64 different possible pointers to navigate it. I may only have 16 bytes of memory in total, in which case my pointer could just be 1 or 0!


So my questions:

Upvotes: 3

Views: 484

Answers (1)

freeflow
freeflow

Reputation: 4355

two paragraphs before section 2.3

https://interoperability.blob.core.windows.net/files/MS-VBAL/%5bMS-VBAL%5d.pdf

"An implementation-defined LongPtr type alias is also defined, mapping to the underlying declared type the implementation will use to hold pointer or handle values. 32-bit implementations SHOULD map LongPtr to Long, and 64-bit implementations SHOULD map LongPtr to LongLong, although implementations MAY map LongPtr to an implementation-defined pointer type. The LongPtr type alias is valid anywhere its underlying declared type is valid. "

Upvotes: 1

Related Questions