question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[ENH] Specify default values for C struct declarations

See original GitHub issue

Is your feature request related to a problem? Please describe.

If there is a sturct with many fields, only some of its fields need to be initialized explicitly, and other fields can be implicitly empty-initialized, designators will be useful in c99:

struct MyStruct {
    int a;
    int b;
    int c;
    int d;
    float e;
    float f;
};

int main(void) {
    struct MyStruct s1 = {
        .a = 1,
        .b = 2,
    };
    struct MyStruct s2 = {
        .c = 42,
        .d = 233,
    };
    struct MyStruct s3 = {
        .e = 0.1,
        .f = 0.2,
    };
}

However, cython will leave other fields uninitialized and generate some warnings.

cdef struct MyStruct:
    int a
    int b
    int c
    int d
    float e
    float f

def main():
    cdef MyStruct s1 = MyStruct(
        a = 1,
        b = 2,
    )
    cdef MyStruct s2 = MyStruct(
        c = 42,
        d = 233,
    )
    cdef MyStruct s3 = MyStruct(
        e = 0.1,
        f = 0.2,
    )
warning: test.pyx:10:31: Not all members given for struct 'MyStruct'
warning: test.pyx:10:31: Not all members given for struct 'MyStruct'
warning: test.pyx:14:31: Not all members given for struct 'MyStruct'
warning: test.pyx:14:31: Not all members given for struct 'MyStruct'
warning: test.pyx:18:31: Not all members given for struct 'MyStruct'
warning: test.pyx:18:31: Not all members given for struct 'MyStruct'

The generated C code looks like this:

struct __pyx_t_4test_MyStruct __pyx_v_s2;
struct __pyx_t_4test_MyStruct __pyx_t_1;

/* "test.pyx":14
 *         b = 2,
 *     )
 *     cdef MyStruct s2 = MyStruct(             # <<<<<<<<<<<<<<
 *         c = 42,
 *         d = 233,
 */
__pyx_t_1.c = 42;
__pyx_t_1.d = 0xE9;
__pyx_v_s2 = __pyx_t_1;

Describe the solution you’d like.

It would be helpful if it’s possible to specify default values for the fields of C structures.

cdef struct MyStruct:
    int a = 0
    float b = 0
    void *c = NULL
    int d[4] = [0, 0, 0, 0]

# equivalent to above:

@cython.default_to_zero
cdef struct MyStruct:
    int a
    float b
    void *c
    int d[4]
def main():
    cdef int foo = 42
    cdef MyStruct s = MyStruct(
        b = 1.23,
        c = &foo,
    )

This code behaves the same as if specified a = 0 and d = [0, 0, 0, 0]:

def main():
    cdef int foo = 42
    cdef MyStruct s = MyStruct(
        a = 0,
        b = 1.23,
        c = &foo,
        d = [0, 0, 0, 0],
    )

The whole struct can also be empty-initialized:

cdef struct Inner:
    int a = 0
    int b = 0

cdef struct Outer:
    int a = 0
    int b = 0
    Inner c = Inner()

# equivalent to above:

@cython.default_to_zero
cdef struct Inner:
    int a
    int b

@cython.default_to_zero
cdef struct Outer:
    int a
    int b
    Inner c


def main():
    cdef Outer s = Outer()

Describe alternatives you’ve considered.

No response

Additional context

No response

Issue Analytics

  • State:open
  • Created 9 months ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
da-woodscommented, Dec 19, 2022

Definitely not opposed to this if someone does the work. Don’t think we can implement it in terms of designated initializers though.

0reactions
da-woodscommented, Dec 20, 2022

Due to C99 or due to other limitations?

Partly C++ too, which only as them as a c++20 feature.

But mostly that Cython’s code generation puts the declarations at the start of the function and the initialisation a little way in. That’s partly for C89 reasons, but mostly just so that variables are function scoped like Python

Read more comments on GitHub >

github_iconTop Results From Across the Web

default value for struct member in C - Stack Overflow
Structure is a data type. You don't give values to a data type. You give values to instances/objects of data types. So no...
Read more >
RFC-0022: Clarification: Default values for struct members
Default values MAY be defined on struct members. Defaults appear at the end of a field definition with a C-like = {value} pattern....
Read more >
default struct field initialization values #485 - ziglang/zig - GitHub
As it is now, one must set all field values when initializing a C struct in zig. This quickly becomes cumbersome, especially if...
Read more >
Struct declaration - cppreference.com
A struct is a type consisting of a sequence of members whose storage is allocated in an ordered sequence (as opposed to union,...
Read more >
What is the syntax for setting default values for struct fields in ...
Simple: use the zero value as your default. Ex: Have an enum of choices with a default? Then make the first one your...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found