New C features in GCC 13 (Red Hat Developer)
The nullptr constant first appeared in C++11, described in proposal N2431 from 2007. Its purpose was to alleviate the problems with the definition of NULL, which can be defined in a variety of ways: (void *)0 (a pointer constant), 0 (an integer), and so on. This posed problems for overload resolution, generic programming, etc. While C doesn’t have function overloading, the protean definition of NULL still causes headaches.
Posted May 4, 2023 19:31 UTC (Thu)
by bartoc (guest, #124262)
[Link] (18 responses)
Posted May 4, 2023 19:48 UTC (Thu)
by kreijack (guest, #43513)
[Link] (7 responses)
Another interesting thing that I hope to be standardized is the GCC[*] __attribute__((cleanup)) will be standardized (with a better syntax).
[*] However IIRC also clang has the same attribute.
Posted May 4, 2023 19:56 UTC (Thu)
by mpolacek (guest, #66426)
[Link] (6 responses)
Posted May 4, 2023 21:04 UTC (Thu)
by mss (subscriber, #138799)
[Link] (5 responses)
Posted May 5, 2023 4:09 UTC (Fri)
by alison (subscriber, #63752)
[Link]
CppCast Episode 297 had a great interview with JeanHeyd Meneide called "Defer is better than destructors" in which they wax eloquent on this topic. Meneide also says that defer and lambdas would make writing a C container library possible.
Posted May 5, 2023 22:36 UTC (Fri)
by bartoc (guest, #124262)
[Link] (3 responses)
#include <stdio.h>
void bar(void (*fn)(void)) {
int main(void) {
This is a legitimate lambda function, and it relies on a sneaky combination of statement expressions and nested functions. Sadly using this feature will mark your stack as executable, and indeed it writes out thunk code to the stack, and executes it (the fn pointer here points to someplace on the stack).
Posted May 6, 2023 14:08 UTC (Sat)
by pebolle (guest, #35204)
[Link]
I ended up in a rabbit hole full of GCC extensions, GCC en AS options, ELF sections, the execstack program, allow_execstack for selinux and what not. Lots of stuff I probably forget before I dive in that hole again.
(That you used "fn" for three different things - a parameter, a function and a nested function - gave me an opportunity to scratch my head quite a bit.)
Posted May 8, 2023 6:20 UTC (Mon)
by eru (subscriber, #2753)
[Link] (1 responses)
Posted May 10, 2023 0:31 UTC (Wed)
by DemiMarie (subscriber, #164188)
[Link]
Posted May 4, 2023 21:13 UTC (Thu)
by atai (subscriber, #10977)
[Link] (1 responses)
Posted May 5, 2023 22:37 UTC (Fri)
by bartoc (guest, #124262)
[Link]
Posted May 5, 2023 9:24 UTC (Fri)
by ballombe (subscriber, #9523)
[Link] (7 responses)
Posted May 5, 2023 22:21 UTC (Fri)
by zev (subscriber, #88455)
[Link] (1 responses)
Posted May 8, 2023 13:23 UTC (Mon)
by mpolacek (guest, #66426)
[Link]
Posted May 5, 2023 22:39 UTC (Fri)
by bartoc (guest, #124262)
[Link]
Posted May 6, 2023 0:23 UTC (Sat)
by NYKevin (subscriber, #129325)
[Link] (3 responses)
Nullptr is potentially more of a problem, but if anyone was going around calling their local variables or typedefs "nullptr," they probably deserve for it to break.
Posted May 6, 2023 18:35 UTC (Sat)
by ballombe (subscriber, #9523)
[Link] (2 responses)
Posted May 6, 2023 23:56 UTC (Sat)
by NYKevin (subscriber, #129325)
[Link] (1 responses)
Posted May 7, 2023 7:12 UTC (Sun)
by epa (subscriber, #39769)
[Link]
Personally I would much prefer they add real keywords and give a good honest build failure (which is usually trivial to fix) rather than play games with macros or cruft up the syntax with double-underscore.
Posted May 4, 2023 21:45 UTC (Thu)
by ceplm (subscriber, #41334)
[Link] (21 responses)
I am just a stupid Pythonista, but is there anybody really who would willingly routinely use types like “int_least32_t”?
Beautiful is better than ugly!
Posted May 5, 2023 0:56 UTC (Fri)
by branden (guest, #7029)
[Link] (8 responses)
With a plain "int" you get an integer of whatever size the platform feels like providing. Is it a fast int? Is it a small int? Undefined.
Posted May 5, 2023 6:56 UTC (Fri)
by mb (subscriber, #50428)
[Link] (7 responses)
Posted May 5, 2023 9:35 UTC (Fri)
by ballombe (subscriber, #9523)
[Link] (5 responses)
Posted May 5, 2023 10:03 UTC (Fri)
by adobriyan (subscriber, #30858)
[Link] (1 responses)
Posted May 5, 2023 22:40 UTC (Fri)
by bartoc (guest, #124262)
[Link]
Posted May 5, 2023 10:05 UTC (Fri)
by hkario (subscriber, #94864)
[Link]
Posted May 5, 2023 12:57 UTC (Fri)
by HenrikH (subscriber, #31152)
[Link] (1 responses)
Posted May 5, 2023 15:19 UTC (Fri)
by magfr (subscriber, #16052)
[Link]
GCC on x86_64 have supported _int128 for years but glibc have not.
Posted May 10, 2023 0:36 UTC (Wed)
by DemiMarie (subscriber, #164188)
[Link]
Posted May 5, 2023 12:08 UTC (Fri)
by vadim (subscriber, #35271)
[Link] (7 responses)
int32_t and the like is the usual sight when you're working with things like file formats and network protocols.
int_least32_t is means you want 32 bits if at all possible, but more is fine. This would be useful for storing large amounts of data, where you want to minimize the size, unless it can't be done. As to what kind of platform couldn't give you a 16 or 32 bit integer, I think it's mostly intended for DSPs and specialized architectures. So this probably mostly gets used for things like codecs. I've never had a reason to use these.
int_fast32_t means you need at least 32 bits, but if there's a faster bigger datatype, use that instead. On x86_64, asking for int_fast16_t gives you a 64 bit value.
These are not a great fit for something like printf and the like but I think for the most part that's not that big of a deal, as the main usage is in cases where you're just dealing with data, encoding something, etc.
Posted May 5, 2023 12:55 UTC (Fri)
by HenrikH (subscriber, #31152)
[Link]
Posted May 5, 2023 15:17 UTC (Fri)
by adobriyan (subscriber, #30858)
[Link] (5 responses)
This is very wasteful, 16-bit arithmetic is just as fast on x86_64 as other bits.
Posted May 5, 2023 18:31 UTC (Fri)
by vadim (subscriber, #35271)
[Link] (2 responses)
Posted May 6, 2023 0:51 UTC (Sat)
by NYKevin (subscriber, #129325)
[Link] (1 responses)
As a result, while an implementation is permitted to consider code length in deciding which type to use for int_fastXX_t, in practice the implementation must select *one* fixed width (for each int_fastXX_t type) and use it throughout the entire ABI. If you think about it, this is pretty much required anyway, because otherwise there could be no separate compilation of functions taking int_fastXX_t as an argument.
Posted May 6, 2023 14:32 UTC (Sat)
by mathstuf (subscriber, #69389)
[Link]
With the one exception that `char`, `signed char`, and `unsigned char` are distinct.
Posted May 5, 2023 19:49 UTC (Fri)
by joib (subscriber, #8541)
[Link] (1 responses)
Posted May 5, 2023 20:14 UTC (Fri)
by adobriyan (subscriber, #30858)
[Link]
Posted May 5, 2023 22:43 UTC (Fri)
by bartoc (guest, #124262)
[Link] (3 responses)
Posted May 8, 2023 13:19 UTC (Mon)
by mpolacek (guest, #66426)
[Link]
Posted May 10, 2023 0:39 UTC (Wed)
by DemiMarie (subscriber, #164188)
[Link] (1 responses)
Posted May 10, 2023 18:03 UTC (Wed)
by NYKevin (subscriber, #129325)
[Link]
(I suppose it would be nice to have e.g. 37 bit ints that wrap or saturate at 1 << 37, but IMHO that seems like a pretty niche use case. Just use min()/max() and/or the modulus operator as needed.)
Posted May 13, 2023 6:51 UTC (Sat)
by oldtomas (guest, #72579)
[Link] (1 responses)
TFA screams at me "Sorry, you need to enable JavaScript to visit this website." (no shit: bold red letters).
"Uh, no?" me thinks. Then I had a cursory look at the javascript code they so unpolitely wanted me to execute. Surveillance capitalism at its cusp.
I'll pass and read up at gcc.gnu.org.
Sad. I used to be a fan of Redhat.
Posted May 18, 2023 18:57 UTC (Thu)
by jwilk (subscriber, #63328)
[Link]
New C features in GCC 13 (Red Hat Developer)
New C features in GCC 13 (Red Hat Developer)
There actually is a proposal for a feature along the lines of New C features in GCC 13 (Red Hat Developer)
__attribute__((cleanup)): N2895.
New C features in GCC 13 (Red Hat Developer)
There actually is a proposal for a feature along the lines of __attribute__((cleanup))
Shame that the defer feature didn't make C23 - neither did its dependency of lambda functions.
While having lambda functions support in the language just save some typing when implementing callbacks, having the defer feature would allow implementing pseudo-RAII in a standard complaint way - so it would be an important improvement.
Without that standard support current implementations, like Glib's g_autoptr(), have to resort to per-compiler extensions like the aforementioned GCC's attribute.
In general, I think it's very good that, as the article says, these proposals align the C and C++ languages a little bit closer to each other by entwining certain features, and make programming in C easier and more secure.
New C features in GCC 13 (Red Hat Developer)
New C features in GCC 13 (Red Hat Developer)
fn();
}
int a = 4;
void (*fn)(void) = ({void fn(void) {
printf("look at me! a: %d\n", a);
} &fn;});
bar(fn);
}
New C features in GCC 13 (Red Hat Developer)
New C features in GCC 13 (Red Hat Developer)
New C features in GCC 13 (Red Hat Developer)
New C features in GCC 13 (Red Hat Developer)
New C features in GCC 13 (Red Hat Developer)
New C features in GCC 13 (Red Hat Developer)
This will cause macros to produce unintended behavior.
It is way too late to make this kind of change to the language.
New C features in GCC 13 (Red Hat Developer)
New C features in GCC 13 (Red Hat Developer)
New C features in GCC 13 (Red Hat Developer)
New C features in GCC 13 (Red Hat Developer)
New C features in GCC 13 (Red Hat Developer)
""This proposal harmonizes C and C++ further by making alignas, alignof, bool, false, static_assert, thread_local, and true ordinary keywords in C2X mode.""
New C features in GCC 13 (Red Hat Developer)
New C features in GCC 13 (Red Hat Developer)
New C features in GCC 13 (Red Hat Developer)
>
> enum F : int { A = 0x8000 } f;
>
> […] Thus a better variant would be to use one of the types defined in <stdint.h>, for example:
>
> enum F : int_least32_t { A = 0x8000 } f;
New C features in GCC 13 (Red Hat Developer)
New C features in GCC 13 (Red Hat Developer)
And the fast/least types are BS. Sprinkling the code with ugly fast-types doesn't make it any faster. It just increases the likelihood of subtle bugs due to actual type size differences between platforms.
Just use fixed types like uint32_t.
New C features in GCC 13 (Red Hat Developer)
New C features in GCC 13 (Red Hat Developer)
New C features in GCC 13 (Red Hat Developer)
New C features in GCC 13 (Red Hat Developer)
New C features in GCC 13 (Red Hat Developer)
New C features in GCC 13 (Red Hat Developer)
intmax_t tend to get frozen when ABIs are set and afterwards they wont change even if the compiler supports wider types.
Some embedded platforms do not define all of the fixed-size integer types! For instance, some DSPs cannot access individual bytes, so New C features in GCC 13 (Red Hat Developer)
char is 16 bits and uint8_t doesn’t exist. int_least8_t could be useful for documentation there.
New C features in GCC 13 (Red Hat Developer)
New C features in GCC 13 (Red Hat Developer)
New C features in GCC 13 (Red Hat Developer)
New C features in GCC 13 (Red Hat Developer)
New C features in GCC 13 (Red Hat Developer)
New C features in GCC 13 (Red Hat Developer)
New C features in GCC 13 (Red Hat Developer)
New C features in GCC 13 (Red Hat Developer)
New C features in GCC 13 (Red Hat Developer)
New C features in GCC 13 (Red Hat Developer)
Can compilers start warning about not using New C features in GCC 13 (Red Hat Developer)
_BitInt(N) at some point? Probably not because backwards compatibility, but it would be nice. Also Rust should get _BitInt(N).
New C features in GCC 13 (Red Hat Developer)
New C features in GCC 13 (Red Hat Developer)
New C features in GCC 13 (Red Hat Developer)
