473,395 Members | 1,720 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,395 software developers and data experts.

void* questions

I have never really understood the purpose of the void*.

I guess it is so different data types could for instance be passed into a
function. And then the function will re cast it to the correct data type?

I have an example here that does not seem to work.

I pass in an integer in one case and a float in another case.

When I pass the float, I would expect to get an answer of 5 but I get a huge
number that is not 5.

So, I was wondering if someone could tell me what I am doing wrong here?

Thanks.

I assume this is a strange way of using the void*, but I was just wanting to
practice using it in some way.

#include <iostream.h>
void f1( void* number );
int main()
{

int number;

float number2;

number2 = 5.25;

number = 5;

f1( &number );

f1( &number2 );

return 0;

}
void f1( void* number )
{
int* num;

num = (int*)number;

cout << *num << endl;
}

Jul 22 '05 #1
5 1840

"johny smith" <pr**************@charter.net> wrote in message
news:10*************@corp.supernews.com...
I have never really understood the purpose of the void*.

I guess it is so different data types could for instance be passed into a
function. And then the function will re cast it to the correct data type?

I have an example here that does not seem to work.

I pass in an integer in one case and a float in another case.

When I pass the float, I would expect to get an answer of 5 but I get a huge number that is not 5.

So, I was wondering if someone could tell me what I am doing wrong here?

Thanks.

I assume this is a strange way of using the void*, but I was just wanting to practice using it in some way.

#include <iostream.h>
void f1( void* number );
int main()
{

int number;

float number2;

number2 = 5.25;

number = 5;

f1( &number );

f1( &number2 );

return 0;

}
void f1( void* number )
{
int* num;

num = (int*)number;

cout << *num << endl;
}


Well, first off, with C++ your use of void* has really diminished. Try
templates. For this sample:

template <class T>
void f1(T number)
{
cout << number;
}

Much safer and a lot more type strict. Furthermore, with void pointers, some
things get a bit crazy. You can't exactly just take a float pointer and cast
it to an integer pointer. To understand this, let's take the following
theoretical example:

The float you're passing in, in binary: 010101010010101101
(Note that's not a real number, just one I came up with)

The integer that results from it: 010101010010101101
(Note it hasn't changed)

Now let's do a REAL type cast (like float a = 5; cout <<(int)a;)

The float you pass in: 010101010101001
The integer that results: 01110101

(Note they are not the same)

This is the problem. Void pointers retain no knowledge of their type. As a
result, typecasting is not safe at all. Floats are interpreted differently
from ints, remember that. Because they are interpreted differently, it's a
big problem, and you're getting some really huge number. Templates are much
easier to work with and much safer.

Good luck
-- Matt

P.S. If you REALLY must use a pointer to void to cast something, try a
reinterpret_cast<>. It's just the C++ way of doing something insane like
that ... but I don't see any purpose for it -- ever.

Jul 22 '05 #2
johny smith posted:
I have never really understood the purpose of the void*.

I guess it is so different data types could for instance be passed into
a function. And then the function will re cast it to the correct data
type?

I have an example here that does not seem to work.

I pass in an integer in one case and a float in another case.

When I pass the float, I would expect to get an answer of 5 but I get a
huge number that is not 5.

So, I was wondering if someone could tell me what I am doing wrong
here?

Thanks.

I assume this is a strange way of using the void*, but I was just
wanting to practice using it in some way.

#include <iostream.h>
void f1( void* number );
int main()
{

int number;

float number2;

number2 = 5.25;

number = 5;

f1( &number );

f1( &number2 );

return 0;

}
void f1( void* number )
{
int* num;

num = (int*)number;

cout << *num << endl;
}

What you're doing is exactly what my accessor_cast does (See the thread
entitled "accessor_cast"). You've got a float, but you're accessing in the
way in which an integer should be accessed. Long story short, the bit
pattern for a float of value 5 is not equal to the bit pattern for an int of
value 5.
-JKop
Jul 22 '05 #3
"Matthew Del Buono" <Ma******@nospam.com> wrote in message news:<0c5Gc.1622$jp1.518@lakeread04>...

P.S. If you REALLY must use a pointer to void to cast something, try a
reinterpret_cast<>. It's just the C++ way of doing something insane like
that ... but I don't see any purpose for it -- ever.


There is a purpose, and that is in writing "opaques", eg streaming
binary, although I prefer to give the class factories a way of
creating their classes from the opaque, and getting the classes to
output opaque themselves. That is the ideal, but in the real world you
sometimes need high performance and a cast can occasionally give you
that little bit extra.
Jul 22 '05 #4
johny smith wrote:
I have never really understood the purpose of the void*.
It's only rarely needed in C++.
I guess it is so different data types could for instance be passed
into a function. And then the function will re cast it to the correct
data type?
You could use it for that, but why not just use overloaded functions
instead?
I have an example here that does not seem to work.

I pass in an integer in one case and a float in another case.

When I pass the float, I would expect to get an answer of 5 but I get
a huge number that is not 5.
So, I was wondering if someone could tell me what I am doing wrong
here?
You're misunderstanding what the cast means. You're converting the
pointer, not the data it points to. When you wrote 5.25 to number2, the
compiler saved a bit pattern into that variable that represents that
value as float. In the function, you just interpret that exact same bit
pattern as an int. No proper conversion to an int value is done.
Therefore you get a bogus value. Basically, C++ doesn't even guarantee
that the pointer conversion itself works. It's only guaranteed that you
can do a lossless conversion of a pointer to an object into a void* and
back into the original type.
Btw: You should stick to the C++ cast and not use the C-style cast. In
this case, reinterpret_cast should be the right one. But again, it's
only rarely needed - mostly in low-level code that needs to do some
binary I/O. But you should not use it without knowing the consequences.
Thanks.

I assume this is a strange way of using the void*, but I was just
wanting to practice using it in some way.

#include <iostream.h>
<iostream.h> is not a standard header in C++. Use <iostream> instead.
void f1( void* number );
int main()
{

int number;

float number2;

number2 = 5.25;

number = 5;

f1( &number );

f1( &number2 );

return 0;

}
void f1( void* number )
{
int* num;

num = (int*)number;

cout << *num << endl;
}


Jul 22 '05 #5

"nmtop40" <nm*****@yahoo.com> wrote in message
news:f3**************************@posting.google.c om...
"Matthew Del Buono" <Ma******@nospam.com> wrote in message

news:<0c5Gc.1622$jp1.518@lakeread04>...

P.S. If you REALLY must use a pointer to void to cast something, try a
reinterpret_cast<>. It's just the C++ way of doing something insane like
that ... but I don't see any purpose for it -- ever.


There is a purpose, and that is in writing "opaques", eg streaming
binary, although I prefer to give the class factories a way of
creating their classes from the opaque, and getting the classes to
output opaque themselves. That is the ideal, but in the real world you
sometimes need high performance and a cast can occasionally give you
that little bit extra.


Okay, maybe "ever" was an overstatement, but it's rarely needed. For most
situations you won't need a cast like that...

-- Matt
Jul 22 '05 #6

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

Similar topics

52
by: Vladimir Grul | last post by:
Hello, I have a class member function declared as class some_class { .... virtual int call(void); }; Can I use this-> inside the function body?
2
by: Christian Christmann | last post by:
Hi, I was asked to remove the void*'s from an old project. My idea was to replace them with templates. Is this a good idea? How would you proceed? Thanks Chris
6
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>...
8
by: David M. Wilson | last post by:
I know this doesn't specifically pertain to the scope of comp.lang.c, but I don't know of a better forum to post it on, besides, what is the practicality of comp.lang.c if it weren't for answers to...
26
by: Janice | last post by:
What is the major reason for using void*? When should we use void* for both input arguments and return value? How can we cast the void* pointer to the type we need? Thanx
9
by: Juggernaut | last post by:
I am trying to create a p_thread pthread_create(&threads, &attr, Teste, (void *)var); where var is a char variable. But this doesnt't work, I get this message: test.c:58: warning: cast to pointer...
5
by: Stijn van Dongen | last post by:
A question about void*. I have a hash library where the hash create function accepts functions unsigned (*hash)(const void *a) int (*cmp) (const void *a, const void *b) The insert function...
49
by: elmar | last post by:
Hi Clers, If I look at my ~200000 lines of C code programmed over the past 15 years, there is one annoying thing in this smart language, which somehow reduces the 'beauty' of the source code...
16
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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...

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.