473,785 Members | 2,218 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

void* operations

Hi,
I haven't done a lot of C++ programming (done a lot of it in C) and the
reason why I'm considering switching now is also the question I'm
posting here.

I have some library functions that return to me a void* to data that
has been specifed by the user. There are also functions that return an
enum which contains information about the data type of this data. So
what I've done in the past is made a switch case statement based on the
data type enum and then explicitly re-cast the void* to the appropriate
data type.

Now I have a case where I have two values and each of them could be any
data type. Lets say I have to multiply them. So if I was to use my
usual method I'd have a switch case statement with numDataType(C)2
(combination) cases. Does C++ have a better way of extracting data from
void*?

Thanks for your help,
Ashish.

Feb 17 '06
21 2344
Praetorian wrote:
[..]
Victor, the void* returned to me does point to a successfully
constructed object in memory, having the size of whatever the data
type enum; I can simply re-cast to the data type pointer and access it
without worrying about any kind of memory corruption (thank God for
that). Let me try and illustrate my problem with explicit casting
using an example:

Let's say I have 2 values that I need to multiply and each value may
be an int16 or an int32. I have void pointers v1 and v2 to each. So
the way I see what I need to do is something like:

if(d1 == INT16 && d2 == INT16)
{
v1int16 = (int16 *)v1;
v2int16 = (int16 *)v2;
You probably meant

v1int16 = *(int16*)v1;

and so on...
output = v1*v2;
What type is 'output'?
}
else if(d1 == INT16 && d2 == INT32)
{
v1int16 = (int16 *)v1;
v2int32 = (int32 *)v2;
output = v1*v2;
}
else if(d1 == INT32 && d2 == INT16)
{ .....}
else
{......}

Now this becomes rather tedious because I need to support 10 different
data types and I have to operate on 3 values instead of 2; basically a
switch case or if else construct from hell as someone put it in an
earlier post.

Although the virtual function method looks promising doesn't that
still mean I need to have a function for each combination of data
types?
You bet.
I was looking into templates but even with them the problem is
that my knowledge of the data type lies in an enum value and I don't
know how to incorporate this in a template; maybe a template which
does the final operation on pointers and virtual functions to
'condition' the values based on their data types, if any conditioning
is needed. These functions would return pointers that can be used by
the template.
Templates are not going to help you since your types are not known
until run-time. Still, even then they are not true C++ types in the
run-time sense (polymorphic types). They are just chunks of memory
whose meaning is expressed in some other values. Even though they
have been constructed, to use them you need to provide some switch
statement, there is no escaping that.

Now, you _could_ define your base class for your operations and then
derive your 10 types from it. Then all the operations just need to
be implemented as a case of double dispatch (look it up).

An alternative would be to extract those values into another type,
some kind of common denominator. If they are all integrals, use
'long' or 'unsigned long' to represent the values. If that's not
enough, use implementation-specific larger value (like 'int64_t' in
Visual C++, for example).
I'm just throwing out ideas here and forgive me if I've managed to
sound stupid with my solution. You guys should be able to suggest
something that works for me.


Hey, at the end of the day, we're not going to solve your problem.
You have to do it yourself. Bite the bullet and just code all those
1000 possibilities.

V
--
Please remove capital As from my address when replying by mail
Feb 18 '06 #11

Victor Bazarov wrote:
Praetorian wrote:
[..]
Victor, the void* returned to me does point to a successfully
constructed object in memory, having the size of whatever the data
type enum; I can simply re-cast to the data type pointer and access it
without worrying about any kind of memory corruption (thank God for
that). Let me try and illustrate my problem with explicit casting
using an example:

Let's say I have 2 values that I need to multiply and each value may
be an int16 or an int32. I have void pointers v1 and v2 to each. So
the way I see what I need to do is something like:

if(d1 == INT16 && d2 == INT16)
{
v1int16 = (int16 *)v1;
v2int16 = (int16 *)v2;


You probably meant

v1int16 = *(int16*)v1;

and so on...
output = v1*v2;


What type is 'output'?
}
else if(d1 == INT16 && d2 == INT32)
{
v1int16 = (int16 *)v1;
v2int32 = (int32 *)v2;
output = v1*v2;
}
else if(d1 == INT32 && d2 == INT16)
{ .....}
else
{......}

Now this becomes rather tedious because I need to support 10 different
data types and I have to operate on 3 values instead of 2; basically a
switch case or if else construct from hell as someone put it in an
earlier post.

Although the virtual function method looks promising doesn't that
still mean I need to have a function for each combination of data
types?


You bet.
I was looking into templates but even with them the problem is
that my knowledge of the data type lies in an enum value and I don't
know how to incorporate this in a template; maybe a template which
does the final operation on pointers and virtual functions to
'condition' the values based on their data types, if any conditioning
is needed. These functions would return pointers that can be used by
the template.


Templates are not going to help you since your types are not known
until run-time. Still, even then they are not true C++ types in the
run-time sense (polymorphic types). They are just chunks of memory
whose meaning is expressed in some other values. Even though they
have been constructed, to use them you need to provide some switch
statement, there is no escaping that.

Now, you _could_ define your base class for your operations and then
derive your 10 types from it. Then all the operations just need to
be implemented as a case of double dispatch (look it up).

An alternative would be to extract those values into another type,
some kind of common denominator. If they are all integrals, use
'long' or 'unsigned long' to represent the values. If that's not
enough, use implementation-specific larger value (like 'int64_t' in
Visual C++, for example).
I'm just throwing out ideas here and forgive me if I've managed to
sound stupid with my solution. You guys should be able to suggest
something that works for me.


Hey, at the end of the day, we're not going to solve your problem.
You have to do it yourself. Bite the bullet and just code all those
1000 possibilities.

V
--
Please remove capital As from my address when replying by mail


Victor, sorry about the typos earlier but you interpreted them
correctly. Output data type is my third variable data type. I'm using
this in a block that will be used in Simulink simulations and all 3
data types will be specified by the user on the block dialog. The
functions that return the void* to me are Simulink callback methods
which are called at every time step to calculate the output of the
block. Simulink doesn't support 64-bit data types and anyway, I don't
want to go down that path because I do actually want to do what the
user is asking me to do. For instance if the user gives me data sets
which will result in an overflow I want there to be an overflow; which
may be hard to reproduce if I up-cast the data.

I just read the definition of double dispatch and it says that it is
used in cases where data types of arguments are only know at run time;
sounds like it is exactly what I need. But I should first look into
some of the more basic aspects of C++ like polymorphism and inheritance
before I jump into that. In the meanwhile could you tell me if you
think it's possible to solve my problem using double dispatch. Because
if not and I'm going to have to code 1000 switch case statements then
I'm going back to C :-)

Feb 18 '06 #12
Praetorian wrote:
[...]
I just read the definition of double dispatch and it says that it is
used in cases where data types of arguments are only know at run time;
sounds like it is exactly what I need. But I should first look into
some of the more basic aspects of C++ like polymorphism and
inheritance before I jump into that. In the meanwhile could you tell
me if you think it's possible to solve my problem using double
dispatch. Because if not and I'm going to have to code 1000 switch
case statements then I'm going back to C :-)


Look, if I were to give any weight to my answer about the possibility
to solve your problem with double dispatch, I'd have to basically solve
it an I have neither the time, nor the energy. And you're right, it
would require you to learn inheritance and polymorphism.

As to going back to C, good luck, but I think you'd still need to write
your 1000 switch case statements there just as well.

V
--
Please remove capital As from my address when replying by mail
Feb 18 '06 #13
What will this code be used for? Please don't say: "to multiply scalars
of different type together", something more higher level information
about the intended use would come in handy when factoring an useful
answer.. :)

Feb 19 '06 #14

persenaama wrote:
What will this code be used for? Please don't say: "to multiply scalars
of different type together", something more higher level information
about the intended use would come in handy when factoring an useful
answer.. :)


In this case it is something as mundane as multiplying 2-3 numbers
together after converting them to the data types that the user has
specified.

Feb 19 '06 #15
Whatever. If you have 10 scalar types, say,

u8, s8, u16, s16, u32, s32, u64, s64, f32, f64

(u = unsigned integer, # bits )
(s = signed integer, # bits)
(f = floating point, # bits)

This gives us 100 permutations with numDownConversi on *
numUpConversion , this is unacceptable. There is a workaround, some
background information.

f32 can store all values of u8, u16, u32. Also s8, s16 and same applies
for signed. f64 respectively, when available on your platoform
(sizeof(double) == 8 and there are 8 bits to a char) all integer values
can be presented.

So, to evaluate your expression up-convert to f64, do yer thang, and
down-convert to type you want. Remember the result might not fit but
same problem persists even if you do the computation in integer.

This, ofcourse, assumes that you are willing to accept that this isn't
a c++ program that you can assume to work everywhere. It is a c++
program that will work on a LOT of platforms, though.

Or you can use Perl.

Feb 20 '06 #16
persenaama wrote:
[...]
f32 can store all values of u8, u16, u32.
No, it cannot. Count significant bits in f32's mantissa.
[...]


V
--
Please remove capital As from my address when replying by mail
Feb 20 '06 #17

persenaama wrote:
Whatever. If you have 10 scalar types, say,

u8, s8, u16, s16, u32, s32, u64, s64, f32, f64

(u = unsigned integer, # bits )
(s = signed integer, # bits)
(f = floating point, # bits)

This gives us 100 permutations with numDownConversi on *
numUpConversion , this is unacceptable. There is a workaround, some
background information.

f32 can store all values of u8, u16, u32. Also s8, s16 and same applies
for signed. f64 respectively, when available on your platoform
(sizeof(double) == 8 and there are 8 bits to a char) all integer values
can be presented.

So, to evaluate your expression up-convert to f64, do yer thang, and
down-convert to type you want. Remember the result might not fit but
same problem persists even if you do the computation in integer.

This, ofcourse, assumes that you are willing to accept that this isn't
a c++ program that you can assume to work everywhere. It is a c++
program that will work on a LOT of platforms, though.

Or you can use Perl.


Unfortunately, it's not as simple as that. I don't have any data type
over 32 bits, both singles and doubles are 32 bits on my target
platform (DSP). So I can't just put everything into 64-bit integers.
And I can't use anything other than C or C++ to code this.

Ashish.

Feb 20 '06 #18
My bad, ofcourse, you are correct. The advice was to use f64, the
prognosis was flawed. I did ponder how to reply such sparsely worded
question with very broad implications, and came to conclusion that Perl
or other similiar scripting language is what he wants to use. That
answer would have been fuck-you-in-the-face therefore posted the most
trivial ad-hoc answer possible, because the most obvious answer of "do
all permutations" is a chore.

He did not state in any way or form how broad context he has in mind,
"mundane" gives implication that shortcuts are alternative. If it
weren't mundane, I would recommend writing a good expression evaluation
engine. Or using pre-written one, if his emploers (assuming he is
employed and doing this as part of his work, again could be wrong)
licensing policies allow.

How complex system he wants to develop for this task depends on how
important tha task is: how much it can cost to develop. If it's just
multiply two numbers I doubt he has too much time allocated for that in
the project schedule. I could be wrong. The simplest solution is to up
convert (to double for up to uint32) and forget it after it's done and
tested. uint64 won't even fit into long double (assuming it's 80 bits
as-in x86).

If that won't work for his platform (apparently doesn't) what he should
do next depends on definition of his problem. If his values are limited
to integer, of some precise range of integers, of if 16 bit is largest
integer, or 32 bit is the largest integer his input is, he might do w/o
large number library (or code that does the same). Etc.. too broad
context to give a clear-cut answer. I don't write code before knowing
what it's for, what it is supposed to do. I made a mistake of answering
poorly defined problem.

Feb 21 '06 #19
Come again? Is the problem that the result does not fit into types you
have, or that the input types are not static? Both?

What you have so far? Why you think it does not work?

Can you afford to implement multiply yourself, would that be too slow?
You didn't mention you are in performance critical section of
application, so that might do the trick. If you can afford that, it's
very easy, shouldn't take you more than 15 minutes or so.

Feb 21 '06 #20

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

Similar topics

5
2371
by: trying_to_learn | last post by:
Hello All Have another qn from the practice exercises. I ve done the exercise and the birds fly member fn was called successfully. But i still cant answer the authors qn. why this ability to assign void * has had to be avoided in C++. I didnt even instantiate bird obj then how was i able to call a bird member fn. Im confused. Quote: "Qn)Create a class called bird that can fly( ) and a class rock that can’t. Create a rock object, take...
6
9145
by: dam_fool_2003 | last post by:
Hai, I thank those who helped me to create a single linked list with int type. Now I wanted to try out for a void* type. Below is the code: #include<stdlib.h> #include<stdio.h> #include<string.h> #include<stddef.h> struct node
15
4167
by: Stig Brautaset | last post by:
Hi group, I'm playing with a little generic linked list/stack library, and have a little problem with the interface of the pop() function. If I used a struct like this it would be simple: struct node { struct node *next; void *data; };
2
1732
by: Xiangliang Meng | last post by:
Hi, all. As far as I know, the speical arithmetic operator on void pointer is an externsion in GNU CC. However, I could not find the relative topics in C99. Would someone like to help me find them out? in linux\mm\Slab.c, typedef struct slab_s { struct list_head list; unsigned long colouroff;
27
8978
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
14
3024
by: arun | last post by:
Hi, Why sizeof operator when applied on void returns one when compiled with gcc compiler ??. When i tried it on VC++ compiler, it gives an error. But another version of the VC++ compiler on my friend's machine gives it as zero. Have anyone tried this ? I believe it should give an error because i think there is nothing called void. Regards, arun..
18
2140
by: Giannis Papadopoulos | last post by:
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...
18
1818
by: hyderabadblues | last post by:
What does (void) poService in followinf peace of code mean tclCtrlBoard::tclCtrlBoard( void* poService ) { # if defined Something (void) poService; \\ What does this mean }
16
2703
by: PeterAPIIT | last post by:
Hello all C++ expert programmer, i have wrote partial general allocator for my container. After reading standard C++ library and code guru article, i have several questions. 1. Why allocator write forward declaration then allocation for void* rather than directly wrote allocator first ?
0
9646
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
10157
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
10096
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
9956
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8982
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
6742
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5386
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
5514
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3658
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.