Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What data type does memory see when I use void?

Tags:

c++

c#

When I create a method of type int the compiler reserves X number of bits in memory. So how does the see a void type? How many bits/bytes does a void type take up?

like image 955
Kredns Avatar asked Jul 18 '26 00:07

Kredns


2 Answers

the void type does not take any bits. you cannot declare a variable of type void. this:

void a;

causes a compilation error.
void is just a place holder that means "nothing" a function that returns void returns nothing and a function that takes void as an argument, takes no arguments.

You can however declare a variable of type void*:

void* a;

This simply declares a pointer that can point to anything what so ever. as any pointer, it takes the size of a pointer type, i.e. sizeof(void*) which typically equals 4 in 32 bit systems.

like image 199
shoosh Avatar answered Jul 19 '26 13:07

shoosh


I think the assumption you made about how a compiler stores return values might be too generic. What happens with return values is determined by the language, the type, and the hardware.

There might be a 'return register' that could hold the value of the return, so no memory would be taken up. The 'return register' could also just point to some allocated in memory based on the type being returned.

I don't see why a function returning void would ever have allocated memory for the return value.

like image 20
MStodd Avatar answered Jul 19 '26 15:07

MStodd



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!