Questions tagged [void]

An incomplete type used as syntactic place-holder for the return type of a method/function when no value is returned.

Programming languages derived from C or Algol68, like C++, C#, Java, etc., may define the return type of methods/functions as void when the method/function does not return a value, but simply completes execution.

An example of its use in Java, compared to a non-void method, is:

int nonVoidMethod {
    // do something
    return 0; // return a value to the caller
}

void voidMethod {
    // do something
    return; // no value allowed to be returned from a void method
    // a "return" statement is not required
}

Although void is used as a type, it's an incomplete type:

  • In some languages, such as Java and Algol68, void is only a keyword used as a return type. It is not considered as a valid type in other language constructs.

  • Other languages, like C and C++, consistently define void as a type with an empty set of values. This allows its use for example in compound type constructs (void pointers). But no void objects can be instantiated.

1913 questions
39
votes
4 answers

What does `((void (*)())0x1000)();` mean?

Here is a code that purpose is to set the program counter to jump to address 0x1000. I know what it does but I don't understand how. It is related to my lack of C language knowledge. May be you can enlighten me. Here is the statement/function (I…
Radoslaw Krasimirow
  • 1,833
  • 2
  • 18
  • 28
39
votes
9 answers

Why does C# not allow me to call a void method as part of the return statement?

I am curious if there is a legitimate reason as to why C# does not support calling a void method as part of the return statement when the calling method's return type is also void. public void MethodA() { return; } public void MethodB() { …
Matt Beckman
  • 5,022
  • 4
  • 29
  • 42
39
votes
4 answers

What is the purpose of List?

I didn't even know this was doable, but I saw while perusing some code online a method with a signature like this: public List read( ... ) ... What? Is there ever a reason to do this? What could this List even hold? As far as I was aware,…
asteri
  • 11,402
  • 13
  • 60
  • 84
37
votes
2 answers

Casting a void pointer to a struct

I started feeling comfortable with C and then I ran into type casting. If I have the following defined in an *.h file struct data { int value; char *label; }; and this in another *.h file # define TYPE void* How do I cast the void…
user1852050
  • 655
  • 2
  • 13
  • 17
36
votes
4 answers

How to test if a void async function was successful with jest?

How do you concisely test if a void async function executed successfully with jest? I'm using TypeScript. // foo.ts export class Foo { public async bar(): Promise { await someAsync(); } } How to test that new Foo().bar() does not…
adanilev
  • 3,008
  • 3
  • 15
  • 20
35
votes
2 answers

How does shared_ptr know which destructor to use?

I wrote the following code to see how a shared_ptr would behave when it is the last reference to a shared_ptr and is itself destroyed. #include #include #include using namespace std; struct Thing{ …
Anne Quinn
  • 12,609
  • 8
  • 54
  • 101
34
votes
5 answers

Switch expression with void return type

Is there any way to force an exhaustive check of all enum values when the switch branches call methods with void return type? It's quite ugly to hard-code a yield just to coax the compiler to demand exhaustiveness. This is my current pattern (the…
Arboreal Shark
  • 2,221
  • 3
  • 17
  • 12
33
votes
4 answers

Can a C++ function be declared such that the return value cannot be ignored?

I'm trying to determine whether a C++ function can be declared in such a way that the return value cannot be ignored (ideally detected at compile time). I tried to declare a class with a private (or in C++11, deleted) operator void() to try to catch…
Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
31
votes
2 answers

What does the void() in decltype(void()) mean exactly?

This is a follow-up of this question, more precisely of the comments of this answer. What does the void() in decltype(void()) represent exactly? Does it represent a function type, an expression or whatever?
skypjack
  • 49,335
  • 19
  • 95
  • 187
31
votes
5 answers

When is casting void pointer needed in C?

I've been looking at Advanced Linux Programming by Mitchell, Oldham and Samuel. I've seen in the section on pthreads something about void pointers and casting that confuses me. Passing an argument to pthread_create(), they don't cast the pointer to…
Amoeba
  • 1,573
  • 4
  • 19
  • 25
31
votes
6 answers

How to test void method with Junit testing tools?

I just happen to implement a method void followlink(obj page,obj link) which simply adds page and link to queue. I have unsuccessfully tried to test this kind of method. All I want is to test that in the queue contains page and link received from…
user152462
  • 311
  • 1
  • 3
  • 3
30
votes
6 answers

what does it mean to convert int to void* or vice versa?

What does it mean to convert an integer value to a void* or viceversa from a memory point of view? My understanding is void* is an address to a block of memory of unspecified length. This seems to be something like comparing apple with oranges. int…
Mathai
  • 839
  • 1
  • 12
  • 24
29
votes
2 answers

What does Void return type mean in Kotlin

I tried to create function without returning value in Kotlin. And I wrote a function like in Java but with Kotlin syntax fun hello(name: String): Void { println("Hello $name"); } And I've got an error Error:A 'return' expression required in a…
Mara
  • 2,947
  • 2
  • 12
  • 17
29
votes
5 answers

Object[] cannot be cast to Void[] in AsyncTask

I'm getting this error inside an extended asynctask, but i'm really sure that Object[] IS a Void[]. This is my custom AsyncTask: public abstract class RepeatableAsyncTask extends AsyncTask { private static final String TAG =…
Luca Vitucci
  • 3,674
  • 4
  • 36
  • 60
29
votes
1 answer

C++ typecast: cast a pointer from void pointer to class pointer

How to cast a pointer to void object to class object?
Naveen
  • 297
  • 1
  • 3
  • 6