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

Size fo empty structs in C is zero byte. #782

Merged
merged 2 commits into from Apr 2, 2020
Merged

Conversation

ldm0
Copy link
Contributor

@ldm0 ldm0 commented Mar 13, 2020

#include<stdio.h>

struct A {
};

int main() {
        struct A a;
        printf("%ld\n", sizeof(a));
}
$ gcc tmp.c
$ ./a.out
0
$ g++ tmp.c
$ ./a.out
1

@eddyb
Copy link
Member

eddyb commented Mar 13, 2020

AFAIK, empty structs in C are a GNU C extension (implemented in GCC and Clang).
That is, the standard disallows that situation but GCC went ahead and supported it.

I wonder if using -std=c11 changes the behavior (the default is something like gnu11).

@eddyb
Copy link
Member

eddyb commented Mar 13, 2020

cc @nagisa @hanna-kruppe @gnzlbg

@ldm0
Copy link
Contributor Author

ldm0 commented Mar 13, 2020

I wonder if using -std=c11 changes the behavior (the default is something like gnu11).

$ gcc tmp.c -std=c11
$ ./a.out
0

@eddyb
Copy link
Member

eddyb commented Mar 13, 2020

Maybe -Wall -Wextra might show something, I hate how many warnings are off by default.

@ldm0
Copy link
Contributor Author

ldm0 commented Mar 13, 2020

Maybe -Wall -Wextra might show something, I hate how many warnings are off by default.

The gcc doesn't complain about this though.

$ gcc tmp.c -Wall -Wextra
$ ./a.out
0

This is really a UB in C:

If the struct-declaration-list contains no named members, the behavior is undefined.

But since this is what we got, we should at least note the reader right?

@ldm0
Copy link
Contributor Author

ldm0 commented Mar 13, 2020

Note:

$ clang tmp.c -Wall -Wextra -std=c11
$ ./a.out
0
$ gcc tmp.c -Wall -Wextra -std=c11
$ ./a.out
0

@hanna-kruppe
Copy link

For comparison, this is also discussed in the UCG. I agree the reference should be more correct about this, but the current diff is not the best we can do. I suggest following the UCG in explicitly distinuishing C standard vs what common C compilers do vs C++.

@eddyb Indeed there is a warning about empty structs in C being an extension: -Wpedantic.

@eddyb
Copy link
Member

eddyb commented Mar 13, 2020

@hanna-kruppe Thanks, and it also looks like -std=c11 doesn't affect that warning 😞.

@ehuss
Copy link
Contributor

ehuss commented Mar 31, 2020

The changes here look good to me, and mostly repeats what is said in UCG. @eddyb or @hanna-kruppe should we just merge this?

@hanna-kruppe
Copy link

I don't have permissions to merge this, but from my perspective it's a fine improvement (aside from the nit I just left).

@Centril
Copy link
Contributor

Centril commented Mar 31, 2020

I'll leave merging this to @ehuss once @hanna-kruppe thinks its ready.

@hanna-kruppe
Copy link

I think this is ready.

@ldm0
Copy link
Contributor Author

ldm0 commented Apr 2, 2020

Ping @Centril

Copy link
Contributor

@Centril Centril left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving on behalf of @hanna-kruppe

@Centril Centril merged commit 8e7e6a0 into rust-lang:master Apr 2, 2020
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request Apr 14, 2020
Update books

## reference

13 commits in 89dd146..3ce94ca
2020-03-31 09:42:10 -0700 to 2020-04-11 17:00:27 +0200
- UB definition: mention size_of_val for determining a reference's span (rust-lang/reference#793)
- Emphasize that `#[inline]` in all shapes is a hint. (rust-lang/reference#791)
- Added missing comma to Unions reference (rust-lang/reference#790)
- Attributes are now allowed on `if` and `if let` (rust-lang/reference#783)
- `enum`s can now be `#[repr(transparent)]` (rust-lang/reference#741)
- Undocument `use` paths. (rust-lang/reference#665)
- Clarify pub(restricted) a bit (rust-lang/reference#481)
- Add information about || and && to grammar describing `while let`. (rust-lang/reference#772)
- Document the `automatically_derived` attribute. (rust-lang/reference#555)
- Fix unstable check. (rust-lang/reference#743)
- Use common script for link checking. (rust-lang/reference#765)
- Add a basic style guide. (rust-lang/reference#787)
- Size fo empty structs in C is zero byte. (rust-lang/reference#782)

## book

14 commits in c8841f2841a2d26124319ddadd1b6a245f9a1856..f5db319e0b19c22964398d56bc63103d669e1bba
2020-03-22 09:07:01 -0500 to 2020-04-13 08:06:03 -0500
- Update ch01-01-installation.md to require TLS 1.2 (rust-lang/book#2301)
- traits as parameters: Use references instead of moving the values (rust-lang/book#2239)
- Fix a broken link
- Update go docs link (rust-lang/book#2285)
- Add the farsi translation repo link (rust-lang/book#2283)
- Add the missing word to the sentence (ch06-2) (rust-lang/book#2278)
- Fixes hardcoded output (rust-lang/book#2276)
- Use rust-lang/rust linkchecker on CI. (rust-lang/book#2272)
- Add union to the list of keywords (rust-lang/book#2271)
- Clarify the wording (rust-lang/book#2256)
- Improve sentence flow (rust-lang/book#2255)
- Add missing apostrophe (rust-lang/book#2247)
- Update cargo profiles link. (rust-lang/book#2245)
- Add note about chapter 18 in chapter 6 (rust-lang/book#2238)

## rust-by-example

1 commits in a6638463efc7631bc0e8dc67ccd256d4e1b61f1a..c106d1683c3a2b0960f0f0fb01728cbb19807332
2020-04-06 09:39:03 -0500 to 2020-04-09 09:14:39 -0300
- Improve the conversion example (rust-lang/rust-by-example#1329)

## edition-guide

1 commits in 37f9e6848411188a1062ead1bd8ebe4b8aa16899..8204c1d123472cd17f0c1c5c77300ae802eb0271
2020-02-10 14:36:14 +0100 to 2020-04-09 18:55:50 -0700
- Fix forge link. (rust-lang/edition-guide#200)

## embedded-book

1 commits in d22a9c487c78095afc4584f1d9b4ec43529d713c..668fb07b6160b9c468f598e839c1e044db65de30
2020-03-04 09:46:30 +0000 to 2020-04-13 12:38:16 +0000
- Add triagebot configuration  (rust-embedded/book#232)
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request Apr 14, 2020
Update books

## reference

13 commits in 89dd146..3ce94ca
2020-03-31 09:42:10 -0700 to 2020-04-11 17:00:27 +0200
- UB definition: mention size_of_val for determining a reference's span (rust-lang/reference#793)
- Emphasize that `#[inline]` in all shapes is a hint. (rust-lang/reference#791)
- Added missing comma to Unions reference (rust-lang/reference#790)
- Attributes are now allowed on `if` and `if let` (rust-lang/reference#783)
- `enum`s can now be `#[repr(transparent)]` (rust-lang/reference#741)
- Undocument `use` paths. (rust-lang/reference#665)
- Clarify pub(restricted) a bit (rust-lang/reference#481)
- Add information about || and && to grammar describing `while let`. (rust-lang/reference#772)
- Document the `automatically_derived` attribute. (rust-lang/reference#555)
- Fix unstable check. (rust-lang/reference#743)
- Use common script for link checking. (rust-lang/reference#765)
- Add a basic style guide. (rust-lang/reference#787)
- Size fo empty structs in C is zero byte. (rust-lang/reference#782)

## book

14 commits in c8841f2841a2d26124319ddadd1b6a245f9a1856..f5db319e0b19c22964398d56bc63103d669e1bba
2020-03-22 09:07:01 -0500 to 2020-04-13 08:06:03 -0500
- Update ch01-01-installation.md to require TLS 1.2 (rust-lang/book#2301)
- traits as parameters: Use references instead of moving the values (rust-lang/book#2239)
- Fix a broken link
- Update go docs link (rust-lang/book#2285)
- Add the farsi translation repo link (rust-lang/book#2283)
- Add the missing word to the sentence (ch06-2) (rust-lang/book#2278)
- Fixes hardcoded output (rust-lang/book#2276)
- Use rust-lang/rust linkchecker on CI. (rust-lang/book#2272)
- Add union to the list of keywords (rust-lang/book#2271)
- Clarify the wording (rust-lang/book#2256)
- Improve sentence flow (rust-lang/book#2255)
- Add missing apostrophe (rust-lang/book#2247)
- Update cargo profiles link. (rust-lang/book#2245)
- Add note about chapter 18 in chapter 6 (rust-lang/book#2238)

## rust-by-example

1 commits in a6638463efc7631bc0e8dc67ccd256d4e1b61f1a..c106d1683c3a2b0960f0f0fb01728cbb19807332
2020-04-06 09:39:03 -0500 to 2020-04-09 09:14:39 -0300
- Improve the conversion example (rust-lang/rust-by-example#1329)

## edition-guide

1 commits in 37f9e6848411188a1062ead1bd8ebe4b8aa16899..8204c1d123472cd17f0c1c5c77300ae802eb0271
2020-02-10 14:36:14 +0100 to 2020-04-09 18:55:50 -0700
- Fix forge link. (rust-lang/edition-guide#200)

## embedded-book

1 commits in d22a9c487c78095afc4584f1d9b4ec43529d713c..668fb07b6160b9c468f598e839c1e044db65de30
2020-03-04 09:46:30 +0000 to 2020-04-13 12:38:16 +0000
- Add triagebot configuration  (rust-embedded/book#232)
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

Successfully merging this pull request may close these issues.

None yet

5 participants