Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does (void *)1 mean?

I'm reading the code of ROS.

In the file ros_comm/roscpp/include/ros/subscriber.h, I see such a piece of code:

operator void*() const { return (impl_ && impl_->isValid()) ? (void*)1 : (void*)0; }

Well, (void *)0 can be regarded as NULL in C, but what does (void *)1 mean?

If a class Foo contains this function, it means that we can code like this:

Foo foo;
void *ptr = foo;

Right? So does it mean that void *ptr = (void *)1 is possible? What does this mean?

like image 299
Yves Avatar asked Jul 10 '19 02:07

Yves


People also ask

What does void *) mean in C?

The void pointer in C is a pointer which is not associated with any data types. It points to some data location in the storage means points to the address of variables. It is also called general purpose pointer. In C, malloc() and calloc() functions return void * or generic pointers.

What is a void *) 0?

JavaScript void 0 means returning undefined (void) as a primitive value. You might come across the term “JavaScript:void(0)” while going through HTML documents. It is used to prevent any side effects caused while inserting an expression in a web page.

What does void * p mean?

void *p = &a; // void pointer holds address of int 'a' p = &b; // void pointer holds address of char 'b' Advantages of void pointers: 1) malloc() and calloc() return void * type and this allows these functions to be used to allocate memory of any data type (just because of void *)

What does “void” mean?

It means “no type”, “no value” or “no parameters”, depending on the context. We use it to indicate that: a pointer does not have a specific type and could point to different types. This is probably the most used context of the void keyword.

Why do we use the void keyword in C++?

We use it to indicate that: a pointer does not have a specific type and could point to different types. This is probably the most used context of the void keyword. Here we use it as a return type of a function. Such function does not return a value. However, it still can use the return statement to return control to the caller at any given time.

What does it mean to cast 1 to a void *?

It casts 1 to a pointer and 0 to a pointer. That is all. – Brandon Jul 10 '19 at 2:25 1 That's a c-style cast. Thus, it's casting 1 to a void *.

Why do we prefer the first definition of a void pointer?

We could have created the function without the void and it will do its job the same way. Then why do we prefer the first definition? Because it is safer. In both cases we don’t use any parameters. But if, by mistake, we call it with arguments we want to be warned that they will not be used. What is void pointer?


Video Answer


1 Answers

This is an old trick to avoid problems with implicit conversions to bool from before explicit contextual conversions were introduced in C++11. It's intended to be used to check validity:

Subscriber my_subscriber = someFunction();
if (!my_subscriber) {
    // error case
}

The important point is that no built-in conversion exists from void* to integer types, but one does exist from bool to integer types. At the same time, a built-in conversion from void* to bool exists. That means that if you define an implicit conversion to bool, then the following is surprisingly valid:

void my_func(int i);

void another_func() {
    Subscriber sub = something();
    my_func(sub);
}

Defining a conversion to void* avoids that issue.


These days that trick is obsolete though. C++11 introduced explicit conversions. explicit conversions to bool are considered in the conditions of if and loops, but aren't considered in other problematic cases. That means that these days that conversion should be written as:

explicit operator bool() const { return impl_ && impl_->isValid(); }
like image 83
Miles Budnek Avatar answered Oct 19 '22 18:10

Miles Budnek