null-pointer

Does forming a reference to an object constitute access?

微笑、不失礼 提交于 2021-02-07 20:30:49
问题 Does forming a reference to an object constitute access? Here's what GCC and Clang currently do: void test(int const volatile* ptr) noexcept { *ptr; // movl (%rdi), eax // Reads *ptr [[maybe_unused]] int const volatile& ref = *ptr; // Does not read *ptr } My question is specifically about the statement [[maybe_unused]] int const volatile& ref = *ptr; According to the abstract machine, does this read the value of the object pointed to by ptr ? Would this statement, in isolation, be undefined

Can we subtract NULL pointers?

穿精又带淫゛_ 提交于 2021-01-27 05:29:34
问题 Since pointer arithmetic is defined within the same array I'm in doubt if we can subtract NULL from another NULL . I'm concerned about the implementation of: //first and second can both either be from the same array //or be both NULL prtdiff_t sub(void *first, void *second){ //Do I really need this condition? if(!first && !second) return (ptrdiff_t) 0; return second - first; } 回答1: Subtracting two NULL pointers is not allowed. Section 6.5.6p9 of the C standard states: When two pointers are

Three questions: Is NULL - NULL defined? Is (uintptr_t)NULL - (uintptr_t)NULL defined? [duplicate]

↘锁芯ラ 提交于 2021-01-05 06:00:46
问题 This question already has answers here : Is the behavior of subtracting two NULL pointers defined? (4 answers) Closed last month . 1.Is NULL - NULL defined.? Is (char *)NULL - (char *)NULL defined.? Is (uintptr_t)NULL - (uintptr_t)NULL defined? I know that it works in all used by me implementations. But how does it look like from the standard point of view? I cant find the clear answer. Edit: From the dupe I assume that the question one answer is: YES. What about the second and third

Is it safe to assume that the NULL constant is zero?

二次信任 提交于 2020-04-09 21:00:44
问题 The book Understanding and Using C Pointers , by Richard Reese says: The null concept is an abstraction supported by the null pointer constant. This constant may or may not be a constant zero. A C programmer need not be concerned with their actual internal representation. My question is, since "this constant may or may not be a constant zero," is it safe for me to do things like the below in my code: int *ptr = NULL; // Some code which probably sets ptr to a valid memory address if(!ptr) {

Is it safe to assume that the NULL constant is zero?

荒凉一梦 提交于 2020-04-09 20:59:39
问题 The book Understanding and Using C Pointers , by Richard Reese says: The null concept is an abstraction supported by the null pointer constant. This constant may or may not be a constant zero. A C programmer need not be concerned with their actual internal representation. My question is, since "this constant may or may not be a constant zero," is it safe for me to do things like the below in my code: int *ptr = NULL; // Some code which probably sets ptr to a valid memory address if(!ptr) {

Null pointer test performance

℡╲_俬逩灬. 提交于 2020-01-24 02:46:08
问题 What is the performance of testing whether a reference-type variable in C# is a null-pointer (like if (x == null) ...) compared to testing for an integer being smaller than zero or even a bool being false? Are there other issues know regarding such null-pointer tests , e.g. is garbadge produced ? I do hundred of these tests for every frame of a game and I was wondering if these could cause problems or could be implemented more efficiently? 回答1: Nullity tests are likely to be equivalent to

About the non-nullable types debate

允我心安 提交于 2019-12-29 08:35:39
问题 I keep hearing people talk about how non-nullable reference types would solve so many bugs and make programming so much easier. Even the creator of null calls it his billion dollar mistake, and Spec# has introduced non-nullable types to combat this problem. EDIT: Ignore my comment about Spec#. I misunderstood how it works. EDIT 2: I must be talking to the wrong people, I was really hoping for somebody to argue with :-) So I would guess, being in the minority, that I'm wrong, but I can't

When are you able to return NULL as the returning value of a C function?

戏子无情 提交于 2019-12-20 06:36:45
问题 I was wondering if you could tell me when you are able to return NULL , as the result of a function in C. For instance int lenght() can't return NULL because is is expecting an int in the return statement. But the function struct node* find(int key) , when working with linked lists allows me to return NULL. 回答1: NULL is a pointer value - or rather a null-pointer value. NULL means that the function can't find where your pointer should point to - for example if you want to open a file, but it

Is apparent NULL pointer dereference in C actually pointer arithmetic?

﹥>﹥吖頭↗ 提交于 2019-12-18 05:01:54
问题 I've got this piece of code. It appears to dereference a null pointer here, but then bitwise-ANDs the result with unsigned int . I really don't understand the whole part. What is it intended to do? Is this a form of pointer arithmetic? struct hi { long a; int b; long c; }; int main() { struct hi ob={3,4,5}; struct hi *ptr=&ob; int num= (unsigned int) & (((struct hi *)0)->b); printf("%d",num); printf("%d",*(int *)((char *)ptr + (unsigned int) & (((struct hi *)0)->b))); } The output I get is 44

Is apparent NULL pointer dereference in C actually pointer arithmetic?

拜拜、爱过 提交于 2019-12-18 05:01:43
问题 I've got this piece of code. It appears to dereference a null pointer here, but then bitwise-ANDs the result with unsigned int . I really don't understand the whole part. What is it intended to do? Is this a form of pointer arithmetic? struct hi { long a; int b; long c; }; int main() { struct hi ob={3,4,5}; struct hi *ptr=&ob; int num= (unsigned int) & (((struct hi *)0)->b); printf("%d",num); printf("%d",*(int *)((char *)ptr + (unsigned int) & (((struct hi *)0)->b))); } The output I get is 44