Is sizeof (int) guaranteed equal to sizeof (void *)

Is the size of the int data type always equal to the size of the c pointer?

I'm just curious.

+4
source share
7 answers

No, there is no guarantee that sizeof(int) == sizeof(void*) . On Linux / AMD64, sizeof(int) is 4 bytes, and sizeof(void*) is 8 bytes (the same as sizeof(long) on this platform).

The modern C standard (for example, C99) defines a standard <stdint.h> header, which should define, among other things, the integral type intptr_t , which is guaranteed to have the size of pointers (and probably even that of pointers).

I think that the standard does not guarantee that all pointers are the same size, in particular a function pointer can be โ€œlargerโ€ than data pointers (I cannot name the platform where this is true). I believe that the recent Posix standard is required (e.g. for dlsym ).

+8
source

No. for example, on most 64-bit systems, int is 4 bytes, and void * is 8.

+2
source

This is not guaranteed.

And, for example, in most 64-bit systems, both sizes are usually different.

Even sizeof (int *) not guaranteed to be equal to sizeof (void *) .

The only guarantee for void * is

 sizeof (void *) == sizeof (char *) == sizeof (signed char *) == sizeof (unsigned char *) 
+2
source

No. Some (mostly older, VAX-period) code suggests this, but it is definitely not required, and it is assumed that it is not portable. There are real implementations in which the two are different (for example, some current 64-bit environments use a 64-bit pointer and a 32-bit int).

+1
source

The C languages โ€‹โ€‹offer no guarantees when it comes to the sizes of integers or pointers.

The size of the int usually the same as the width of the data bus, but not necessarily. The size of the pointer usually matches the width of the address bus, but not necessarily.

Many compilers use non-standard extensions, such as the far keyword, to access data beyond the default pointer width.

In addition to 64-bit systems, there are also many microcontroller / microprocessor architectures, where the size of the int and the size of the pointer are different. Windows 3.1 and DOS are other examples.

+1
source

There is no guarantee of any connection between the sizes of these two types, nor that they can be accurately represented in the other using round-trip spells. All of this is determined by implementation.

With that being said, in the real world, if you are not dealing with really obscurely obsolete 16-bit systems or odd DSPs or such, sizeof(int) will be less than or equal to sizeof(void *) , and you can faithfully convert int values โ€‹โ€‹to void * to pass them to interfaces (e.g. pthread_create ) that take the general argument void * to avoid wasting and allocating memory to hold one int . In particular, if you are already using POSIX or Windows interfaces, this is certainly a safe, real guess.

You should never assume that void * can be accurately represented in int (i.e. hovering over int and back). This does not work on any popular 64-bit real-world systems, and the percentage of systems on which it works will probably fall in the near future.

+1
source

No. Pointer types do not have to be the same size or representation as integer types. Here are some relevant sections from the C language standard (an online project is available here ):

6.2.5 Types
...
27 A pointer to a void must have the same presentation and alignment requirements as a pointer to a character type. 39) Likewise, pointers to qualified or unqualified versions of compatible types must have the same presentation and alignment requirements. All pointers to structure types must have the same presentation and alignment requirements as each other. All pointers to types of trade unions should have the same presentation and harmonization of requirements with each other. Pointers to other types should not have the same presentation or alignment requirements.
...
39) The same requirements for representation and harmonization imply interchangeability, like arguments for functions, return values โ€‹โ€‹from functions, and members of unions.
...
6.3.2.3 Pointers
...
5 An integer can be converted to any type of pointer. Except as previously indicated, the result is determined by the implementation, may not be aligned correctly, may not point to an object of a reference type, and may be a trap representation. 56)

6 Any type of pointer can be converted to an integer type. Except as noted above, the result is determined by implementation. If the result cannot be represented in an integer type, the behavior is undefined. The result should not be in the range of values โ€‹โ€‹of any type integer.
...
56) The mapping functions for converting a pointer to an integer or integer into a pointer are intended to be compatible with the addressing structure of the runtime.
+1
source

Source: https://habr.com/ru/post/1391695/


All Articles