Specify a NULL pointer in C

I have a structure

struct detail { int id; uintptr_t init; // blah blah }; struct detail info; info.id = 1; info.init = (uintptr_t)NULL; 

I need to make the init element NULL. What may or may not happen if I draw (or don't think up) NULL ? What if I directly assign it to NULL, for example info.init = NULL; Does it matter for runtime errors. It compiles fine. But code execution is my main problem.

thanks

+4
source share
3 answers

There is no guarantee in the standard that if ptr is a null pointer, then (uintptr_t)ptr is 0 .

If you do not need systems for which null pointers and zero integers are not equivalent, then info.init = 0; excellent.

The init element is an integer type; it cannot be "made null". You can assign it 0 , or you can assign it the result of converting a null pointer to uintptr_t . In almost all C implementations, all of this is one and the same. But this is not guaranteed, and there were systems on which it is not the same.

NULL may be a null pointer, or it may be an integer constant of 0 . In the latter case, the standard guarantees that (uintptr_t)(NULL) 0 . Thus, there may be implementations on which info.init = NULL; (void*)(info.init); info.init = NULL; (void*)(info.init); has undefined behavior. This will not result in a null pointer if the integer equivalent null is not 0, and the calculation of the invalid pointer value is UB.

So, if you want to ensure that info , when converted to a pointer type, outputs a null pointer, then for true portability you should do info.init = (uintptr_t)(void*)(NULL); . You could additionally provide the reader with additional help by specifying the type of pointer to be converted by uintptr_t , not void* . There are very few good reasons to store uintptr_t , so hints about what happens can help the reader.

Note that there is a guarantee in the standard that a constant null value converted to a pointer type is a null pointer. This does not mean that a null undefined expression converted to a pointer type is a null pointer. Also, this does not mean that the null pointer, converted to an integer type, is 0. These last two things happen in most implementations (including all "modern" ones).

+5
source

NULL is a built-in constant with a value corresponding to a null pointer on your system. This is perfectly true for assigning a constant int value of the same size (or larger) than a pointer to your system.

+3
source

I would vote for the cast, since assigning pointers to integers is usually not allowed and will generate a warning. It is a very good idea to use intptr_t , of course, since it is big enough.

0
source

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


All Articles