How to use a struct parameter from the `size` function/expression?

I’d like to access the struct parameter from within the size function/expression, but have found no way to do so. The struct’s parameter is available somewhere, even before the struct body is executed, since that body needs its value to set up the variables etc. However, it seems that it’s inaccessible from within size.

I have the following struct:

typedef struct (int L) {
    uint8 value[L];
} Misc;

I need to access its parameter to calculate its size.

I can access the parametrized size of the value field in the read function:

// Works
typedef struct (int L) {
    uint8 value[L];
} Misc <read=dump(this.value, sizeof(this.value))>;

But I can’t access it in the size function, as expected, since nothing inside the struct definition has executed yet.

// Doesn't work
typedef struct (int L) {
    uint8 value[L];
} Misc <size=(sizeof(this.value))>;

typedef struct (int L) {
    uint8 value[L];
} Misc <size=this.L>;

It raises ERROR: Variable 'value'/'L' is not a member of the structure.

Trying to use L straightforwardly also fails:

// Doesn't work
typedef struct (int L) {
    uint8 value[L];
} Misc <size=L>;

It raises ERROR: Variable 'L' is undefined.

Using a local buffer variable doesn’t work - as expected, since nothing inside of the struct has executed yet when size is called:

// Doesn't work either
typedef struct (int L) {
    uint8 value[L];
    local int len=L;
} Misc <size=this.len>;

However, there’s no way to access it from the template, without loading some custom DLLs to sniff around 010’s process heap to extract it.

Currently there is no way for the size function to access the struct’s parameters. Please note that the attribute is optional and you generally do not need to use it unless you are running into memory issues. I realize you may be posting sample code, but in this example the size attribute only saves a single variable definition which does not take much memory. If you really need this to work, in the size function you could possible use startof(this) and then use that address to figure out the size, but we’d have to see more of your code to see if that is possible. If there are only a few possible values for L you could have separate structs for each case. In the future we may be able to allow size functions to access parameters and we will take a look.

Graeme
SweetScape Software

1 Like