-
-
Notifications
You must be signed in to change notification settings - Fork 8
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
n3199 - Improved __attribute__((cleanup))
Through defer
#67
Comments
__attribute__((cleanup))
Through defer
__attribute__((cleanup))
Through defer
It took a while, but the paper was fully written. Current standard revision: https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3199.htm |
Thank you for the proposal. I like it very much! I have one question regarding Section 3.8.3: It seems there is no specification over whether Pros Consider the following use case: for (int i = 0; i < 10; i++) {
void *ptr = malloc(8);
if (!ptr)
break;
defer { free(ptr); }
} It seems we should allow such jump over in this example. Also, Cons Some common Personal opinion I implemented a C-based polyfill using During my usage, I find #define _DEFER_MERGE(a,b) a##b
#define _DEFER_VARNAME(a) _DEFER_MERGE(____defer_scopevar_, a)
#define _DEFER_FUNCNAME(a) _DEFER_MERGE(____defer_scopefunc_, a)
#define _DEFER(n) \
auto void _DEFER_FUNCNAME(n)(int *a); \
__attribute__((cleanup(_DEFER_FUNCNAME(n)))) int _DEFER_VARNAME(n); \
void _DEFER_FUNCNAME(n)(int *a)
#define defer _DEFER(__COUNTER__) It closely resembles some behaviors defined by the draft (i.e., example 2, 4, 5 from Section 6.4), which can be tested here: https://godbolt.org/z/3vv5dMMqG |
Thanks for your support! Regarding jumps like
S here is the scope of the for (int i = 0; i < 10; i++) {
void *ptr = malloc(8);
if (!ptr)
break;
defer { free(ptr); }
} is perfectly fine and valid. if int handle = get_handle();
goto meow; // label after defer: using `goto` to jump forwards or backwards over is illegal
defer { release_handle(handle); }
meow0; ;
meow1: ;
printf("hiiii");
goto meow1; // okay I think that covers the cases you were speaking of. Also, even if someone implements |
One small organizational issue: the last paragraph of 3.4 (all of which is duplicated in N3198) states that "for the macros we provide almost every single one will be defined and have the value of |
Most reviewers? Each of the returns inside the loop will leak memory on namelist in addition to all namelist[k]'s for k in [i, n). |
Greetings, if implementation experience from an amateur compiler could help get this through, I wrote a little bit here along with the implementation. |
Some feedback on C++ compatibility (hope this is relevant, since the draft does contain sizable words for that): The lambda based scoped guard implementation under My short discussion with LLVM people: llvm/llvm-project#100869 |
Nothing I can do about that, unfortunately. Maybe if C gets a form of applicable NRVO, but otherwise the intent and the effect is still matching and the same. |
Thank you for the implementation. At the moment |
Thanks, just keep doing what you're doing. I'll keep stalking wg14 documents for fun stuff to implement. |
Hello, I just wanted to ask if any provisions to completely solve the resource leak and dangling resource problems in C were mentioned by the committee during the discussions of this feature. I know this feature is supposed to help with that, and I appreciate it very much, but it doesn't protect the user in a language level manner (you still need "better analysers and linters" and many reviewers to gain confidence about your resource use, and you still cannot be sure that you haven't missed anything). That being said, thank you for working on this, it's really useful. |
C cannot fundamentally solve this problem without changing core parts of C. Have you considered writing Rust? |
I'm currently learning Rust, but that is not what this is about. I was just curious whether the committee considers this a part of a broader strategy, or just a one-off feature. Nevertheless, thank you. |
If you'd like to know the Committee Strategy, you can see the Charter that was updated for C2y, which added a much higher focus on application security: https://www.open-std.org/JTC1/SC22/WG14/www/docs/n3280.htm Nevertheless, the C committee cannot cook up features. People, like me, have to write proposals. They can be coordinated, but generally they are done independently. Other people are trying their own things which they have shown interest in standardizing but have written no papers yet, such as: |
Thanks for all the work that this must have taken. It's exciting to see that things like this are on the cards. I'm curious about the rationale for proposing the The
I'm sure this has had consideration, so I'd like to hear your perspective. I think I saw in the minutes of one of the meetings that there had been some debate on this aspect of the proposal (but there was no detail). |
This is discussed during the "Implementation Experience" section: https://thephd.dev/_vendor/future_cxx/papers/C%20-%20Improved%20__attribute__((cleanup))%20Through%20defer.html#experience Specifically:
|
There is another option, which is |
Latest draft: https://thephd.dev/_vendor/future_cxx/papers/C%20-%20Improved%20__attribute__((cleanup))%20Through%20defer.html
The text was updated successfully, but these errors were encountered: