Skip to content

Obsolete struct initialization syntax #13

Open
@kapec94

Description

@kapec94

The code uses an obsolete GCC extension syntax for struct initialization.

Example:

IsoTpReceiveHandle handle = {
    success: false,
    completed: false,
    arbitration_id: arbitration_id,
    message_received_callback: callback
};

This syntax is obsolete since GCC 2.5 (released in 1993!) (source: Designated Inits, source: GCC releases)

There are a few alternatives to this. One of which is a standarized version of field initialization introduced in C99:

IsoTpReceiveHandle handle = {
    .success = false,
    .completed = false,
    .arbitration_id = arbitration_id,
    .message_received_callback = callback
};

C99 support is quite widespread (source) and even if declared "partial", designated initializers should be available on any major, modern compiler (however, this cross-compiler support doesn't matter, since the code relies on an ancient GCC extension anyway). And to ensure that on GCC it still works as before, a simple preprocessor macro might be prepared to choose between two syntaxes.

There are also other standard options of struct initialization, like using a factory function or setting fields manually. I believe that this would not introduce any performance hit (if that worried you), since there are very few places that use that syntax and in most of them we return the initialized struct immediately.

I might prepare a pull request for that change.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions