www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - casting and arrays..

reply "Chris Warwick" <sp m.me.not> writes:
 std.file.read(filename);

returns type void[]

And it it seems i can cast that to whatever type i like, int[], bool[], 
ect.. anyway it compiles... but.. when casting between array types like this 
are runtime checks made to make sure the block being cast has the correct 
granularity.. if the return was void[7] will i get an runtime error casting 
it to int? Does a cast like that even work?

cheers,

cw
Mar 10 2007
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Chris Warwick" <sp m.me.not> wrote in message 
news:esvf5h$2317$1 digitalmars.com...
 std.file.read(filename);

 returns type void[]

 And it it seems i can cast that to whatever type i like, int[], bool[], 
 ect.. anyway it compiles... but.. when casting between array types like 
 this are runtime checks made to make sure the block being cast has the 
 correct granularity..
Yes.
 if the return was void[7] will i get an runtime error casting it to int?
Yes. void main() { ubyte[] a = [cast(ubyte)1, 2, 3, 4, 5, 6, 7]; int[] x = cast(int[])a; } That cast will give an "array cast misalignment" at run-time.
Mar 10 2007
next sibling parent "Chris Warwick" <sp m.me.not> writes:
"Jarrett Billingsley" <kb3ctd2 yahoo.com> wrote in message 
news:esvhdv$261a$1 digitalmars.com...
 "Chris Warwick" <sp m.me.not> wrote in message 
 news:esvf5h$2317$1 digitalmars.com...
 std.file.read(filename);

 returns type void[]

 And it it seems i can cast that to whatever type i like, int[], bool[], 
 ect.. anyway it compiles... but.. when casting between array types like 
 this are runtime checks made to make sure the block being cast has the 
 correct granularity..
Yes.
Cool.. very nice feature.. :-)
Mar 10 2007
prev sibling parent reply "Chris Warwick" <sp m.me.not> writes:
"Jarrett Billingsley" <kb3ctd2 yahoo.com> wrote in message 
news:esvhdv$261a$1 digitalmars.com...
 "Chris Warwick" <sp m.me.not> wrote in message 
 news:esvf5h$2317$1 digitalmars.com...
 std.file.read(filename);

 returns type void[]
Ok say you have that void[] array returned from std.read.file(). Whats the best way to copy a struct out from the void array? Only way i can think is memcpy, or some heavy duty casting and pointers. Is there a cleaner - pretier way with D? thanks, cw
Mar 10 2007
parent reply Kirk McDonald <kirklin.mcdonald gmail.com> writes:
Chris Warwick wrote:
 "Jarrett Billingsley" <kb3ctd2 yahoo.com> wrote in message 
 news:esvhdv$261a$1 digitalmars.com...
 "Chris Warwick" <sp m.me.not> wrote in message 
 news:esvf5h$2317$1 digitalmars.com...
 std.file.read(filename);

 returns type void[]
Ok say you have that void[] array returned from std.read.file(). Whats the best way to copy a struct out from the void array? Only way i can think is memcpy, or some heavy duty casting and pointers. Is there a cleaner - pretier way with D? thanks, cw
Assuming that array.length == YourStruct.sizeof, you can say cast(YourStruct*)array.ptr to treat the array as the struct in-place. -- Kirk McDonald http://kirkmcdonald.blogspot.com Pyd: Connecting D and Python http://pyd.dsource.org
Mar 10 2007
parent reply "Chris Warwick" <sp m.me.not> writes:
"Kirk McDonald" <kirklin.mcdonald gmail.com> wrote in message 
news:esvk4l$28mi$1 digitalmars.com...
 Chris Warwick wrote:
 "Jarrett Billingsley" <kb3ctd2 yahoo.com> wrote in message 
 news:esvhdv$261a$1 digitalmars.com...
 "Chris Warwick" <sp m.me.not> wrote in message 
 news:esvf5h$2317$1 digitalmars.com...
 std.file.read(filename);

 returns type void[]
Ok say you have that void[] array returned from std.read.file(). Whats the best way to copy a struct out from the void array? Only way i can think is memcpy, or some heavy duty casting and pointers. Is there a cleaner - pretier way with D? thanks, cw
Assuming that array.length == YourStruct.sizeof, you can say cast(YourStruct*)array.ptr to treat the array as the struct in-place.
Well i cant realy do that cause im reading a binary file in, and then i want to parse it, extract structs and various types here and there. cheers, cw
Mar 10 2007
next sibling parent reply gareis <dhasenan gmail.com> writes:
Chris Warwick wrote:
 "Kirk McDonald" <kirklin.mcdonald gmail.com> wrote in message 
 news:esvk4l$28mi$1 digitalmars.com...
 Chris Warwick wrote:
 "Jarrett Billingsley" <kb3ctd2 yahoo.com> wrote in message 
 news:esvhdv$261a$1 digitalmars.com...
 "Chris Warwick" <sp m.me.not> wrote in message 
 news:esvf5h$2317$1 digitalmars.com...
 std.file.read(filename);

 returns type void[]
Ok say you have that void[] array returned from std.read.file(). Whats the best way to copy a struct out from the void array? Only way i can think is memcpy, or some heavy duty casting and pointers. Is there a cleaner - pretier way with D? thanks, cw
Assuming that array.length == YourStruct.sizeof, you can say cast(YourStruct*)array.ptr to treat the array as the struct in-place.
Well i cant realy do that cause im reading a binary file in, and then i want to parse it, extract structs and various types here and there.
Then you need to do pointer arithmetic, it seems. Assuming your struct has a deserializer method that takes a void*: --- for (int i = 0; i < array.length; i++) { if (someStructStartsHere()) { someStruct s; s.deserialize(array.ptr + i); do_stuff_with_someStruct (s); } } --- That should probably work.
 cheers,
 
 cw
 
 
Mar 10 2007
next sibling parent reply Daniel Keep <daniel.keep.lists gmail.com> writes:
gareis wrote:
 Chris Warwick wrote:
 "Kirk McDonald" <kirklin.mcdonald gmail.com> wrote in message
 news:esvk4l$28mi$1 digitalmars.com...
 Chris Warwick wrote:
 "Jarrett Billingsley" <kb3ctd2 yahoo.com> wrote in message
 news:esvhdv$261a$1 digitalmars.com...
 "Chris Warwick" <sp m.me.not> wrote in message
 news:esvf5h$2317$1 digitalmars.com...
 std.file.read(filename);

 returns type void[]
Ok say you have that void[] array returned from std.read.file(). Whats the best way to copy a struct out from the void array? Only way i can think is memcpy, or some heavy duty casting and pointers. Is there a cleaner - pretier way with D? thanks, cw
Assuming that array.length == YourStruct.sizeof, you can say cast(YourStruct*)array.ptr to treat the array as the struct in-place.
Well i cant realy do that cause im reading a binary file in, and then i want to parse it, extract structs and various types here and there.
Then you need to do pointer arithmetic, it seems. Assuming your struct has a deserializer method that takes a void*: --- for (int i = 0; i < array.length; i++) { if (someStructStartsHere()) { someStruct s; s.deserialize(array.ptr + i); do_stuff_with_someStruct (s); } } --- That should probably work.
 cheers,

 cw
Personally, I like std.stream.MemoryStream for things like this. That way, I don't have to stuff around with pointer arithmetic, and potentially make a mistake. Incidentally, I never knew that you could safely cast arrays of one type to another! Yet another thing I didn't know about D :P -- Daniel -- Unlike Knuth, I have neither proven or tried the above; it may not even make sense. v2sw5+8Yhw5ln4+5pr6OFPma8u6+7Lw4Tm6+7l6+7D i28a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP http://hackerkey.com/
Mar 10 2007
parent Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
Daniel Keep wrote:
 
 Incidentally, I never knew that you could safely cast arrays of one type
 to another!  Yet another thing I didn't know about D :P
I never saw anyone claim it was _safe_, just that it was _possible_ ;)... Casting arrays is about as safe as pointer casting (which is actually pretty much what you're doing), which is to say it isn't safe unless you're very careful ;). For example, if the type you're casting to an array of contains any pointers or class references, you've usually got a problem unless you're sure those are valid (reading pointers from anything that wasn't always in memory is a bad idea, for example; so no reading pointers from file or socket connections). The only thing that's safer about array casting is that the length of the data is taken into account, so you won't as easily be accessing invalid memory. Which I'd say is more of a property of arrays vs pointers, instead of array casting vs pointer casting.
Mar 11 2007
prev sibling parent Reiner Pope <some address.com> writes:
gareis wrote:
 Then you need to do pointer arithmetic, it seems. Assuming your struct 
 has a deserializer method that takes a void*:
 ---
 for (int i = 0; i < array.length; i++) {
     if (someStructStartsHere()) {
         someStruct s;
         s.deserialize(array.ptr + i);
         do_stuff_with_someStruct (s);
     }
 }
 ---
 
Let D do the memcpy? foreach (i, elem; array) { if (someStructStartsHere()) { someStruct s = * (cast(someStruct*) &array[i]); } } That should copy the struct out of the array. Cheers, Reiner
Mar 10 2007
prev sibling parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Chris Warwick" <sp m.me.not> wrote in message 
news:esvnvu$2ku8$1 digitalmars.com...
 Well i cant realy do that cause im reading a binary file in, and then i 
 want to parse it, extract structs and various types here and there.
I wouldn't really recommend std.file.read for anything more than reading in the simplest of files. Use std.stream.File or BufferedFile for reading in complex files. You can then read in a struct with .readExact.
Mar 10 2007
parent reply "Chris Warwick" <sp m.me.not> writes:
"Jarrett Billingsley" <kb3ctd2 yahoo.com> wrote in message 
news:et00eg$66h$1 digitalmars.com...
 "Chris Warwick" <sp m.me.not> wrote in message 
 news:esvnvu$2ku8$1 digitalmars.com...
 Well i cant realy do that cause im reading a binary file in, and then i 
 want to parse it, extract structs and various types here and there.
I wouldn't really recommend std.file.read for anything more than reading in the simplest of files. Use std.stream.File or BufferedFile for reading in complex files. You can then read in a struct with .readExact.
Note to self ... RTFM ;-)
Mar 11 2007
parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Chris Warwick" <sp m.me.not> wrote in message 
news:et0s2n$1no1$1 digitalmars.com...
 Note to self ... RTFM
Hehe :) Though when I first started using D, I thought std.file would be the best place for file IO too! Maybe I don't think like a C++ programmer, where file IO should be done with streams..
Mar 11 2007