473,657 Members | 2,505 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

void vs void* (philosophical question)

According to the standard (ISO C99 draft WG14/N1124), void is the
incomplete type that cannot be completed and comprises the empty set of
values. Since it declares the absense of a value can it be considered a
data type?

Regarding void*, is it just a simple reuse of the same keyword (void) or
they have a closer relationship? I could think of only one - a pointer
to an incomplete type. However, if one could say that void is the
absense of a value, how can we have a pointer to something that does not
exist?
--
one's freedom stops where others' begin

Giannis Papadopoulos
Computer and Communications Engineering dept. (CCED)
University of Thessaly
http://dop.freegr.net/
Jun 30 '06 #1
18 2128
Giannis Papadopoulos wrote:
According to the standard (ISO C99 draft WG14/N1124), void is the
incomplete type that cannot be completed and comprises the empty set of
values. Since it declares the absense of a value can it be considered a
data type?

Regarding void*, is it just a simple reuse of the same keyword (void) or
they have a closer relationship? I could think of only one - a pointer
to an incomplete type. However, if one could say that void is the
absense of a value, how can we have a pointer to something that does not
exist?


Here's my best explanation:

A void * is a pointer to (just about) anything. If you dereference a
void *, you might be referring to a 32 bit integer or a 128 bit
floating point value. You might be referring to a structure or a
union. You might be referring to damn near __anything__. In C, you do
the bookkeeping, not the compiler (usually).

Since a void * may point to just about anything, it's safest for the
compiler to treat it as if it points to __nothing in particular__. You
must convert a void * to another pointer type before using it:

#include <stdio.h>

int main(void)
{
int a = 42;
void *p = &a;
printf("%d\n", *(int *) p);
return 0;
}

[mark@icepick]$ gcc -ansi -pedantic -Wall -O2 -o foo foo.c
[mark@icepick]$ ./foo
42
Mark F. Haigh
mf*****@sbcglob al.net

Jun 30 '06 #2
Mark F. Haigh wrote:
Giannis Papadopoulos wrote:
According to the standard (ISO C99 draft WG14/N1124), void is the
incomplete type that cannot be completed and comprises the empty set of
values. Since it declares the absense of a value can it be considered a
data type?

Regarding void*, is it just a simple reuse of the same keyword (void) or
they have a closer relationship? I could think of only one - a pointer
to an incomplete type. However, if one could say that void is the
absense of a value, how can we have a pointer to something that does not
exist?


Here's my best explanation:

A void * is a pointer to (just about) anything. If you dereference a
void *, you might be referring to a 32 bit integer or a 128 bit
floating point value. You might be referring to a structure or a
union. You might be referring to damn near __anything__. In C, you do
the bookkeeping, not the compiler (usually).

Since a void * may point to just about anything, it's safest for the
compiler to treat it as if it points to __nothing in particular__. You
must convert a void * to another pointer type before using it:

#include <stdio.h>

int main(void)
{
int a = 42;
void *p = &a;
printf("%d\n", *(int *) p);
return 0;
}

[mark@icepick]$ gcc -ansi -pedantic -Wall -O2 -o foo foo.c
[mark@icepick]$ ./foo
42
Mark F. Haigh
mf*****@sbcglob al.net


I do not want to know about the usage of void and void*. I want to know
if they have some common meaning, apart from the first 4 letters.

Thanks anyway however.

--
one's freedom stops where others' begin

Giannis Papadopoulos
Computer and Communications Engineering dept. (CCED)
University of Thessaly
http://dop.freegr.net/
Jun 30 '06 #3
Giannis Papadopoulos said:
According to the standard (ISO C99 draft WG14/N1124), void is the
incomplete type that cannot be completed and comprises the empty set of
values. Since it declares the absense of a value can it be considered a
data type?

Regarding void*, is it just a simple reuse of the same keyword (void)
Yes.
or they have a closer relationship? I could think of only one - a pointer
to an incomplete type.
Apart from that, they pretty much mean *opposite* things. void means
"nothing", and void * means "pointer to anything" (in the "whaddya mean,
what am I pointing at? How should I know?" sense).
However, if one could say that void is the
absense of a value, how can we have a pointer to something that does not
exist?


If void means "absence of value", surely void * means "a pointer which you
can't dereference because to do so would yield the absence of a value".

But yes, it's sticky-icky. I'd have bitten the bullet and introduced a new
keyword, e.g. object, which I'd have used to replace void in pointer
contexts. Thus: object *malloc(size_t) , etc.

You can do this easily enough in your own code with:

#define object void /* possible; just not very wise */

But I am NOT advocating this! :-) If the language supported it, that would
be different, but it doesn't.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jun 30 '06 #4
Giannis Papadopoulos wrote:
Mark F. Haigh wrote:
Giannis Papadopoulos wrote:
According to the standard (ISO C99 draft WG14/N1124), void is the
incomplete type that cannot be completed and comprises the empty set of
values. Since it declares the absense of a value can it be considered a
data type?

Regarding void*, is it just a simple reuse of the same keyword (void) or
they have a closer relationship? I could think of only one - a pointer
to an incomplete type. However, if one could say that void is the
absense of a value, how can we have a pointer to something that does not
exist?


I do not want to know about the usage of void and void*. I want to know
if they have some common meaning, apart from the first 4 letters.


My 2 pence: void is an incomplete type, a pointer to the incomplete
type must be able to point to any possible object, therefore void *
must be the generic pointer.

Consider analogously the pointer to an incomplete struct.

--
imalone
Jun 30 '06 #5
On 2006-06-30, Giannis Papadopoulos <ip******@inf.u th.gr> wrote:
Mark F. Haigh wrote:
Giannis Papadopoulos wrote:
According to the standard (ISO C99 draft WG14/N1124), void is the
incomplete type that cannot be completed and comprises the empty set of
values. Since it declares the absense of a value can it be considered a
data type?

Regarding void*, is it just a simple reuse of the same keyword (void) or
they have a closer relationship? I could think of only one - a pointer
to an incomplete type. However, if one could say that void is the
absense of a value, how can we have a pointer to something that does not
exist?

Here's my best explanation:

A void * is a pointer to (just about) anything. If you dereference a
void *, you might be referring to a 32 bit integer or a 128 bit
floating point value. You might be referring to a structure or a
union. You might be referring to damn near __anything__. In C, you do
the bookkeeping, not the compiler (usually).

Since a void * may point to just about anything, it's safest for the
compiler to treat it as if it points to __nothing in particular__. You
must convert a void * to another pointer type before using it:


[snip]
I do not want to know about the usage of void and void*. I want to know
if they have some common meaning, apart from the first 4 letters.

Thanks anyway however.


I think Mark F. Haigh has elucidated this a bit: "void" means "nothing",
"void *" means "pointer to anything".

Anything != nothing, although anything ~= "nothing in particular". So
they're not the same void. But closely related ideas, so we use the same
keyword.

void * is not quite consistent with void as follows:

int *p;

can be read as "p is a pointer and *p is an int". But:

void *p;

"p is a pointer [true] and *p is a void [false]"
Jun 30 '06 #6
Giannis Papadopoulos wrote:

According to the standard (ISO C99 draft WG14/N1124),
void is the incomplete type that cannot be completed
and comprises the empty set of values.
Since it declares the absense of a value can it be considered a
data type?


No.

sizeof(void) is undefined.
You can't declare an object of type void.
You can't have an array of type void elements.
An expression of type void can neither be
the left nor right operand of the assignment operator.

--
pete
Jun 30 '06 #7
Ben C wrote:
void * is not quite consistent with void as follows:

int *p;

can be read as "p is a pointer and *p is an int". But:

void *p;

"p is a pointer [true] and *p is a void [false]"


[true] and [true], actually. *p is a valid expression of type void, and
&*p evaluates to p. (Of course, it's pointless to do this except for
very special cases.)

Jun 30 '06 #8
Giannis Papadopoulos schrieb:
According to the standard (ISO C99 draft WG14/N1124), void is the
incomplete type that cannot be completed and comprises the empty set of
values. Since it declares the absense of a value can it be considered a
data type?

Regarding void*, is it just a simple reuse of the same keyword (void) or
they have a closer relationship? I could think of only one - a pointer
to an incomplete type. However, if one could say that void is the
absense of a value, how can we have a pointer to something that does not
exist?


Its logical if you think about it in terms of set theorie.

Think of two structs:

struct person
{
char* name;
};
struct worker
{
char* name;
char* job;
};

A pointer person* can point to a struct person and to a struct worker,
so it is a pointer to "at least a person".

A void* is a pointer to "at least nothing", so it can't point to
effectively everything.

Thomas
Jun 30 '06 #9
In article <e8**********@v olcano1.grnet.g r>
Giannis Papadopoulos <ip******@inf.u th.gr> wrote:
According to the standard (ISO C99 draft WG14/N1124), void is the
incomplete type that cannot be completed and comprises the empty set of
values. Since it declares the absense of a value can it be considered a
data type?
In a larger, "not Standard C" sense, yes. In Standard C, however,
types are partitioned into "object types", "function types", and
"incomplete types". (The Standard does not define "data type" but
logically it would appear to map to "object type". Interestingly,
my C99 draft *uses* the phrase "data types" in the description of
<wchar.h>, in paragraph 1 of section 7.19.1. It is clear enough
that it does not mean "incomplete types" here. Perhaps the phrasing
was changed in the final standard, though.)

(If I were in charge of things, I would probably make "void" an
ordinary object type whose size is zero, so that sizeof(void) ==
0; its value, upon conversion to any other scalar type, would be
zero. Thus:

void v, *pv = &v;
printf("%d %d\n", v, *pv);

would print "0 0\n". :-) Of course, I would also add zero-sized
arrays:

int a[0], b;

with the allowance -- but not requirement -- that &a[0] == &b. Then
I might also remove the "flexible array member" and simply bless the
Struct Hack.)
Regarding void*, is it just a simple reuse of the same keyword (void) or
they have a closer relationship?


As most others have said, it is just reuse of the keyword.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Jun 30 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1160
by: Maurice LING | last post by:
Hi, Just a philosophical check here. When a program is distributed, is it more appropriate to provide as much of the required 3rd party libraries, like SOAPpy, PLY etc etc, in the distribution itself or it is the installer's onus to get that part done? Cheers Maurice
14
1555
by: dracolytch | last post by:
Alright, this is a controvercial question for my fellow gurus: Why do we bother with Object-Oriented programming in a web environment? The web, by nature, is a stateless environment. Web-based applications are, on the whole, fairly uncomplicated things. Objects are, generally, conceptually thought of as entities that have a lifespan. Having an object survive for longer than the execution of a single page can be a pain. Having an object...
8
2004
by: Steve Jorgensen | last post by:
Hi folks, I'm posting this message because it's an issue I come up against relatively often, but I can't find any writings on the subject, and I haven't been able to figure out even what key words one would use to look for it. First, in broad philosophical terms, code actually -is- data. Code is the data that's fed into a compiler, interpreter, or microprocessor that tells it what to do. Code execution is, then, just a form another...
4
2351
by: Madhav | last post by:
Hi all, I am a newbie in c++. I want to know what is the philosophical reason behind the existence of friend functions. I thought giving access to private data to a function which is not a member of the class is a violation of encapsulation. Thanks, Madhav.
27
8949
by: Erik de Castro Lopo | last post by:
Hi all, The GNU C compiler allows a void pointer to be incremented and the behaviour is equivalent to incrementing a char pointer. Is this legal C99 or is this a GNU C extention? Thanks in advance. Erik
6
2246
by: Juha Nieminen | last post by:
Whenever one sees example C++ code basically anywhere, be it in a book, in a tutorial in the internet, in an online forum or whatever, I would estimate that at least in 99% of cases one sees the use of "using namespace std;" to get rid of that namespace. In fact, "using namespace ..." is very popular with all documentation and example code of most C++ libraries out there which use their own namespace. This raises the question why use...
4
3526
by: crc294 | last post by:
Hi all, in order to experiment with AJAX, I created a site called Rob's Easy Budget Tool (http://budget.edgerob.net). Check it out if ya like. I found its functionality useful so I made it available to all as well as source code & resources used. In the course of making this site, I ran into a question. I exclusively use responseText to receive PHP backend responses. However, I ran into some limitations with this. If I wanted to update two...
0
8420
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8324
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8842
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8740
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8516
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7353
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4330
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1733
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.