Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

[BUG] in make struct! ?

 [1/5] from: rebol-list2::seznam::cz at: 29-Jul-2003 12:28


Hello rebol-list, just wondering if this is a bug: this seems to be ok:
>> s: make struct! [a [short]] [1] third s
== #{0100}
>> s: make struct! [a [short] b [short]] [1 2] third s
== #{01000200}
>> s: make struct! [a [short] b [short] c [short]] [1 2 3] third s
== #{010002000300}
>> s: make struct! [a [short] b [short] c [long]] [1 2 3] third s
== #{0100020003000000} but:
>> s: make struct! [a [short] b [long]] [1 2] third s
== #{0100000002000000} I suppose it should be == #{010002000000} If it's a bug, it should be fixed. If it's by design, it would make the struct! much more useless:( and I will have to rather use the 'parse to get data from binary stream:( PS: I would like to know why make struct! is still not available in rebol/core:( -- Best regards, rebOldes mailto:[oliva--david--seznam--cz]

 [2/5] from: g:santilli:tiscalinet:it at: 29-Jul-2003 12:44


Hi rebOldes, On Tuesday, July 29, 2003, 12:28:35 PM, you wrote: r> but:
>>> s: make struct! [a [short] b [long]] [1 2] third s
r> == #{0100000002000000} That's called padding. Most CPUs/architectures require that 32 bit data be word aligned, i.e. start on an address that is divisible by 4. This is very platform dependent anyway, so what you see here is not what you might get on other platforms. Remember that structs are intended for interfacing with C libraries. Regards, Gabriele. -- Gabriele Santilli <[g--santilli--tiscalinet--it]> -- REBOL Programmer Amiga Group Italia sez. L'Aquila --- SOON: http://www.rebol.it/

 [3/5] from: rebol-list2:seznam:cz at: 4-Aug-2003 19:59


Hello Gabriele, Tuesday, July 29, 2003, 12:44:57 PM, you wrote: GS> Hi rebOldes, GS> On Tuesday, July 29, 2003, 12:28:35 PM, you wrote: r>> but:
>>>> s: make struct! [a [short] b [long]] [1 2] third s
r>> == #{0100000002000000} GS> That's called padding. Most CPUs/architectures require that 32 bit GS> data be word aligned, i.e. start on an address that is divisible GS> by 4. This is very platform dependent anyway, so what you see here GS> is not what you might get on other platforms. Remember that GS> structs are intended for interfacing with C libraries. if it would be padding, why this struct is not padded as well:
>> s: make struct! [a [short] b [short] c [short]] [1 2 3] third s
== #{010002000300} I would like to see Carl's comment (probably will have to try some luck and write on the feedback) If this is not a bug it's bad, because one will not be able to do thinks like that (which is working on windows): REBOL [ Title: "Bmp-tools" Date: 25-Jul-2003/19:14:56+2:00 Name: none Version: 0.0.1 File: %bmp-tools.r Author: "oldes" Usage: [ bmp-info %test.bmp bmp-create %/j/test/test3.bmp 20x20 ] Purpose: {'bmp-create is a function useful to create extra large BMP files} Email: [oliva--david--seznam--cz] Links: [ http://www.wotsit.org/download.asp?f=bmpfrmat http://www.wotsit.org/download.asp?f=bmp ] Require: [rss-utils %print-struct.r] ] print-struct: func[ {Prints readable pairs of variables and values of the struct! datatype} st [struct!] "Struct! to explore" /local val i ][ i: 0 parse first st [ opt [set val string!] (print val loop length? val [prin "="] print "") any [ set val word! ( insert/dup tail val: to-string val #"." 20 - length? val print [val pick second st i: i + 1] ) | any-type! ]] ] s_BitmapFileHeader: make struct! [ "Size, and layout of a device-independent bitmap file" ;Type [short] "The characters identifying the bitmap." Size [long] "Complete file size in bytes" Reserved [long] "Reserved for later use." OffsetBits [long] "Offset from beginning of file to the beginning of the bitmap data." ] none s_BitmapInfoHeader: make struct! [ "The dimensions, compression type, and color format for the bitmap." Size [long] "Length of the Bitmap Info Header" Width [long] "Horizontal width of bitmap in pixels" Height [long] "Vertical height of bitmap in pixels" Planes [short] "Number of planes in this bitmap" BitCount [short] "Bits per pixel used to store palette entry information" Compression [long] "Compression specifications" SizeImage [long] "Size of the bitmap data in bytes" XPelsPerMeter [long] "Horizontal resolution expressed in pixel per meter" YPelsPerMeter [long] "Vertical resolution expressed in pixels per meter" ColorsUsed [long] "Number of colors used by this bitmap" ColorsImportant [long] "Number of important colors" ] none bmp-info: func [bmp [binary! port! file!] "BMP file to parse"][ if file? bmp [bmp: open/direct bmp] if (to string! copy/part bmp 2) <> "BM" [make error! "Not a BMP file!"] if binary? bmp [bmp: skip bmp 2] change third s_BitmapFileHeader copy/part bmp 12 print-struct s_BitmapFileHeader if binary? bmp [bmp: skip bmp 12] change third s_BitmapInfoHeader copy/part bmp 40 print-struct s_BitmapInfoHeader if port? bmp [close bmp] ] bmp-create: func [ target [file! none!] "Where to save the new BMP file" size [pair!] "Size of the created BMP file" ][ bmp: open/new/direct/binary/write/no-wait target insert bmp #{424D} insert bmp third make struct! s_BitmapFileHeader reduce [(54 + (3 * size/x * size/y)) 0 54] insert bmp third make struct! s_BitmapInfoHeader reduce [40 size/x size/y 1 24 0 0 11808 11808 0 0] loop size/y [ insert/dup bmp #{FFFFFF} size/x ] close bmp bmp/state/outBuffer: none unset 'bmp ] -- Best regards, rebOldes mailto:[oliva--david--seznam--cz]

 [4/5] from: lmecir:mbox:vol:cz at: 4-Aug-2003 23:22


Hi Oldes,
> GS> That's called padding. Most CPUs/architectures require that 32 bit > GS> data be word aligned, i.e. start on an address that is divisible
<<quoted lines omitted: 4>>
> >> s: make struct! [a [short] b [short] c [short]] [1 2 3] third s > == #{010002000300}
because the structure doesn't contain a 32-bit value, all values are 16-bit

 [5/5] from: g:santilli:tiscalinet:it at: 5-Aug-2003 0:26


Hi rebOldes, On Monday, August 4, 2003, 7:59:53 PM, you wrote: r> if it would be padding, why this struct is not padded as well:
>>> s: make struct! [a [short] b [short] c [short]] [1 2 3] third s
r> == #{010002000300} A short is 16 bit. Usually CPUs require them to be evenly aligned.
>> third make struct! [a [char] b [short]] [#"^(01)" 2]
== #{01000200} Indeed, on my system you get a pad byte in this case. You don't need padding if you are aligned already:
>> third make struct! [a [char] a' [char] b [short]] [#"^(01)" #"^(02)" 3]
== #{01020300} You get the same results if you create these structures in a C compiler (i.e. the compiler adds the required pad bytes as REBOL does). Regards, Gabriele. -- Gabriele Santilli <[g--santilli--tiscalinet--it]> -- REBOL Programmer Amiga Group Italia sez. L'Aquila --- SOON: http://www.rebol.it/

Notes
  • Quoted lines have been omitted from some messages.
    View the message alone to see the lines that have been omitted