www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - casting a type to a void*

reply Pierre-Luc Cyr <Pierre-Luc_member pathlink.com> writes:
Hello,
I was just wondering, because I woudl need it, how to cast a void* on a byte?
I mean, something like that, which, obviously, do not work:

void* number;
int inumber;
number = cast(int)(inumber);

It's probably easy enough but I never did it before, so.

Thanks of your help,
Pierre-Luc Cyr
Dec 10 2005
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Pierre-Luc Cyr" <Pierre-Luc_member pathlink.com> wrote in message 
news:dng80g$caa$1 digitaldaemon.com...
 Hello,
 I was just wondering, because I woudl need it, how to cast a void* on a 
 byte?
 I mean, something like that, which, obviously, do not work:
byte b; void* v = cast(void*)&b; That cast() might even be optional, I forget.
Dec 10 2005
parent reply Derek Parnell <derek psych.ward> writes:
On Sun, 11 Dec 2005 02:14:53 -0500, Jarrett Billingsley wrote:

 "Pierre-Luc Cyr" <Pierre-Luc_member pathlink.com> wrote in message 
 news:dng80g$caa$1 digitaldaemon.com...
 Hello,
 I was just wondering, because I woudl need it, how to cast a void* on a 
 byte?
 I mean, something like that, which, obviously, do not work:
byte b; void* v = cast(void*)&b; That cast() might even be optional, I forget.
Yes it is optional. -- Derek Parnell Melbourne, Australia 11/12/2005 11:02:31 PM
Dec 11 2005
parent reply Pierre-Luc Cyr <Pierre-Luc_member pathlink.com> writes:
Thanks, it seems strange (I mean, different from the way I thought it should
have been done, but well)

You used a byte, it does also work for other types?
If so, it's good...

Thanks

Pierre-Luc Cyr

 byte b;
 void* v = cast(void*)&b;
 
 That cast() might even be optional, I forget.
Yes it is optional.
Dec 11 2005
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Pierre-Luc Cyr" <Pierre-Luc_member pathlink.com> wrote in message 
news:dnhf7m$2b7k$1 digitaldaemon.com...
 Thanks, it seems strange (I mean, different from the way I thought it 
 should
 have been done, but well)
How did you expect it do be done? All you're doing is getting a pointer to the variable and casting it to void*, unless that's not what you're looking for..
 You used a byte, it does also work for other types?
Yes :) void* can point to anything.
Dec 11 2005
parent reply Pierre-Luc Cyr <Pierre-Luc_member pathlink.com> writes:
Well, it's because I am developping a class "number" which will be of a variable
precision, I mean, depending of the value you put in it, it will use the good
type and return the good one. Oh, and the library will also be used to do maths
calculation, some things that sometimes may be useful.
Well, that's what I want it to do, here an example how I wish we might use it:

number n1 = new number(50);  // considered as a byte
number n2 = new number(20000);
number n3 = new number(1241284129012312.12313123141)   // a big floating-point
value, I did not mesured this one)

number n4 = new number(n3.FindQuadRoots(n3, n1, n2)); // Result is the type of
the most precise value

Well I wish it will work, but I'm not sure void pointers will work fine. It is
just my first idea. Perharps will I have to find an other way, I'll see.

Pierre-Luc Cyr


In article <dnhu0l$9lo$1 digitaldaemon.com>, Jarrett Billingsley says...
"Pierre-Luc Cyr" <Pierre-Luc_member pathlink.com> wrote in message 
news:dnhf7m$2b7k$1 digitaldaemon.com...
 Thanks, it seems strange (I mean, different from the way I thought it 
 should
 have been done, but well)
How did you expect it do be done? All you're doing is getting a pointer to the variable and casting it to void*, unless that's not what you're looking for..
 You used a byte, it does also work for other types?
Yes :) void* can point to anything.
Dec 11 2005
next sibling parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Pierre-Luc Cyr" <Pierre-Luc_member pathlink.com> wrote in message 
news:dni15r$gsa$1 digitaldaemon.com...
 Well I wish it will work, but I'm not sure void pointers will work fine. 
 It is
 just my first idea. Perharps will I have to find an other way, I'll see.
Well it's certainly an interesting idea, although most "big number" classes I've seen have been implemented in other ways, such as using strings to represent the numbers and performing the calculations digit by digit (kind of like doing it by hand). I'm not sure what kind of performance you'll get by using the void pointers, as you'll have to dereference every time you want to access the value (although I can't say that string number classes would be incredibly efficient as well). I would imagine you're already using some kind of "type" variable to keep track of what type of data the data pointer is pointing to (int, short, byte etc.), so what you could do instead is to use a union of all numeric types, like this: union { byte mByte; ubyte mUByte; short mShort; ... } And keep track of what type you're storing, and access the correct union member using that.
Dec 11 2005
prev sibling parent Pierre-Luc Cyr <Pierre-Luc_member pathlink.com> writes:
How did you expect it do be done?  All you're doing is getting a pointer to 
the variable and casting it to void*, unless that's not what you're looking 
for..
It's because in C, if I remember, I had to create an other function that would return a pointer to something that I do not remember. Perhaprs it's not what I'm looking for, I'll see it tonight.
Dec 11 2005