473,785 Members | 2,219 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
Here's a brute-force peasant algorithm implementation:

uint32 mul(uint32 a, uint32 b)
{
uint32 v = 0;
for ( ; b; )
{
v += a & (0 - (b & 1));
a <<= 1;
b >>= 1;
}
return v;
}

It multiplies two 32 bit values, if you want 64 bit result you need to
write 64 bit addition and use "uint64" for v. Easiest way to do that is
something like this, since you don't have 64 bit integers:

struct uint64
{
uint32 v[2];
// .. operators you need here..
};

How you going to use the 64 bit result is up to you.. if you want
signed, too, it is fairly trivial.

This is not really answering your question, which is a bit mystry what
the problem is. Choosing the right types for compiler/language built-in
* operator? Or how to store and broadcast the user supplied
non-statically typed values to the built-in * operator?

By now, you should already have the code. I'm 99% sure that you do, so
the thread is closed? ;-o

Feb 21 '06 #21

persenaama wrote:
Here's a brute-force peasant algorithm implementation:

uint32 mul(uint32 a, uint32 b)
{
uint32 v = 0;
for ( ; b; )
{
v += a & (0 - (b & 1));
a <<= 1;
b >>= 1;
}
return v;
}

It multiplies two 32 bit values, if you want 64 bit result you need to
write 64 bit addition and use "uint64" for v. Easiest way to do that is
something like this, since you don't have 64 bit integers:

struct uint64
{
uint32 v[2];
// .. operators you need here..
};

How you going to use the 64 bit result is up to you.. if you want
signed, too, it is fairly trivial.

This is not really answering your question, which is a bit mystry what
the problem is. Choosing the right types for compiler/language built-in
* operator? Or how to store and broadcast the user supplied
non-statically typed values to the built-in * operator?

By now, you should already have the code. I'm 99% sure that you do, so
the thread is closed? ;-o


I'm sorry, I didn't check this thread at all yesterday and that's why I
didn't respond to your posts. I don't have the code yet. Let me explain
the problem to you once more: I have a dialog box on which the user
select data types for 3 variables and an operation to be performed on
those variables. The data types I have are 8, 16 and 32 bit signed and
unsigned integers and floats and doubles; total of 8. That leads to
8*8*8 permutations if I was to code it with switch case or if else
statements. This of course is unacceptable and so what I do for now is
restrict the user to 2 data type options only.

I have some library functions which will return a void * to the 3
variables and I can get their data types from another function which
will return an enum of the data type. It is then my job to type cast
the void * to the appropriate data type. What I would like to have is
some mechanism that lets data types be decided at run time and a
general template like thing (I don't know much C++ so I'm just throwing
out terms I've learned in the past few days) that will let me do the
operation using the appropriate data type.

Someone mentioned double dispatch in this thread and I looked into that
but unfortuantely it is a little too complicated for me to understand
without delving deeper into C++. I've started doing that, bought a
couple of books and reading tutorials online but you guessed correctly
that there isn't much time allocated to this in the project and so I'm
doing this on my own time; so it's slow going. However, once I develop
this i could use it whenever I'm faced with a problem of this kind and
so it would be rather useful to get it working.

Also, this is part of my work, not a school project; so you're not
helping me do my homework :-)

Feb 22 '06 #22

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
9483
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,...
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
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?
1
4055
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
3
2887
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.