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

`sizeof` still broken with regard to bitsize/packed bitfields #10082

c-blake opened this issue Dec 23, 2018 · 2 comments


None yet
2 participants
Copy link

commented Dec 23, 2018

  A = int8        # change to int16 and get sizeof(C)==6
  B = int16
  C = object {.packed.}
    d {.bitsize:  1.}: A
    e {.bitsize:  7.}: A
    f {.bitsize: 16.}: B

echo sizeof(C)    # prints 4

import macros     # Lifted from tests/misc/tsizeof.nim
macro c_sizeof(a: typed): int32 =
  result = quote do:
    var res: int32
    {.emit: [res, " = sizeof(", `a`, ");"] .}

echo c_sizeof(C)  # prints 3

This seems pretty wrong. This may be related to #9668 . As mentioned in my side-comment, the answer can be made even more wrong with a wider Nim integer type for A.

@Araq Araq closed this in 062d7e3 Dec 30, 2018


This comment has been minimized.

Copy link
Contributor Author

commented Dec 30, 2018

You probably want to re-open this:

  O = object {.packed.}
    a: uint32
    b {.bitsize:  8.}: uint8
    c {.bitsize:  1.}: uint8
    d {.bitsize:  7.}: uint8
    e {.bitsize: 16.}: uint16
    f: uint32

echo sizeof(O)
# prints 1 with both `a`, `f` declared
# Also prints 1 with `a` commented out
# prints 4 (correctly!) if both `a`, `f` commented out

@dom96 dom96 reopened this Dec 30, 2018


This comment has been minimized.

Copy link
Contributor Author

commented Dec 30, 2018

Thanks for re-opening, @dom96. Presumably, Nim does not just emit C/C++ sizeof because of the Javascript backend? Things like {.packed.} probably also don't fail (or at least do nothing) on the JS backend. Maybe {.packed.} should do nothing on JS and elsewise emit C/C++ sizeof? Just a thought. I haven't looked at the code/VC history in detail, but it "smells like" something better deferred to the backend if possible.

krux02 added a commit to krux02/Nim that referenced this issue Jan 7, 2019

@krux02 krux02 closed this in #10227 Jan 7, 2019

krux02 added a commit that referenced this issue Jan 7, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.