Keith made the following comment
" You can convert an int*, or an int**, or an int***, or ... to void*
without loss of information -- except that you lose the type
information, which you have to carefully keep track of yourself. "
How would you track the type infornation? 5 1927
On 18 Sep, 12:27, Chad <cdal...@gmail. comwrote:
Keith made the following comment
" You can convert an int*, or an int**, or an int***, or ... to void*
without loss of information -- except that you lose the type
information, which you have to carefully keep track of yourself. "
How would you track the type infornation?
why are you casting a whatever-type to a void*?
If you need the type information why not keep the original type?
I suppose some sort of generic container might be a reason
to do this.
Basically you need some sort of tag
enum Tag {INT, FLOAT, STRING};
struct Node
{
enum Tag tag;
void *data;
};
store data like this
struct Node nod;
int i;
nod.tag = INT;
nod.data = &i;
add (&handle, &nod);
casting it back when you retrieve it.
You might consider a union instead. Or a redesign.
--
Nick Keighley
Nick Keighley said:
On 18 Sep, 12:27, Chad <cdal...@gmail. comwrote:
>Keith made the following comment
>" You can convert an int*, or an int**, or an int***, or ... to void* without loss of information -- except that you lose the type information, which you have to carefully keep track of yourself. "
How would you track the type infornation?
why are you casting a whatever-type to a void*?
Where did he say he was casting?
If you need the type information why not keep the original type?
Because you're writing generic code?
I suppose some sort of generic container might be a reason
to do this.
Ha - yes, quite so.
Basically you need some sort of tag
enum Tag {INT, FLOAT, STRING};
or a function whose address you hand to the container, and which knows how
to deal with the type.
>
struct Node
{
enum Tag tag;
void *data;
};
store data like this
struct Node nod;
int i;
nod.tag = INT;
nod.data = &i;
add (&handle, &nod);
casting it back when you retrieve it.
Why?
--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
In article
<83************ *************** *******@l43g200 0hsh.googlegrou ps.com>,
Chad <cd*****@gmail. comwrote:
Keith made the following comment
" You can convert an int*, or an int**, or an int***, or ... to void*
without loss of information -- except that you lose the type
information, which you have to carefully keep track of yourself. "
How would you track the type infornation?
In many cases you know the type that the void* points to, so you can
just cast back to that. Why use void* at all, you ask? Often a function
doesn't need to know anything about the type pointed to, so using void*
allows one function to work on many types. The best example is probably
qsort(), where the comparison function gets a void*.
blargg wrote:
In article
<83************ *************** *******@l43g200 0hsh.googlegrou ps.com>,
Chad <cd*****@gmail. comwrote:
>Keith made the following comment
" You can convert an int*, or an int**, or an int***, or ... to void* without loss of information -- except that you lose the type information, which you have to carefully keep track of yourself. "
How would you track the type infornation?
In many cases you know the type that the void* points to, so you can
just cast back to that. Why use void* at all, you ask? Often a function
doesn't need to know anything about the type pointed to, so using void*
allows one function to work on many types. The best example is probably
qsort(), where the comparison function gets a void*.
... and where the true type of what the void* points to
is "tracked" by the comparison function: The caller supplies
a pointer to a function that "knows" what's on the other end
of the pointer, and thus specifies its type.
One other aspect of the type, its size, is provided to
qsort() as an explicit argument.
--
Eric Sosman es*****@ieee-dot-org.invalid
On 18 Sep, 13:01, Richard Heathfield <r...@see.sig.i nvalidwrote:
Nick Keighleysaid:
On 18 Sep, 12:27, Chad <cdal...@gmail. comwrote:
Keith made the following comment
" You can convert an int*, or an int**, or an int***, or ... to void*
without loss of information -- except that you lose the type
information, which you have to carefully keep track of yourself. "
How would you track the type infornation?
why are you casting a whatever-type to a void*?
Where did he say he was casting?
a moment of insanity on my part
If you need the type information why not keep the original type?
Because you're writing generic code?
ok, but does he need generic code? Sometimes things
get stuffed into void* for no good reason that I can see.
But I should assume Good Faith; that the OP is not in fact
crazy.
I suppose some sort of generic container might be a reason
to do this.
Ha - yes, quite so.
Basically you need some sort of tag
casting it back when you retrieve it.
Why?
a *second* moment of insanity
--
Nick Keighley
"Almost every species in the universe has an irrational fear of the
dark.
But they're wrong- cos it's not irrational. It's Vashta Nerada."
The Doctor This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: sathya_me |
last post by:
Dear clc,
I have a variable void *a; Since variable "a" can be assigned (point
to) any type and also
any type can be assigned to "a" (i.e means "a" = any typed variable;
any typed variable = "a".
Considering the above I have a function, which is declared and defined
to take any type
of parameter with void*
return-type foo (void *a);
In the processes of assignment of value to the variable "a" I want to
|
by: jacob navia |
last post by:
I would like to add at the beginning of the C tutorial I am writing
a short blurb about what "types" are. I came up with the following text.
Please can you comment?
Did I miss something?
Is there something wrong in there?
--------------------------------------------------------------------
Types
A type is a definition for a sequence of storage bits. It gives the
meaning of the data stored in memory. If we say that the object a is an
|
by: S.Tobias |
last post by:
I'm trying to understand how structure type completion works.
# A structure or union type of unknown
# content (as described in 6.7.2.3) is an incomplete type. It
# is completed, for all declarations of that type, by
^^^
# declaring the same structure or union tag with its defining
# content later in the same scope.
^^^^^
(6.2.5#23)
|
by: lovecreatesbeauty |
last post by:
Why (type*)pointer isn't equal to *(type**)pointer,
In the code snippet, it shows that:
(int *) == (int **) ,
(int *) != (*(int **)) .
Does type-casting change the address? or doesn't type-casting do
anything?
1 int main(void)
|
by: Abhishek |
last post by:
why do I see that in most C programs, pointers in functions are
accepted as:
int func(int i,(void *)p) where p is a pointer or an address which is
passed from the place where it is called. what do you mean by pointing
to a void and why is it done?
Aso, what happens when we typecast a pointer to another type. say for
example int *i=(char *)p;
under different situations? I am kind of confused..can anybody clear
this confusion by clearly...
| |
by: Abhishek |
last post by:
now suppose I have declared an integer value inside a function as
int x;
now if the return type of the function is of type (void *) then can I
write
return((void *)x) in side the function?
I came across this in a document on multithreading in C..I can post the
exact portion of code which works correctly with such an assignment if
you people want.
Please clarify my doubt.I hope I have conveyed it properly.
|
by: Frank-René Schäfer |
last post by:
-- A class needs to have N members according to N types
mentioned in a typelist (possibly with one type occuring more than
once).
-- The classes should be generated **avoiding** multiple inheritance
(avoiding prosperation of virtual func tables).
-- At the same time, a class taking N types shall contain a virtual
member function that calls a function according to the number
of arguments
That means, something like:
|
by: pinkfloydhomer |
last post by:
Is it well-defined and portable to do something like:
typedef struct
{
int type;
char c;
} S1;
typedef struct
{
|
by: CoreyWhite |
last post by:
Problem:
You have numbers in string format, but you need to convert them to a
numeric type, such as an int or float.
Solution:
You can do this with the standard library functions. The functions
strtol, strtod, and strtoul, defined in <cstdlib>, convert a null-
terminated character string to a long int, double, or unsigned long.
You can use them to convert numeric strings of any base to a numeric
|
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...
|
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,...
| |
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...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |