Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cstruct initialisation dangerous #156

Closed
skaller opened this issue Sep 27, 2020 · 2 comments
Closed

cstruct initialisation dangerous #156

skaller opened this issue Sep 27, 2020 · 2 comments

Comments

@skaller
Copy link
Member

skaller commented Sep 27, 2020

Currently I think cstructs are initialised like structs, with a reinterpret cast. Not sure atm. It is certainly the case if the fields are listed out out of order, the result corrupts memory. Memberwise initialisation should be used to ensure the correct members are initialised.

i think part of the reason is C89 provides no way to initialise a struct in an expression, which Felix has to do. However I think C++ now does and I think C99 designated initialisers could also be used possibly wrapped inside a C++ lambda, although exactly how a copy can be avoided I'm not sure.

Binding is complicated because it is split up in multiple places.

@skaller
Copy link
Member Author

skaller commented Sep 27, 2020

#include <stdio.h>

struct X {int a; double b; };
void f (X const &x) { printf ("a=%d b=%f\n",x.a,x.b); }
int main() {
  f( X{.a=1,.b=43.0} ); 
}

correct method

@skaller
Copy link
Member Author

skaller commented Sep 28, 2020

Now, test case:

requires header "struct X { double b; int a; };";
cstruct X { int a; double b; };
var x = X (a=1,b=2.3);
println$ x._strr;

works correctly, generating this:

     ptf-> x  = X{.a=1, .b=2.3}/* apply cstruct*/; //init

Notice the C struct decl is out of order with the cstruct decl.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant