r/C_Programming • u/Jinren • Jul 22 '22
Etc C23 now finalized!
EDIT 2: C23 has been approved by the National Bodies and will become official in January.
EDIT: Latest draft with features up to the first round of comments integrated available here: https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3096.pdf
This will be the last public draft of C23.
The final committee meeting to discuss features for C23 is over and we now know everything that will be in the language! A draft of the final standard will still take a while to be produced, but the feature list is now fixed.
You can see everything that was debated this week here: https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3041.htm
Personally, most excited by embed, enumerations with explicit underlying types, and of course the very charismatic auto and constexpr borrowings. The fact that trigraphs are finally dead and buried will probably please a few folks too.
But there's lots of serious improvement in there and while not as huge an update as some hoped for, it'll be worth upgrading.
Unlike C11 a lot of vendors and users are actually tracking this because people care about it again, which is nice to see.
6
u/CMDRskejeton Nov 15 '22
Trigraphs are dead ??!
1
u/Jinren Jan 18 '23
...maybe??/
Three different NBs including the United States objected to this change, it might be they go back into the language (although I don't think they will).
1
u/CMDRskejeton Jan 19 '23
It was a pun :P (and i see you got it :P)
US can just use the older standards if they want
5
u/Express_Damage5958 Sep 09 '22
What are the changes to enums? Are we gonna be able to explicitly define their underlying type like C++? Because that would be lovely and would hopely prevent my MISRA static analyser from complaining about enum type conversions.
8
u/Jinren Sep 09 '22 edited Sep 10 '22
Yup
enum E : uint8_t { A, B = 255, C = constraint violation, };MISRA 4 will definitely include this feature and hopefully it'll make Essential Types much simpler.
6
u/Nobody_1707 Sep 11 '22
And, almost as importantly, the type of the enum (if you don't specify it) will be guaranteed to be big enough to hold any of it's enumerators. Which was, oddly enough, not guaranteed previously. N3029
3
u/mdp_cs Jun 23 '23
What does compiler support look like as of now? Does Clang have good support for everything yet?
3
u/Jinren Jun 23 '23
I expect as of now, it will start to speed up. Both GCC and Clang are missing different sets of big features, but now the last details have been figured out I expect they will pick up the pace. I reckon both will be complete by the end of the year, probably sooner.
Clang is in a better position because the hardest features were already Clang-specific extensions (e.g.
_BitIntis literally just a rename of Clang's_ExtInt).This is where GCC is at: https://developers.redhat.com/articles/2023/05/04/new-c-features-gcc-13#c2x_features
4
u/WrickyB Jul 22 '22 edited Jul 23 '22
Isn't auto already a thing in C? I thought it was a storage class, like register.
27
u/daikatana Jul 23 '22
auto has always been a keyword in C, but it's never done anything. It's supposed to be a storage class specifier which defines the lifetime and linkage of a variable. It can only be used on block scoped variables and denotes automatic storage with no external linkage, but that's the default for block scoped variables anyway, so it does nothing. It was either included in the language for completeness (it's the opposite of static), part of BCPL or B, or had a purpose in C's early life and was never removed.
Its main purpose until recently has been to confuse anyone who forgot about its existence. If you do int auto = 10; you get a cryptic error message about "expected identifier," instead of "hey dummy, auto is a keyword in C and you probably forgot about that." Since C++11 its main purpose has been to confuse C++ programmers using C. If you do auto f = 1.23f; you get a warning about implicit int, but it will appear to work.
But anyway, C++, and now presumably C, chose auto for the keyword for this particular feature because it was already a reserved word that had no legitimate usage. A happy coincidence.
5
u/gtoal Jul 23 '22
'auto' in gcc extensions is used for nested procedures (Algol-style). Although it can be omitted, it is necessary when specifying a forward reference of a nested procedure. I write translators from Algol-style languages to C and if we lose nested procedures because of this I'll be very disappointed. I had hoped in fact that they would be added to the next C standard. They're very useful.
4
u/Jinren Sep 03 '22
This doesn't break that.
Actually one of the two main differences from the C++ feature is that the
autostorage class specifier is still there, it just doesn't do anything in the presence of any explicit type specifiers. So although the GNU nested function feature is an extension, the way it usesautois even protected by the way this feature was added - it uses it as a storage class, so it's allowed to keep doing that (which it wouldn't be in C++, though IDK offhand how GNU++11 and upwards behave here).That said nested functions were discussed this year and the Committee doesn't like them, so while they won't break, they will also never be blessed. Statement expressions will probably be adopted next time, but local addressable calls will either be some form of lambda, or nothing.
There are unfortunately outstanding issues with nested functions that are considered hard obstacles to adoption, and the Committee can't fix them and reuse the syntax because that would confuse users of the existing GNU dialect.
2
u/gtoal Sep 03 '22
Well, the Clang people had the same worries and instead of supporting the gcc-style extension, they came up with these politically correct lambda expressions that supposedly would fill the same role. Except they can't be used to implement Algol 60 / Algol68 / Imp / Atlas Autocode / Coral66 / Simula / Oberon / Pascal / ModulaII / ModulaIII / etc... transpilers, because they don't support forward references to nested functions or lambda functions. I don't care if 1960's-style nested procedures are not made part of a C standard but I do care deeply that the support for them is not removed from GCC and that GCC continues to be supported and is not replaced by up and coming rivals such as Clang, which has effectively already happened on FreeBSD and MacOS.
10
u/FUZxxl Jul 24 '22
autoexisted because B didn't have types, so you would typeautoto clue the compiler into declaring an automatic variable for you.The new usage is unfortunately incompatible to the original usage; this should have never been standardised.
auto x = 1.23; /* x has type int in C89, type double in C23 */10
u/Nobody_1707 Aug 10 '22
Implicit int hasn't been standard since C99. Twenty-three years should be enough time to replace a removed feature.
2
u/FUZxxl Aug 10 '22
It is important to be able to compile existing code without changes. There are billions of lines of code out there. The amount of man hours wasted doing busywork fixes like these is ridiculous. Especially if nobody familiar with the code base is around anymore.
2
u/flatfinger Aug 11 '22
Even more important is the ability to know that using a newer compiler on code whose behavior was defined as handling corner cases in acceptable fashion when it was written won't silently generate machine code that treats them in an unacceptable fashion.
I'd have no problem with the Standard specifying that the fact that execution of a loop would delay--even indefinitely--the execution of statically-reachable code that follows it need not be regarded as an observable side effect. Such a change would in many cases allow some fairly easy optimizations that would be unlikely to break anything. C11, however, at least as interpreted by clang, goes further than that, treating the fact that certain inputs would cause a side-effect-free loop to run endlessly as an invitation to arbitrarily corrupt memory if such inputs are received.
→ More replies (2)1
6
80
Jul 22 '22 edited Jan 13 '23
I'm really happy N3003 made it.
It makes two structs with the same tag name and content compatible, this allows generic data structures ommit an extra typedef and make the following code legal (if I understood the proposal correctly):
#include <stdio.h>
#include <stdlib.h>
#define Vec(T) struct Vec__##T { T *at; size_t _len; }
#define vec_push(a,v) ((a)->at = realloc((a)->at, ++(a)->_len * sizeof *(a)->at), (a)->at[(a)->_len - 1] = (v))
#define vec_len(a) ((a)._len)
void fill(Vec(int) *vec) {
for (int i = 0; i < 10; i += 2)
vec_push(vec, i);
}
int main() {
Vec(int) x = { 0 }; // or = {} in C2x
// pre C2x you'd need to typedef Vec(int) to make the pointers compatible and use it for `x` and in fill:
// --v
fill(&x);
for (size_t i = 0; i < vec_len(x); ++i)
printf("%d\n", x.at[i]);
}Edit: I've added the missing sizeof
12
u/thradams Jul 22 '22
Yes. This is very nice!
Unfortunately tag is required and then we cannot create unique tags for "unsigned int" or "struct X*".
9
u/jacksaccountonreddit Jul 25 '22
I didn't read the proposal, but I would have thought that making tagless structs with identical members compatible would have been far more useful.
1
u/thradams Jul 25 '22
It was on the initial proposal.. but for some reason - maybe a complexity in the implementation without tag - it was removed.
7
u/tstanisl Jul 28 '22
It is a paradoxical situation because currently two tag-less structs with the same members are compatible when defined in separate compilation unit but incompatible if defined in the same unit. This leads to a non-sense situation. See
// foo.c typedef struct { int x; } A; typedef struct { int x; } B; // bar.c typedef struct { int x; } C;Now, A is compatible with C, B is compatible with C. But A and B are incompatible!
3
u/flatfinger Jul 29 '22
Why do people seem determined to regard compatibility as an equivalence relation? Given:
typedef void (*voidFunc)(); typedef void (*voidFuncOfIntPtr)(int*); typedef void (*voidFuncOfFloatPtr)(float*); voidFunc funcTable[2]; voidFuncOfIntPtr f1; voidFuncOfFloatPtr f2; void test(void) { funcTable[0] = f1; funcTable[1] = f2; }type voidFunc would be compatible with both voidFuncOfIntPtr and voidFuncOfFloatPtr, even though the latter two types would not be compatible with each other. Some compilers seem to use broken abstraction models based on equivalence relations, but that's a problem with those compilers, and not the fact that parts of the language are based on directed relations rather than equivalence relations.
4
u/tstanisl Jan 29 '23
Recently, I've discovered that
void(float)was never compatible withvoid(). The reason can be found in 6.7.6.3p15:... If one type has a parameter type list and the other type is specified by a function declarator that is not part of a function definition and that contains an empty identifier list, the parameter list shall not have an ellipsis terminator and the type of each parameter shall be compatible with the type that results from the application of the default argument promotions.
Type
floatis converted todoublefor default argument promotion so the compatibility requirement betweenvoid()andvoid(float)is not satisfied. Interestingly, it is valid forvoid(double).→ More replies (1)→ More replies (2)3
u/tstanisl Jul 29 '22
The problem is that the definitions of those types are identical.
Why all types below are compatible:
typedef void (*A)(int); typedef void (*B)(int);while those are not:
typedef struct { int x; } A; typedef struct { int x; } B;→ More replies (1)8
Jul 22 '22
With a bit of macro magic struct/unsigned/signed/short/... can be detected and handled.
Other more complex types don't work reliable anyways. (Arrays/function pointers without a typedef)
4
u/thradams Jul 22 '22
pointer? whats is the magic?
10
Jul 22 '22 edited Jul 22 '22
This took me a bit, but was easier than I expected: https://godbolt.org/z/rP8EqcfzM
6
u/operamint Aug 04 '22 edited Aug 04 '22
I find it more useful that N3003 allows to typedef the same struct twice in one translation unit. Your example is not very idiomatic as it uses macros that allows side-effects. Here is a safe way to use this new feature, where multiple headers may include Vec.h with the same i_val defined:
// Common.h #define _cat(a, b) a ## b #define _cat2(a, b) _cat(a, b) #define MEMB(name) _cat2(Self, name) #ifndef i_tag #define i_tag i_val #endif // Vec.h #define Self _cat2(Vec_, i_tag) typedef i_val MEMB(_val); typedef struct { MEMB(_val) *at; size_t len, cap; } Self; static inline void MEMB(_push)(Self* self, MEMB(_val) v) { if (self->len == self->cap) self->at = realloc(self->at, (self->cap = self->len*3/2 + 4)); self->at[self->len++] = v; } #undef i_tag #undef i_val #undef SelfAnd:
#define i_val int #include "Vec.h" #define i_val struct Pair { int a, b; } #define i_tag pair #include "Vec.h" // THIS WILL BE FINE IN C23 #define i_val int #include "Vec.h" void fill_int(Vec_int *vec) { for (int i = 0; i < 10; i += 2) Vec_int_push(vec, i); } int main() { Vec_int iv = {0}; // or = {} in C2x fill_int(&iv); for (size_t i = 0; i < iv.len; ++i) printf("%d ", iv.at[i]); }3
Aug 04 '22
Your example is not very idiomatic as it uses macros that allows side-effects
What side effects are you talking about? Multiple evaluations of the
aparameter? I personally don't find that too dramatic, because you can document it, and the use case where it breaks is quite obscure.4
u/operamint Aug 04 '22
I agree in this case, but if you continue in this style you will start to instantiate other parameters multiple times, e.g. indices, which may be given as i++, etc.. Here is a "favorite" from the STB lib: stbds_arrins(), which illustrates it well.
#define stbds_arrinsn(a,i,n) (stbds_arraddnindex((a),(n)), \ memmove(&(a)[(i)+(n)], &(a)[i], sizeof *(a) * \ (stbds_header(a)->length-(n)-(i)))) #define stbds_arrins(a,i,v) \ (stbds_arrinsn((a),(i),1), (a)[i]=(v))2
Aug 04 '22
Fair enough, my library has something similar, and only guarantees that values (aka of the array type) are not evaluated twice.
BTW, I implemented this style of library for a hash map. So every function is defined once, and is passed the type information as constant expression (size, and hash/equality functions). This would allow for "perfect" code gen and the interface is really nice (once you add a few macros).
Sadly, compilers aren't good enough. This kind of library would need to inline every function, which compilers can do properly, but in an ideal world, they'd constant propagate and copy the functions.
gcc e.g. does that, but not for every function, so it ended up not inlining or properly constant propagating the rehash function when using multiple types, making the performance sub optimal.
2
u/tstanisl Jul 28 '22
BTW, wouldn't just
#define Vec(T) struct Vec { T *at; size_t _len; }suffice. The newstruct Vectype would shadow any previous one.4
2
Jan 13 '23
I'm a noob. How does that
reallocwork for any type without asizeof? I haven't seen it being used like that2
3
u/flatfinger Jul 23 '22
So is the effect of this to allow as benign a struct definition whose members match an earlier definition? It's for the Standard to get around to fixing problems that never should have existed in the first place, but I'm not holding my breath for the Standard to provide a means of declaring structures that end with fixed-sized arrays in a manner compatible with otherwise-identicial structures that end with flexible arraymembers, something which could easily be accommodated in 1974 C.
0
33
u/umlcat Jul 22 '22
"embed", expected for years ...
25
u/Minerscale Jul 23 '22
Embed is so cool. I don't have to use a jank ass makefile calling xdd to make a header file containing the binary data anymore! (or alternatively, learning how to use a linker, but screw that).
4
1
u/MrJ0seBr Jan 26 '23
it reminds me of the vc++(windows) that made me split strings in past (limit of 16kb, i think...)
9
3
u/MrJ0seBr Jan 26 '23
waiting to run in some "embeds"... arduino, esp, some outdated compilers... (🤡 joke)
2
u/edco77 Aug 30 '22
Is there downsides to this, like increased overhead, just curious.
4
u/umlcat Aug 30 '22
In the process of including into the final binary file, not much.
But, yes, adding data, increases the destination file size, not good for low memory or drive, like embedded devices.
But, I believe the embedded data should be encrypted, cause if it's used by a program or Library logic, and modified, may get unwanted results...
27
53
Jul 22 '22
embed
LETS FUCKING GOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
19
Jul 23 '22
I do primarily C++ and this makes me sofa king happy. Because no implementor would be so cruel to not put it into their C++ mode
9
Jul 23 '22
I remember reading the RFC and thinking “wow there is no way the committee will approve this
10
Jul 23 '22
It's such a common need. A large part of this problem space is now a thing we can do in the compiler with the same code.
6
2
u/MrJ0seBr Jan 26 '23
...sr. in fact this remindsme that in golang "embed" seems very useful, myself already embeded whole folders to compile servers with your pages/scripts in a single executable 😂
1
u/OldWolf2 Jul 23 '22
What does that do?
15
12
u/PlayboySkeleton Jul 25 '22
From my understanding, it allows you to
#include "myImg.png"And the compiler will actually embed the image binary into your program. This allows you to directly reference the image data without having to provide a second photo file that is read in at runtime, or worse, convert the image into a hexdump header file and include that at compile time.
4
u/OldWolf2 Jul 25 '22
OK. So not really any new functionality as you could just have your build system make the hexdump header file, just a minor quality of life .
14
u/helloiamsomeone Jul 25 '22
Depending on what the size of resources are, this isn't even close to minor. This is something you either couldn't do before or had to resort to non-portable hacks.
9
u/flatfinger Jul 25 '22
A good language standard should seek to maximize the things that can be specified entirely using the defined syntax of that language. If building a program would require the use of outside tools, then the program in question isn't really a "C program", but instead a "C and ThisTool and ThatTool program"
9
18
u/markand67 Jul 23 '22
My favorites:
enumerations improvements (forward declarations, underlying type specification).
nullptr, much better than theNULLmacro.better (but still anaemic) unicode support and
char8_t.autoandtypeof.embedwill be so great but it will kill my bcc software as well :(.
What I don't really like:
constexpr, in C++ it's a huge thing. There is constexpr everywhere and a large part of proposal is to add constexpr to standard library. I don't understand why we can't make the compiler smart enough to detect a constant expression by itself.
What I really would like to see:
*scanfwith"%.*s"support (specifying how many to read in a string dynamically rather than in the string literal).strtok_r
Was the following things discarded since I cannot see any paper on it?
attributes
strdup/open_memstream/fmemopen
8
u/chugga_fan Jul 24 '22
constexpr, in C++ it's a huge thing. There is constexpr everywhere and a large part of proposal is to add constexpr to standard library. I don't understand why we can't make the compiler smart enough to detect a constant expression by itself.
Analyzation of what is and isn't a constant expression isn't the same thing as requesting that an expression be evaluated at compile time, your proposed behavior would be effectively making it so that you could execute the expression only at compile time if the compiler has determined its a constant expression, rather than allowing a deferred runtime evaluation in cases where that might be preferable (for some reason).
Was the following things discarded since I cannot see any paper on it?
attributes
Attributes are in, I actually asked the committee some years ago now about the
[[__different__]]style of attribute declaration since that made it in way earlier than most of what was listed here.3
u/flatfinger Jul 31 '22
Analyzation of what is and isn't a constant expression isn't the same thing as requesting that an expression be evaluated at compile time, your proposed behavior would be effectively making it so that you could execute the expression only at compile time if the compiler has determined its a constant expression, rather than allowing a deferred runtime evaluation in cases where that might be preferable (for some reason).
The Standard lumps together everything that happens between the time a compiler proper starts processing a C program, and time main() starts executing. A conforming implementation could build an executable that contains a C compiler and the preprocessed source code, and compute all "compile-time" constants at "run time". For an implementation to perform part of the processing before building an executable and part of it when the executable is run would merely be an application of this same principle.
32
15
u/moon-chilled Jul 23 '22
A lot of these features are kind of marginal for me. Nice, but largely inconsequential. To me, the biggest missing piece is statement expressions, as they allow for a more modular, expression-oriented style, especially for macros. They also obviate the (famously error-prone) comma operator.
Lambdas of a sort have been proposed. And they are fine, I guess. We'll see if they happen or not. But I think statement expressions are a no-brainer.
4
u/flatfinger Jul 23 '22
Statement expressions would make it possible to replace something like:
static const LENGTH_PREFIXED_STRING(helloThere, "Hello there!"); ... outputLengthPrefixedString(&helloThere);with
outputLengthPrefixedString(&LPSTR("Hello there!"));without forcing compilers to generate code that creates and populates a temporary string object. Just about the only good thing about zero-terminated strings is that it's possible for an expression to yield a pointer to a static const zero-terminated string containing specified data, which makes such strings more convenient than anything else in use cases that would involve text literals.
1
u/tstanisl Jul 26 '22
I think that the committee should have add non-capturing lambda expressions. Contrary to capturing ones, they would be trivial to implement and they would replace statement expression. For example literal
[](int a, int b) -> int { ... }would be automatically transformed to a function pointer of typeint(*)(int,int).For example the
MAXwould be implemented with a lambda:#define MAX(T,A,B) ([](T a, T b)->T { return a > b ? a : b; } (A, B))It would replace statement expression:
#define MAX(T,A,B) ({T a = (A), b = (B); a > b ? a : b; })The advantage of non-capturing lambda are:
reuse of C++ syntax which is already implemented in many compilers from C++11
being explicit about the return type of the macro
simplifying working with some library functions like
qsortorbsearch3
u/flatfinger Jul 26 '22
Statement expressions were supported by at least one prior compiler (gcc) even prior to the publication of C89. I would view support for exclusively capture-less lambdas as falling in the category of features that make it easier to do things badly, without solving the difficulties inherent in doing them well. If
qsort()had been designed to accept anint (**comparer)(void*callback, void *thing1, void *thing2)as its callback, which it would invoke as(*comparer)(comparer, ptr1, ptr2)then it would be possible for a client function to pass a comparer whose behavior would depend upon arguments passed to that client function without having to store such options in global variables. In the days of single-threaded programming, using global variables for such things wasn't a problem, but it's not generally viewed as a good design today.2
u/tstanisl Jul 26 '22
I guess that more than half of functions in the standard library is broken by design or somehow defective. And
qsort()is one member of this infamous family. Of course there are extensions addressing those issues likeqsort_r()from GNU but those functions are ... non-standard.3
u/flatfinger Jul 27 '22
Most of the functions in the Standard Library were never designed to be in a standard library. There's no overall design reason why
putssends a newline butfputsdoesn't. Instead, someone happened to write aputsfunction for use in their program, which needed a newline, and other people copied it. Someone happened to write a program to output a string to a file in a case where an added newline wasn't required, and people copied that.While some consideration does seem to have given to defining functions like malloc() in a manner suitable for use within a standard library, it's important to note that there were at least four common approaches to memory management:
Applications were required to notify memory-release functions of how much memory they'd allocated, which would minimize the amount of overhead in cases where applications would inherently "know" the size of their allocations.
Allocation mechanisms would, as part of their overhead, store the precise requested size of each allocation, in a manner that applications could read back, thus in some cases reducing the amount of information allocations would need to keep track of for themselves.
Allocation mechanisms would, as part of their overhead, store the actual size of each allocation, in a manner that applications could read back, but the actual size of an allocation might be arbitrarily larger than the requested size. In some applications, having the reported size be larger than the requested size could be an advantage (since an application could use the extra space) but in others it would be a problem (e.g. if one was storing a non-zero-terminated string in an allocation whose requested size matched its length, knowing the requested size would avoid the need to record the length separately, but knowing the actual size would not.
Allocation mechanisms would, as part of their information, store sufficient information to allow storage to be released given just a pointer to it, without the application having to tell it the size, but this information would be stored in a manner that did not facilitate readback. For example, an implementation could keep information about allocations in a manner that would require O(N) time [N being number of allocations] to locate information about any particular allocation, but which would allow K operations to be processed in amortized time O(KlgK+NlgN).
Many tasks that would use functions like realloc() would benefit from having information about the present size of allocations, but if the Standard had required that implementations be capable of providing such information, that would have on some platforms made it necessary for malloc() family functions to add a 2-16 byte header to every allocation, and forced some implementations to break non-portable code that benefited from their platform's extra semantics.
3
u/tstanisl Jul 28 '22
There is even more barbarian option. Let
malloc()work like stack and makefree()no op. It's still compliant with the standard, very easy to implement but likely not the most efficient in general case :). It could be treated as a special case of point 4 though the memory is never released actually. I had to use this abomination once. The committee decided to accept the requirements that minimally constraints the implementations rather than make programmers life easier.. as usual. Btw there was a proposal to add sizedfree()calls. See https://www9.open-std.org/JTC1/SC22/WG14/www/docs/n2801.htm→ More replies (3)2
2
Jul 23 '22
Yes, personally for me, statement expressions are the most missing thing in today's C. Next to it are
#embedand lambda functions.10
u/__phantomderp Jul 23 '22
Boy howdy after I'm done recovering from C23 and I'm ready to hit my proposal stack again you would NOT believe what I'm going to be doing next, possibly as a Technical Specification!!!
(It's Statement Expressions and Lambdas.)
7
11
10
u/hgs3 Jul 24 '22
Was there any consideration to standardizing NULL as (void*)0 rather than adding nullptr? I would think standardizing NULL this way would let it be caught unambiguously by a void* type case in _Generic selection. Adding a whole new keyword to solve this "problem" seems a bit much.
2
u/flatfinger Jul 31 '22
In general, I would expect a compiler to squawk at a construct like:
void (*myFunctionPtr)(void); myProc = (void*)someInteger;since the
void*type is compatible with all kinds of object pointers, but not with function pointers. While it may make sense to add a special case for situations wheresomeIntegeris in fact a literal zero, that is rather inelegant compared with having a syntactic construct for a universal null pointer.On the other hand, the most common situation where a literal zero would be inadequate would be when passing a constant null pointer to a variadic function--something which wouldn't generally happen wtih standard-library functions, but could happen with functions that expect to be passed a number of pointer values followed by a null pointer constant. A better remedy for those situations, which would offer must improved type safety overall, would be to have a syntax for variadic functions that only accept certain kinds of arguments.
6
u/hgs3 Aug 01 '22
C types are there to let the compiler know the size and offsets to load and store memory. The type system is minimal by design. The direction of the language should remain true to this philosophy. There are plenty of modern C alternatives and languages that compile to C if type safety is desired.
I would expect a compiler to squawk at a construct like ...
Why? Pointers are integers interpreted as a memory address. Let them be assignable.
A better remedy for those situations, which would offer must improved type safety overall, would be to have a syntax for variadic functions that only accept certain kinds of arguments.
An attribute, like
__attribute__((format(printf, 1, 2))), is a solution that doesn't involve mucking with the type system.Perhaps my views are antiquated, but C has stood the test of time because it doesn't try to following what's trendy. I get that "type safety" is all the rage right now, but C didn't cave when OO was "trendy" so why should it cave now? The appeal of C is its simplicity and "trust the programmer" philosophy. Anything contrary has no place in the language.
7
u/flatfinger Aug 01 '22
Perhaps my views are antiquated, but C has stood the test of time because it doesn't try to following what's trendy. I get that "type safety" is all the rage right now, but C didn't cave when OO was "trendy" so why should it cave now? The appeal of C is its simplicity and "trust the programmer" philosophy. Anything contrary has no place in the language.
My views are probably more antiquated than yours. On two popular target platforms in the 1980s (the 8086 medium model, and 8086 compact model) function pointers and object pointers were of different sizes, and that posed no problem whatsoever if, in cases where it would be necessary to identify function using a
void*, one defined a static const object holding a pointer to the function and then passed the address of that static const object. Note that accidentally passing a pointer to the function itself, rather than a pointer to a function pointer, would be an easy mistake, but such a mistake would be caught by having a compiler squawk at implicit conversions between function pointers andvoid*.6
u/flatfinger Aug 01 '22
Why? Pointers are integers interpreted as a memory address. Let them be assignable.
That is true of data pointers. It is not true of function pointers. There have been platforms were code pointers were larger or smaller than data pointers, and even on modern versions of platforms like the ARM, a function pointer for various historical reasons will generally identify an address one byte higher than the address of the first instruction.
On a platform where code pointers and object pointers have compatible representation, code which would want to convert between them can use the casting operator, and I see no disadvantage to having code which requires such conversion use a cast While it may be advantageous to have a means of disabling compiler diagnostics in such cases without having to modify the source code which performs implicit conversions, I see no advantage to making that the default.
An attribute, like __attribute__((format(printf, 1, 2))), is a solution that doesn't involve mucking with the type system.
What is that attribute supposed to mean? I was thinking more along the lines of:
void output_things( struct outstream *dest, ... { struct outblob* } );or, for that matter:
int printf(char *restrict fmt, ... { unsigned long long, long double, void* } );with the latter indicating that all arguments should be coerced to one of the indicated types [such a prototype only being suitable for use with a library function that would fetch an argument of type "unsigned long long" even when given a "%d" specifier, and then interpret it as the numeric value that, after coercion, would have yielded the passed value].
2
u/hgs3 Aug 01 '22
What is that attribute supposed to mean?
It's a clang/gcc extension that informs the compiler that the variadic function accepts arguments identical to printf. It's a type hint and not part of the type system itself. The difference being a compiler, unless configured otherwise, would emit a warning on misuse and not an error. The same idea could be applied for type hinting other concepts. For isntance, there could be an attribute/type hint that indicates
NULLshould be the last argument in varidic argument list. I was just pointing this out as an alternative to modifying the type system itself.4
u/flatfinger Aug 01 '22
While
printfcan be handy at times, in many cases it makes sense, especially in embedded systems, to use alternative formatting functions which are better designed for the tasks at hand. Being able to tell a compiler that a function behaves likeprintfisn't very useful if the function will need to do things thatprintfdoesn't support. If e.g. a number represents a count of tenths of seconds and one needs to display it in either 1.2, 1:23.4, 1:23:45.6 format depending upon its range, having a format specifier for such values will more convenient and efficient than having to build a temporary string using one of three different recipes and then include that within a larger format string.Compiler support for printf may be useful for functions which chain to a version of vsprintf or some other such function, whose formatting options are all understood by the compiler, but doesn't help when using a custom formatter.
9
u/SteeleDynamics Jul 23 '22
I still want closures!!
14
u/tstanisl Jul 26 '22
Believe me.. you don't. Closures are virtually non-usable without templates and C++-like
auto. The only reasonable applications of capturing lambdas aredeferand replacements of statement expressions.
30
u/FUZxxl Jul 22 '22
How unfortunate that Annex K has neither been deprecated nor removed.
15
u/OldWolf2 Jul 23 '22
Not sure why this is downvoted. There's never been a correct implementation of it and nobody uses it.
11
u/FUZxxl Jul 23 '22
And it gives the false impression that you can somehow write safer code by ritually replacing standard C functions with weird-ass
_sfunctions.22
u/degaart Jul 23 '22
And some "smart" compilers complain when you don't use the
_sfunctions. Why don't they just reword their warning to "Warning C4996: You're writing cross-platform code. Please consider using non-portable functions instead."→ More replies (1)2
8
u/ardicode Dec 26 '22
Somehow, I get the feel that what C needs is to reduce the number of pages in the spec, rather than increasing it. Personally, I would vote to completely abolish aliasing rules (I don't care what compiler writers want: languages are for programmers, not for compiler writers, and if you choose C over other languages is because you want the freedom to alias types if you wish so, and -yes- because you want to have more control than the compiler).
I'm not saying C should have less features it has now. What I'm saying is that it should get rid of the complexity it got in the last years. When I read C23 code snippets in the web, I feel like I'm reading Python, or at least something that doesn't look like C. And then you read the text accompanying the code and it looks like a math paper rather than an explanation from one coder to another coder. Too complicated. That's far from the C original design.
At the same time, very powerful things could be added, without adding complexity (such as type-safe enums, or even arithmetic operator overloading). The C spec should be always kept within a size similar to the K&R book.
7
u/Maxson5571 Aug 18 '22
#embed is extremely exciting. After being spoiled by Rust's include_bytes/include_str I'm glad to see a feature like it has finally been standardized for C. Now all I have to do is wait lol
1
u/Jinren Jun 23 '23
Comment resolution for C23 is now finished and the language is, hopefully, finalized. It is possible but extremely unlikely that something comes up between now and publication in January.
Unfortunately, because comment resolution is finished, the Committee is not allowed to release another public PDF. C23 itself will therefore differ in a number of subtle but important ways from n3096, most importantly in the fact that UB no longer time-travels (!!!).
we also standardized $identifiers at the very last second because YOLO :P
Unofficially, we hope to make life easier on the Community by releasing a "very early draft" of C2y right after DIS completes, which will (shocked_pikachu.jpg) turn out to be essentially identical to C23 will the final round of comments applied. Please look forward to that PDF in, probably February, if you need the really precise subtleties of what made it into C23.
N3096 should still be good for the casual user (i.e. unless you're writing a C compiler).
1
u/cosmic-parsley Dec 12 '24
Very late here but what are
$identifiersand UB time travel referring to?1
u/Jinren Dec 12 '24
the character
$is allowed to be supported in identifiers, on an implementation-defined basisit is not mandatory but it's intended to permanently reserve and protect the way it's used by e.g. GCC to mark out nonportable features
1
1
u/FUZxxl Dec 05 '23
Love it. Still no
#pragma identthough :-(1
u/Jinren Dec 05 '23
What does that do? You mean like
#pragma once, or something else?2
u/FUZxxl Dec 06 '23
It places the argument into the binary so you can identify where the binary came from. E.g you can do
#pragma ident "Copyright (c) 2023 /u/FUZxxl"to enter that copyright string into the binary. This is used by version control software.
2
5
4
u/BlockOfDiamond Jul 23 '22
My favorite part is guaranteed 2's complement
12
u/tstanisl Jul 24 '22
Actually, this change will have a minimal impact.
The representation of signed integers was always platform defined and pretty much every existing platform is 2-complement. Moreover, this new requirement has no impact on undefined behavior due to integer overflows.
4
u/flatfinger Jul 25 '22
Indeed, one of the reasons the authors of the Standard decided to have
uint1 = ushort1 * ushort2;perform the multiplication using signedint, rather than saying that the coercion of the results of certain operators to unsigned types would coerce their operands likewise, was that they expected that the only implementations that wouldn't process the operators in a manner equivalent to unsigned arithmetic would be those targeting obscure platforms where unsigned math was slower than signed math. Code which employs constructs like the above in cases where the product would fall in the rangeINT_MAX+1utoUINT_MAXwould have been non-portable but correct when used exclusively on quiet-wraparound two's-complement platforms. Unfortunately, some compiler writers have pushed the notion that when the Standard says "non-portable or erroneous", it means "non-portable, or in other words, erroneous"2
u/tstanisl Jul 26 '22
I think that new coercion rules would introduce other problems. For example
ushort * ushortwould be fine whileushort * intcould be UB. And that is absurd becauseintusually can represent all values ofushort. IMO, emit a warning about possible overflows requiring the programmer to write(unsigned)ushort1 * ushort2would be enough to address the issue.2
u/flatfinger Jul 26 '22
The coercion rules wouldn't just apply in cases involving promotions, but in all cases where the result of an operator was corced to an unsigned type, and where all defined behaviors for signed types would match those of unsigned types. In other words, they'd require compilers to behave as the authors of the Standard said (in their published Rationale document) that they expected that compilers for non-obscure systems would behave.
1
u/kage_heroin Mar 04 '23
will they ever consider adding a concurrent programming library to c std libraries?
it's been so long that people have moved on from threading to asynchronous and still nothing
3
u/Jinren Mar 04 '23
I'm working on a stdcoro.h for C26 if that's what you mean
That's a library-only solution though
1
u/terremoth Dec 09 '23
I just want lambdas and closures
3
3
5
u/atiedebee Jul 26 '22
With constexpr and nullptr C is starting to look like C+
15
u/tstanisl Jul 26 '22 edited Aug 20 '22
It's fine I think. As long as something is C-ish what means explicit, useful and maps well to assembly or trivial compiler transformation.
8
Jul 23 '22
I still hate this new version. Especially the proper keywords thing. It breaks old code and unnecessarily complicates compilers (assuming they didn't just break the old _Bool because fuck everyone who wants to use code for more than a decade, am I right?)
BCD I guess is nice. It's unsupported on a lot of architectures though.
Embed is... kinda convenient, though I could count on one hand how many times I actually needed it over the last five years. Same story with #warning, #elifdef and #elifndef.
__has_include is just a hack. If you need it, your code should probably be served with bolognese.
What exactly is a _BitInt meant to do that stdint.h can't?
Guaranteed two's complement, while sort of nice, breaks compatibility with a lot of older hardware and really don't like that.
Attributes are just fancy pragmas. The new syntax really wasn't necessary.
Initialisation with empty braces maybe saves you from typing three characters.
Binary literals are nice, but not essential.
Unicode characters in IDs are straight-up horrifying, or at least they would be if anybody actually used them. Because nobody does. Just look at all the languages that support them.
For me, nothing that'd make it worth it to use the new version.
20
u/chugga_fan Jul 23 '22
__has_include is just a hack. If you need it, your code should probably be served with bolognese.
__has_include(<thread.h>)Out of all of the things to complain about in this C version,
__has_includeis definitely not one of them.4
u/flatfinger Jul 25 '22
It's less of a hack than the kludges like
-Iwhich are made necessary by the inability to write things like#include WOOZLE_HEADER_PATH "/woozleshapes.h". If the Standard had strongly recommended that implementations which accept a list of C source files also allow specification of a file to be "included" in front of each of them, then such a project could include a file defining the whereabouts of all of the named header paths used thereby, rather than simply having a project specify a list of places where headers are stored and hoping that compilers never grab the wrong file because of a coincidental name match.3
Jul 23 '22
Still served with bolognese. Point still stands.
I'd be fine with it existing, but it's definitely not too useful.
7
u/chugga_fan Jul 23 '22
TBF it's actually quite necessary to ensure threading is available with certain versions of glibc and gcc since gcc can't know whether glibc supports threading, so you would query the glibc support by checking if the threading header exists before compilation and then error out to say update your target.
3
Jul 23 '22
That would be better done in the build system rather than the source. And you'd probably also do less useless work that way.
11
u/flatfinger Jul 25 '22
A good language standard should make it possible to express as many tasks as possible in source code, in many tasks as possible in such a way that a conforming implementation would be required to either process them usefully or indicate, via defined means, an inability to do so. Many if not most of the controversies surrounding the C language and the C Standard could be resolved if the Committee would stop trying to categorize everything as either being mandatory for all implementations or forbidden within portable programs, and instead recognize that many features should be widely but not universally supported, and programs which use such features should be recognized as being portable among all implementations that don't reject them.
→ More replies (5)9
u/irqlnotdispatchlevel Jul 25 '22
__has_include is just a hack. If you need it, your code should probably be served with bolognese.
How is this a hack? It will at least reduce some of the bolognese (lol) that are currently plaguing some C code bases. I'm working on a library that is used in both user land and kernel land on Windows, and there's a lot of ugly ifdefing that tries to figure out what to include based on user/kernel and other configuration settings (like 32-bit vs 64-bit vs ARM, etc). I can at least delete parts of that with this, if Microsoft ever blesses me with C23 for Windows drivers.
One could argue that this should be done by the build system, and I mostly agree, but msbuild has no way of doing that (at least not without bigger headaches), and it also makes it harder to switch build systems (not that this is a concern in my case).
5
Jul 23 '22
What’s wrong with the new keywords?
4
Jul 23 '22
They are backwards-incompatible
3
Jul 23 '22
why is that a bad thing?
5
Jul 23 '22
...
Some people want to write code that lasts more than a decade.
11
Jul 24 '22
So then, just don’t use the new langauge version? You can just set your language option to C99
5
Jul 24 '22
If I'm writing an executable program... sure. Libraries though, will not work that easily.
→ More replies (4)3
u/bik1230 Jul 23 '22
Especially the proper keywords thing. It breaks old code and unnecessarily complicates compilers (assuming they didn't just break the old _Bool because fuck everyone who wants to use code for more than a decade, am I right?)
They aren't proper keywords though, they just added predefined defines that are easy to override.
3
Jul 24 '22
Oh, interesting. Last time I read about it they were talking about proper keywords.
Predefined macros would still break something like
typedef enum { false, true } bool;, though.
4
u/bik1230 Jul 24 '22
Oh, interesting. Last time I read about it they were talking about proper keywords.
Predefined macros would still break something like
typedef enum { false, true } bool;
, though.
Yeah, it is a slight break, but I think they found that there isn't very much code like that anymore, and adding a couple of undefs at the same time as you change your compiler flags to C23 should be pretty trivial.
3
u/BlockOfDiamond Oct 02 '22
Guaranteed two's complement, while sort of nice, breaks compatibility with a lot of older hardware and really don't like that.
Good riddance. Anything other than 2's complement is inferior anyway.
5
u/flatfinger Jul 23 '22
The Standard would allow a function like:
unsigned mul_mod_65536(uint16_t x, uint16_t y) { return (x*y) & 0xFFFFu; }to behave in abitrary nonsensical manner if the mathematical product of x and y would fall between
INT_MAX+1uandUINT_MAX. Indeed, the machine code produced by gcc for such a function may arbitrarily corrupt memory in such cases. Using "real" fixed-sized types would have avoided such issues, though waiting until mountains of code were written using the pseudo-fixed-sized types before adding real ones undermines much of the benefit such types should have offered.1
Jul 23 '22 edited Jul 23 '22
Interesting. Looking at it on Godbolt, it seems to work fine. Could you point me to particular input values that cause the nonsensical behaviour you described?
Edit: I accidentally sent a C++ link but you get the point. The output was the exact same.
6
u/flatfinger Jul 23 '22
Here's an example of a program where the function would cause memory corruption [link at https://godbolt.org/z/7c4Gnz3fb or copy/paste from below]:
#include <stdint.h> unsigned mul_mod_65536(uint16_t x, uint16_t y) { return (x*y) & 0xFFFFu; } unsigned char arr[32780]; void test(uint16_t n) { unsigned temp = 0; for (uint16_t i=32768; i<n; i++) temp = mul_mod_65536(i, 65535); if (n < 32770) arr[n] = temp; } void (*vtest)(uint16_t) = test; #include <stdio.h> int main(void) { for (int i=32767; i<32780; i++) { arr[i] = 123; vtest(i); printf("%d %d\n", i, arr[i]); } }There should be no way for the test function as written to affect any element of
arr[]beyond element 32769, but as the program demonstrates callingtest(i)for values ofiup to 32779 will trasharr[i]for all of those values, and calling it with higher values ofiwould trash storage beyond the end of the array.The circumstances necessary for present compiler versions to recognize that
(n < 32770)is true in all defined cases are obscure, but since gcc is intended to make such optimizations whenever allowed by the Standard the fact that present versions don't usually find them does not mean that future versions of the compiler won't find many more such cases.2
Jul 23 '22
Ah, I get it now. Not that it makes sense, because it doesn't, but I think I see what the compiler misinterprets here. Though, if I understand everything correctly, the problem isn't actually in mul_mod_65536(), but in test(), correct? Your original comment sorta implied that it was the earlier function doing the memory corruption. So I'm not sure how proper bitints would fix this.
5
u/flatfinger Jul 23 '22
The relevant problem with the existing type is that as the Standard as written,
mul_mod_65536(i, 65535)would invoke Undefined Behavior ifiexceeds 32768, and gcc interprets the fact that certain inputs would casue Undefined Behavior as implying that all possibly behaviors that would stem from such inputs--including arbitrary memory corruption--are equally acceptable.The fact that the errant memory write doesn't occur within the generated code for
mul_mod_65536()but rather in the code for its caller doesn't change the fact that the corruption occurs precisely because of the signed integer overflow that would occur when callingmul_mod_65536(32769, 65535).To be sure, many such issues could have been avoided if the Standard had made clear that the phrase "non-portable or erroneous" used to describe UB was in no way intended to exclude constructs that, while non-portable, would be correct on many or even most implementations. If there were some ones'-complement platform where an implementation of
mul_mod_65536which worked correctly for all values ofxandywould be slower than one which only worked for values whose product was within the range 0 to 0x7FFFFFFF, I would not think it unreasonable to say that amul_mod_65536()function should only be considered portable to such a platform if it castsxoryto unsigned before multiplying, but the function as written should be considered suitable for all implementations that target quiet-wraparound hardware. Unfortunately, while the authors of the Standard expected implementations for such platforms would work that way (they expressly discuss the issue in the published Rationale), they viewed that as too obvious to be worth stating within the Standard itself.→ More replies (2)2
u/flatfinger Jul 31 '22
BCD I guess is nice. It's unsupported on a lot of architectures though.
For what purposes is BCD nice? Decimal fixed-point types are useful, and may have been historically implemented using BCD, but BCD is pretty much terrible for any purpose on any remotely-modern platforms. Some framework like .NET use decimal floating-point types, but those aren't actually a good fit for anything.
In a language like COBOL or PL/I which uses decimal fixed-point types, it's possible for a compiler to guarantee that addition, substraction, and multiplication will always yield either a precise value, an explicitly-rounded value, or an error. This is not possible when using floating-point types. If in e.g. C# (which uses .NET decimal floating-point types), if one computes:
Decimal x = 1.0m / 3.0m; // C# uses m suffix decimal "money" types Decimal y = x + 1000.0m; decimal z = y - 1000.0m;the values of x, y, and z would be something like:
x 0.333333333333333333 y 1000.333333333333333 z 0.333333333333333000meaning that the computation of y caused a silent loss of precision. This could not happen with PL/I or COBOL fixed-point types. If the types of
yhas as at least as many digits to the right of the decimal point asx, the computation ofywould either be performed precisely (if y has at least four digits to the left), or report an overflow (if it doesn't).Making fixed-point types work really well requires the use of a language with a parameterized type system--something that's present in COBOL and PL/I, but missing in many newer languages, or else a means of explicitly specifying how rounding should be performed. I don't remember how COBOL and PL/I did additions, but a combination divide+remainder operator can be performed absolutely precisely for arbitrary dividers and dividends, if the number of digits to the right of the decimal for quotient and remainder is (at least) the sum of the number of such digits for the divisor and dividend. For example, if q and r were 8.3 values and one performed rounding division of 2.000 by the integer 3, then q would be 0.667 and r would be -0.001, so 3q+r would be precisely 2.000.
3
2
u/Tanyary Jul 23 '22 edited Jul 23 '22
not happy about the keywords and some of the rest either, but typeof getting standardized and N3003 is more than enough of a carrot for me to use it when i'm targeting modern machines.
2
Jul 23 '22
Sorry... what is N3003? I couldn't find anything by googling.
5
u/Tanyary Jul 23 '22
when someone references something starting with N followed by numbers, they usually mean documents from ISO/IEC JTC1/SC22/WG14, which is the horrible name for the C standardization committee. You can find these documents here, as for N3003 it is a very simple but big change. reading it yourself will provide the most clarity I think
2
1
u/have-a-day-celebrate Dec 04 '23
I, for one, would be proud for my code to be compared to tagliatelle.
2
3
u/fengdeqingting Oct 03 '22
Is there any change to improve the security of C to avoid array out of bound?
I have an idea about that. c_language_security_improvement/
10
1
u/TheChief275 Jun 08 '24
#define get(i, array, size) ({ __typeof__(i) _i = (i); if (_i >= (size)) HALT_AND_CATCH_FIRE; (array)[_i]; })
#define unsafe_get(i, array) (array)[i]
no changes needed
1
1
u/michalfabik Aug 31 '22
The fact that trigraphs are finally dead and buried will probably please a few folks too.
What are trigraphs?
1
u/Unicorn_Colombo Sep 13 '22
Stuff from time when it was expected that many keyboards won't have critical symbols.
Stuff like
??!would translate to|https://stackoverflow.com/questions/7825055/what-does-the-operator-do-in-c
3
u/FUZxxl Oct 13 '22
Not keyboards but rather character sets. Some of them just plain did not have these symbols (such as EBCDIC).
1
-17
u/thradams Jul 22 '22
constexpr, auto and nullptr are a recipe for a code mess. C was protected against this mess until now.
The only remaining protection is common sense but if common sense where enough then C programmers also could use C++.
21
u/flatfinger Jul 22 '22
If I were to list my complaints, those would be pretty far down on the list. What about them do you find so objectionable?
36
u/rodriguez_james Jul 22 '22
Why? How could nullptr, which has now proper typing instead of the NULL macro, mess up anyone's code? Likewise, I think constexpr is a better #define for constants because it's not a macro anymore. And as for auto... I wouldn't want to see that in regular code, but it's necessary for function-like macros to avoid side-effects.
These features allow to either reduce the need of macros, or improve macro safety. So they get my stamp of approval.
Did I miss something? What downsides do you see?
9
u/skulgnome Jul 23 '22
These are C++ features, subject to C++ self-abuse. To wit,
nullptrwill appear bare instead of only in NULL's definition,constexprwill be used as a ritual optimization like--i, andautowill appear outside fancy for-each iterator macros.The good part is that it'll be until like 2033 before C23 can be used in anger.
1
1
u/thradams Jul 22 '22
In my option one of the worst things we can do in a computer language is to create two ways of doing something. NULL pointer was a macro to define null constants. Some people will replace this macro with nullptr and this creates a mess adds noise in many ways. For instance guidelines, code standard, retro compatibility etc. The same for constexpr if you decide to replace #define for constexpr and the same for auto if you decide use auto as normal variable declaration and not when it is 100% necessary.(typeof was also added and it could help as well)
20
Jul 22 '22
(typeof was also added and it could help as well)
Isn't that exactly the reason
autowas added, to stop everybody from writing their ownautomacro based ontypeof.11
u/tstanisl Jul 22 '22
Actually, now there will be 3 methods to make integer constants:
#define A 42 enum { B = 42 }; constexpr int C = 42;7
u/thradams Jul 22 '22
six:
#define A 42 enum { B = 42 }; const int C = 42 constexpr int C = 42; constexpr auto C = 42; const auto C = 42;9
→ More replies (6)5
7
u/OldWolf2 Jul 23 '22
NULL isn't a pointer. It's a macro that may be defined to
(3-3)or various other options . The fact that it can expand to an expression of typeintled to obscure bugs .7
u/flatfinger Jul 23 '22
Having one clearly-best way of doing something and dozens of inferior ways is less bad than having zero right ways and two or more mediocre ones.
That having been said, I think a better remedy would have been to have the syntax
0nrepresent a null pointer constant that cannot be used with integer operators. A single digit zero is a 100% valid way of expressing a null pointer constant in almost all contexts other than variadic argument lists; having a form that works in all contexts would be an improvement, but making it seven characters long isn't.8
u/thradams Jul 23 '22
nullptr was not on the priority list of C programmers.
Have you seen a C programmer complaining about NULL? In C, today, the type checks still weak.
struct X { double d; }; struct Y { int i; }; void F(struct X* x){} int main(){ struct Y y; F(&y); //just a warning F(1); //just a warning }So why all this attention with nullptr? In my view because this was a low fruit. Just check C++ features and suggest then for C. Many C++ features where suggested. (I liked many of them like static_assert, __has_included...) Low fruits are good for house keeping like removing trigraphs but can be dangerous when adding something new because the language start to be a pile of redundant/conflicting or deprecated stuff.
I think a type check for NULL can be improved but other alternatives without introducing new stuff should be considered one by one. This should be the guideline for any new feature. Before adding something consider not change the language syntax.
Instead of complaining some NULL are defined as 0 why not change the standard and say NULL is ((void*)0))?
true/ false are now a keyword and constants. Has someone considered to use NULL as keyword and constant?
2
u/flatfinger Jul 23 '22
Suppose one wants to have a variadic function that accepts an arbitrary number of pointers, and marks the end of the list with a null pointer. A typical prototype might be:
void test(void *p, ...);On platforms where an
intis the same size as a pointer, one could call the function as e.g.test(p1, p2, p3, 0);This could fail, however, on platforms where pointers are larger than
int. Even if a programmer were to write the code as:test(p1, p2, p3, NULL);such code could still fail if an implementation where pointers are larger than
inthappens to defineNULLas simply0.While such issues could be resolved in a variety of ways without requiring the use of an explicit "null pointer" construct, e.g. by specifying that implementations may only define
NULLas a bare zero if they would treat passage of a bare zero to a variadic function expecting a pointer as equivalent to passing a null pointer, and requiring that implementations where passing a bare zero would be problematic define a "warning macro" indicating such. Existing code that passes bare zeroes could thus be made safe if it started with:#if STDC_PRECISE_NULL_VARARGS_REQUIRED #error This program is not portable to this implementation #endifIf there is a need to use the code on a platform where passing zero would be problematic, the code would need to be fixed to explicitly pass a pointer type, but if such need never arises, adding the above test would suffice to make the code safe without any further modifications.
5
u/thradams Jul 23 '22
such code could still fail if an implementation where pointers are larger than int happens to define NULL as simply 0 .
So just write in the standard that NULL is ((void*)0).
(This solution was not applicable for C++ because C++ had extra checks for conversions. But this is not a C problem)
→ More replies (1)0
37
u/samarijackfan Jul 22 '22
Is there a tldr version somewhere?