473,396 Members | 1,924 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How do you track type information when converting int* to void*

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?
Sep 18 '08 #1
5 1909
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


Sep 18 '08 #2
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
Sep 18 '08 #3
In article
<83**********************************@l43g2000hsh. googlegroups.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*.
Sep 18 '08 #4
blargg wrote:
In article
<83**********************************@l43g2000hsh. googlegroups.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
Sep 19 '08 #5
On 18 Sep, 13:01, Richard Heathfield <r...@see.sig.invalidwrote:
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

Sep 19 '08 #6

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

Similar topics

14
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 =...
51
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...
6
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 ...
10
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...
16
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...
12
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...
4
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...
20
by: pinkfloydhomer | last post by:
Is it well-defined and portable to do something like: typedef struct { int type; char c; } S1; typedef struct {
2
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.