What type of pointer operations

I searched quite a bit, but could not find anything useful - but then I am not sure that I was looking for the right thing.

Is there any scalar defined by the standard that should be at least as large as the pointer? That is, sizeof (?)> = Sizeof (void *).

I need this because I am writing a small garbage collector and want something like this:

struct Tag { uint32_t desc:sizeof(uint32_t)*8-2; // pointer to typedescriptor uint32_t free:1; uint32_t mark:1; }; 

I would prefer something valid according to the standard (if we are on it, I was very surprised that sizeof (uint32_t) * 8-2 is valid for determining the bit field, but VS2010 allows this).

So size_t fulfills this requirement?

Edit: So, after my inclusion of C and C ++ leads to some problems (well, there I thought they would be similar in this respect), I would really agree to one of them (I don't need C ++ for this part of the code, and I can bind C and C ++ together to make them work). And C99 seems to be the right standard in this case from the answers.

+4
source share
2 answers

You can include <stdint.h> (or <cstdint> ) and use uintptr_t or intptr_t .

Since MSVC refuses to support C99, you may need to enable <Windows.h> and use ULONG_PTR or LONG_PTR instead . (See heading C99 stdint.h and MS Visual Studio )

(Also use CHAR_BIT instead of 8.)

+4
source

C99 has an optional uintptr_t in <stdint.h> , which ensures that you can convert between uintptr_t and the pointer value, although it does not say anything about any operations on integer.

As a rule, on common platforms, void * matches any other pointer and converts a pointer to an integer, manipulating this integer and converting it back to a pointer gives clearly defined results, but C does not guarantee this, you need to know the compilers / platform, on which you want to target.

Best of all, you can use the above uintptr_t if you have a C99 compiler, or compile a program on the target platform that checks if sizeof (void *) is equal to any of sizeof unsigned short, int, long, long long and generate a header file where you typed your own uintptr according to what the program found.

+1
source

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


All Articles