digitalmars.D - strange bug with unions?
- seen (28/28) May 29 2015 I don't know why but when using a template union with a static
 - deadalnix (2/30) May 29 2015 http://fr.wikipedia.org/wiki/Endianness#Little_endian
 - seen (4/40) May 29 2015 endianness should not be an issue since I'm not changing machines
 - =?UTF-8?B?QWxpIMOHZWhyZWxp?= (9/37) May 29 2015 That function returns a slice to a...
 - Vladimir Panteleev (5/11) May 29 2015 This function is incorrect: it is returning a slice (dynamic
 
I don't know why but when using a template union with a static 
ubyte array i get bizarre results!
module main;
import std.stdio;
private union ByteConverter(T)
{
	ubyte[T.sizeof] bytes;
	T value;
}
ubyte[] toBytes(T)(T from) if (!is(T == const))
{
	ByteConverter!T converter;
	converter.value = from;
	return converter.bytes;
}
T fromBytes(T)(in ubyte[] bytes)
{
	ByteConverter!T converter;
	converter.bytes = bytes;
	return converter.value;
}
void main(string[] args)
{
	writeln(fromBytes!uint(toBytes(34u)));// should print 32 but 
prints 1244564
}
why dose it print 1244564 the array is static size so why is it 
acting like bytes is a pointer not a static array?
 May 29 2015
On Friday, 29 May 2015 at 07:55:55 UTC, seen wrote:
 I don't know why but when using a template union with a static 
 ubyte array i get bizarre results!
 module main;
 import std.stdio;
 private union ByteConverter(T)
 {
 	ubyte[T.sizeof] bytes;
 	T value;
 }
 ubyte[] toBytes(T)(T from) if (!is(T == const))
 {
 	ByteConverter!T converter;
 	converter.value = from;
 	return converter.bytes;
 }
 T fromBytes(T)(in ubyte[] bytes)
 {
 	ByteConverter!T converter;
 	converter.bytes = bytes;
 	return converter.value;
 }
 void main(string[] args)
 {
 	writeln(fromBytes!uint(toBytes(34u)));// should print 32 but 
 prints 1244564
 }
 why dose it print 1244564 the array is static size so why is it 
 acting like bytes is a pointer not a static array?
http://fr.wikipedia.org/wiki/Endianness#Little_endian
 May 29 2015
On Friday, 29 May 2015 at 08:00:54 UTC, deadalnix wrote:On Friday, 29 May 2015 at 07:55:55 UTC, seen wrote:endianness should not be an issue since I'm not changing machines and using static array unions is a common way to convert to bytes in D.I don't know why but when using a template union with a static ubyte array i get bizarre results! module main; import std.stdio; private union ByteConverter(T) { ubyte[T.sizeof] bytes; T value; } ubyte[] toBytes(T)(T from) if (!is(T == const)) { ByteConverter!T converter; converter.value = from; return converter.bytes; } T fromBytes(T)(in ubyte[] bytes) { ByteConverter!T converter; converter.bytes = bytes; return converter.value; } void main(string[] args) { writeln(fromBytes!uint(toBytes(34u)));// should print 32 but prints 1244564 } why dose it print 1244564 the array is static size so why is it acting like bytes is a pointer not a static array?http://fr.wikipedia.org/wiki/Endianness#Little_endian
 May 29 2015
On 05/29/2015 12:55 AM, seen wrote:I don't know why but when using a template union with a static ubyte array i get bizarre results! module main; import std.stdio; private union ByteConverter(T) { ubyte[T.sizeof] bytes;That's a fixed-length array (aka static array), which has value semantics.T value; } ubyte[] toBytes(T)(T from) if (!is(T == const))That function returns a slice to a...{ ByteConverter!T converter; converter.value = from; return converter.bytes;... local static array. :( Replace the return type with a) auto b) ubyte[T.sizeof]} T fromBytes(T)(in ubyte[] bytes) { ByteConverter!T converter; converter.bytes = bytes; return converter.value; } void main(string[] args) { writeln(fromBytes!uint(toBytes(34u)));// should print 32 but prints 1244564 } why dose it print 1244564 the array is static size so why is it acting like bytes is a pointer not a static array?Now it prints 34. :) Ali
 May 29 2015
On Friday, 29 May 2015 at 07:55:55 UTC, seen wrote:
 ubyte[] toBytes(T)(T from) if (!is(T == const))
 {
 	ByteConverter!T converter;
 	converter.value = from;
 	return converter.bytes;
 }
This function is incorrect: it is returning a slice (dynamic 
array) of a static array, which is located on the stack. The fact 
that it compiles without error is bug 9279 
(https://issues.dlang.org/show_bug.cgi?id=9279).
 May 29 2015








 
 
 
 "seen" <sycam.inc gmail.com> 