Jump to content

Recommended Posts

  • Administrators
Posted

So, my latest code is as follows. If you can see a mistake or think of a more elegant way to do it, then shout:

nAlign = Value of the "align" keyword, default is 8

iDataTypeSize = is the size in bytes of the current member

nLargestSize = is the size in bytes of the largest member of a structure

Individual member padding:

// Does this offset need padding first for correct alignment?
            if (iDataTypeSize < nAlign)
            {
                nTemp = iOffset % iDataTypeSize;
                if(nTemp)
                    nTemp = iDataTypeSize - nTemp;
            }
            else
            {
                nTemp = iOffset % nAlign;
                if(nTemp)
                    nTemp = nAlign - nTemp;
            }
            iOffset += nTemp;                   // Update offset with padding

End of structure padding:

// Ensure the end of the structure is padded based on the size of the largest member
    if (nLargestSize < nAlign)
    {
        nTemp = iOffset % nLargestSize;
        if(nTemp)
            nTemp = nLargestSize - nTemp;
    }
    else
    {
        nTemp = iOffset % nAlign;
        if(nTemp)
            nTemp = nAlign - nTemp;
    }
    iOffset = iOffset + nTemp;              // Update offset with padding

  • Administrators
Posted

I'm pretty sure it should always pad but I'm struggling to find exactly how align should affect the end padding. All the docs I've looked at just say "changing align also has an effect on the end padding" but don't say how.

Just did some tests in C++ and the value of align (#pragma pack) seems to follow the same rules for the end of structure padding. So the code above seems to be correct (and the current beta is wrong for non-default alignment)
  • Administrators
Posted

Hmm, still not quite right:

$sStruct='align 2;int;struct;double;endstruct;byte'
$stStr=DllStructCreate($sStruct)
MsgBox(0, "", "Struct size:"&DllStructGetSize($stStr)&@CRLF)

This gives me a size of 18, but it should be 14 according to this:

# pragma pack (2)
 struct 
 {
     int    m1;
     struct 
     {
         double m1;
     } inner;
     unsigned char m3;
 } test1;

    printf("test1 size: %d\n", sizeof(test1));

Posted

Jon,

What is AutoIt3_struct.exe supposed to do? For me it just NOPs on au3 files.

Anyway, I don't believe you need the tests as in the code you posted.

Individual member:

// Does this offset need padding first for correct alignment of next member whose size is iDataTypeSize?
            nTemp = min(iDataTypeSize, nAlign);
            iOffset += (nTemp - (iOffset % nTemp)) % nTemp;                 // Update offset with padding

Struct tail:

// Ensure the end of the structure is padded based on the size of the smallest of (largest member or pack size)
            nTemp = min(nLargestSize, nAlign);
            iOffsetIn = iOffset + (nTemp - (iOffset % nTemp)) % nTemp;      // Update offset with padding

You might be able to combine both parts into one, depending on how your code is structured.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

  • Administrators
Posted

3.3.7.3 (27th May, 2011) (Beta)

Minor visible changes, but I've been changing some underlying code while testing the VC10 Code Analysis to get rid of some warnings, so useful to release often to make sure I'm not breaking stuff :huh2:

- Fixed: More DllStruct alignment fixes.

UDFs:

- Fixed #1933: _GetIP is outdated and could possibly fail.

Report issues here.

Download here.

Posted

3.3.7.3 crashes on parsing a (meaningless) align directive at the end of a struct:

Local $s5 = DllStructCreate("int; STRUCT; short; char; align 1; ENDSTRUCT; char")
Local $s6 = DllStructCreate("int; align")

Another issue is that the end of struct padding isn't always correct:

Local $test = DllStructCreate("double m1; STRUCT; short mm1; align 1; char mm2; ENDSTRUCT; char m3")

ConsoleWrite(StringFormat('test                 size: % 3d\n', DllStructGetSize($test)))
ConsoleWrite(StringFormat('   m1    offset: % 3d   size: % 3d   padding: % 3d\n', DllStructGetPtr($test, 1) - DllStructGetPtr($test), 8, DllStructGetPtr($test, 2) - DllStructGetPtr($test, 1) - 8))
ConsoleWrite(StringFormat('   m2    offset: % 3d   size: % 3d   padding: % 3d\n', DllStructGetPtr($test, 2) - DllStructGetPtr($test), 3, DllStructGetPtr($test, 4) - DllStructGetPtr($test, 2) - 3))
ConsoleWrite(StringFormat('   m2.mm1 offset: % 3d   size: % 3d  padding: % 3d\n', DllStructGetPtr($test, 2) - DllStructGetPtr($test), 2, DllStructGetPtr($test, 3) - DllStructGetPtr($test, 2) - 2))
ConsoleWrite(StringFormat('   m2.mm2 offset: % 3d   size: % 3d  padding: % 3d\n', DllStructGetPtr($test, 3) - DllStructGetPtr($test), 1, DllStructGetPtr($test, 4) - DllStructGetPtr($test, 3) - 1))
ConsoleWrite(StringFormat('   m3    offset: % 3d   size: % 3d   padding: % 3d\n', DllStructGetPtr($test, 4) - DllStructGetPtr($test), 1, DllStructGetSize($test) - Number(DllStructGetPtr($test, 4) - DllStructGetPtr($test, 1))))

3.3.7.3 adds 4 padding bytes at the end of the outer struct.

While flat C GCC says otherwise:

#include <stdio.h>
#include <stddef.h>

# pragma pack (8)
struct test {
    double m1;
    struct {
        short mm1;
# pragma pack (1)
        char mm2;
    } m2;
    char m3;
} test;

void main(
    int argc,
    char **argv
){
    printf("test                        size: % 3d\n", sizeof(test));
    printf("    m1      offset: % 3d    size: % 3d  padding: % 3d\n", offsetof(struct test, m1), sizeof(test.m1), offsetof(struct test, m2) - offsetof(struct test, m1) - sizeof(test.m1));
    printf("    m2      offset: % 3d    size: % 3d  padding: % 3d\n", offsetof(struct test, m2), sizeof(test.m2), offsetof(struct test, m3) - offsetof(struct test, m2) - sizeof(test.m2));
    printf("    m2.mm1   offset: % 3d   size: % 3d  padding: % 3d\n", offsetof(struct test, m2.mm1), sizeof(test.m2.mm1), offsetof(struct test, m2.mm2) - offsetof(struct test, m2.mm1) - sizeof(test.m2.mm1));
    printf("    m2.mm2   offset: % 3d   size: % 3d  padding: % 3d\n", offsetof(struct test, m2.mm2), sizeof(test.m2.mm2), offsetof(struct test, m3) - offsetof(struct test, m2.mm2) - sizeof(test.m2.mm2));
    printf("    m3      offset: % 3d    size: % 3d  padding: % 3d\n", offsetof(struct test, m3), sizeof(test.m3), sizeof(test) - offsetof(struct test, m3) -  sizeof(test.m3));
}

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Posted (edited)

I think, this should make it clear: http://msdn.microsoft.com/en-us/library/2e70t5y1%28VS.80%29.aspx

pack takes effect at the first struct, union, or class declaration after the pragma is seen

So # pragma pack (1) in jchd's C-source should do nothing in those two structs (at least if GCC interprets it the same as VC++)

Edited by ProgAndy

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Posted

If pragma pack is only "taking effect at the first (I read "next") struct after the pragma is seen, then inndeed GCC is plain wrong.

Either that or MS description isn't precise enough and doesn't cope with pack inside structs, which seems the interpretation GCC does.

Maybe the MS wording is placed in the context of opposing /Zp setting and pragma pack(...).

What does VC say in compiling the C code? Does it bark, or give yet another packing/padding result?

Now given that GCC is heavily tested on a huge number of platforms, including cross-compiles, I hardly think it departs from "pure" C, whatever pure means.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

  • Administrators
Posted

VC says:

test                        size:  12
    m1      offset:   0 size:   8  padding:   0
    m2      offset:   8 size:   3  padding:   0
    m2.mm1   offset:   8   size:   2  padding:   0
    m2.mm2   offset:  10   size:   1  padding:   0
    m3      offset:  11 size:   1  padding:   0

Posted (edited)

I think, no one really modifies the packing inside a struct.

This is the output of VC++2010 (cl.exe /EHsc)

test                        size:  12
    m1      offset:   0    size:   8  padding:   0
    m2      offset:   8    size:   3  padding:   0
    m2.mm1   offset:   8   size:   2  padding:   0
    m2.mm2   offset:  10   size:   1  padding:   0
    m3      offset:  11    size:   1  padding:   0

Too late...

Edited by ProgAndy

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Posted

Means that pack takes effect as soon as encountered. Hopefully, they agree in practice.

Then, the code I posted (with seamingly redundant %) should work. I don't have time to check that right now, I must leave.

Jon, sorry for reopening this can of worms!

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

  • Administrators
Posted

Means that pack takes effect as soon as encountered. Hopefully, they agree in practice.

Then, the code I posted (with seamingly redundant %) should work. I don't have time to check that right now, I must leave.

Jon, sorry for reopening this can of worms!

I don't think it's that. IIRC when moving back up a struct level I am restoring the previous align value. So by the time it gets right to the end it's working with align=8 and pads accordingly. I'll just remove the "restore" code and hopefully it will work.
Posted

Ha, you were doing a hiden pack(pop) ...

Cross fingers!

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

  • Administrators
Posted

This is what I get with the change. Size looks right, not sure why it's saying there is 1 byte padding. Bug in the test script?

test                size:  12
   m1   offset:   0   size:   8   padding:   0
   m2   offset:   8   size:   3   padding:   0
   m2.mm1 offset:   8   size:   2  padding:   0
   m2.mm2 offset:  10   size:   1  padding:   0
   m3   offset:  11   size:   1   padding:   1

Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...