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

Fix partial array initialization #262

Closed
yulvil opened this issue Oct 23, 2017 · 4 comments
Closed

Fix partial array initialization #262

yulvil opened this issue Oct 23, 2017 · 4 comments

Comments

@yulvil
Copy link
Contributor

yulvil commented Oct 23, 2017

int a[4] = {10, 20}; should be initialized as {10, 20, 0, 0}

currently transpiles to: var a []int = []int{nil, 10, 20}
error: cannot convert nil to type int

C99 specs: http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf, page 127, item 21

If there are fewer initializers in a brace-enclosed list than there are elements or members
of an aggregate, or fewer characters in a string literal used to initialize an array of known
size than there are elements in the array, the remainder of the aggregate shall be
initialized implicitly the same as objects that have static storage duration.
@elliotchance
Copy link
Owner

Excellent reference! 👍

@yulvil
Copy link
Contributor Author

yulvil commented Nov 3, 2017

@elliotchance It turns out that go behaves the same way in terms of partial array initialization. The rest of the array is assigned the zero-value for that type. The keyword here is array.

The problem is that we convert C arrays to Go slices.

Go https://play.golang.org/p/g2XcSacYz5

var a [4]int = [4]int{10,20}
fmt.Println(a[0],a[1],a[2],a[3]) // -> 10 20 0 0

If we convert arrays to slices (current c2go behavior):

var a []int = []int{nil, 10, 20} // nil is the ArrayFiller
fmt.Println(a[0],a[1],a[2],a[3]) // -> 1 2 <arrayOutOfBounds>

Should we convert C fixed arrays as Go fixed arrays instead of slices?

@elliotchance
Copy link
Owner

Hmmm you bring up a good point. Fixed size arrays will have to be equivalent (fixed size) in Go, otherwise assigning to an index in the capacity would fail terribly. The alternative (to simplify pointers) is we could always use slices, but have slices preallocated when created.

@yulvil
Copy link
Contributor Author

yulvil commented Nov 3, 2017

I think it is ok to use slices for pointers and strings, but my first impression is to convert C fixed-sized arrays to go fixed-sized arrays.

I did not see that many partial initialization (like in the example), so I will refrain from making any changes until we confirm that it is necessary to transpile sqlite3.c.

I would label this issue as a "low".

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

2 participants