Jens Gustedt's Blog

October 24, 2012

C11 defects: initialization of padding

Filed under: C11, C99, defects, language — Jens Gustedt @ 21:55

The C11 has added an attempt to force compilers to initialize padding of structures and unions under certain circumstances. Unfortunately the situation has become confusing now, since it still foresees that padding can be treated differently from other parts of structures that are not initialized explicitly.


October 14, 2012

C11 defects: C threads are not realizable with POSIX threads

Filed under: C11, defects, language, POSIX — Tags: — Jens Gustedt @ 21:19

This post is mainly identical to a defect report that hopefully will be discussed by the C standards committee on their next meeting. I found that problem that this raises needs to be better known before people start using this interface more widely, so I decided to also publish it here.

The thread interfaces as they are declared in the threads.h header are largely underspecified, such that interpreting them is often just guess-work and leaves room for a wide range of interpretations. This is particularly irritating since there already is an ISO standard about threads that is quite elaborated and mature, namely ISO/IEC 9945:2009, commonly know as POSIX 2010. C11 mentions ISO/IEC 9945:2009, but completely misses to technically relate to it on the thread interface. The semantic specification of C11 threads is in parts so loose, that a stringent implementation of C11 threads on top of POSIX doesn’t seem possible.

Other platforms that are less formalized than POSIX have their own technical restrictions that should additionally be taken into account. The “other platform” for threads that clearly had been targeted by the committee are threads on Microsoft Windows platforms. Most other widely used commodity operating systems are POSIX compatible (from mainframes down to Android phones). But we should not underestimate the potential of the C threads interface. Because it has a reduced interface it might be suitable for a larger range of platforms than we can foresee today. Because C threads don’t enforce a complete share of the address space, such platforms could e.g be accelerators (providing a portable thread interface on GPU?) or networks on chips. The only memory that must be shared by C threads are objects with static storage duration and objects allocated through malloc and friends. Thus freestanding environments without malloc would only be required to shared statically allocated objects.

In the following I only give an incomplete list of the defects as I noticed them, I suspect that there might be a lot of others.


August 17, 2012

named constants

Filed under: C11, C99, feature request, language — Jens Gustedt @ 11:48

It is now exactly two years that I wrote about the underestimated register keyword in C. Today I’d like to tell about an idea that is spinning in my head ever since, that register variables in file scope would be a perfect tool to offer a feature that is much missed in C: named constants of arbitrary data types.

The whole of it is a bit technical, be warned, but the essence of it will be to declare something like the following in a header file, without creating linkage or “instantiation” problems, and helping us to write readable and debuggable code:

register const double PI = 3.14159265358979323846;

typedef struct listElem listElem;
struct listElem { unsigned val; listElem* next; };

register const listElem singleton = { 0 };


July 15, 2012

Capture return codes from library functions

Filed under: C11, C99, library — Jens Gustedt @ 21:57

C (and POSIX) library functions often communicate error conditions in a two stage procedure: first the return value of the function is set to a special value (such as 0 for malloc) and then the caller can read the particular error condition from the pseudo-variable errno.

This “traditional” error handling has several disadvantages:

  • Code that captures all returns from library functions with conditional code quickly becomes unreadable. Optically such code emphasizes on the error handling part, instead of the normal control flow.
  • errno is a fragile thing, and in particular after it has been set by a transient error, it must be reset to 0.

P99 now lets you overload functions with appropriate macros for the error check, and that if an error occurs will either abort execution or throw an error via P99’s P99_TROW.


July 11, 2012

Drop out: there is not opt out button for dropbox

Filed under: rants — Jens Gustedt @ 06:51

Recently someone sent me a “share” request on dropbox and used one of my email addresses that is not yet linked to that service, and for which I don’t want to ever use dropbox. I then redirected that person to use another mail account for that, and the person did. Everything fine, so far.

But then days later dropbox kindly reminded me that there was this request pending. I guess that spam would have gone on for a while, if I hadn’t stopped it.

So I went to the dropbox site to opt out of their service for this particular email. It took me a while to realize that the expectation that there might exist just a little button for that was far too naive, there is no such thing on their site. I am sure that technically such a button would not be much of a problem, basically just the same as subscribing for such a service. (You’d have to verify the email, somehow.)

So I had to go through their “help” system, which took actually two entire days until I got through to a human. Finally they put that mail address on a black list.

In summary not too good an experience as a “non-user”. I already would rate the services that they offer to real customers relatively bad:

  • pro: a file sharing service, even free of charge for all the use I had for now
  • contra: ridiculous management of access rights:
    • it allow your “friends” to inadvertently delete your files when “moving” them on their laptop disk, instead of copying them
    • It allows your friends to silently accumulate GigaBytes of files on your hard disk, by collecting files in shared directories that basically are never cleaned up. (currently 3.4 GiB on my hard disk)
  • contra: no encryption or data protection

I will probably think again before I send an “invite” to share some files on dropbox, this might be a real privacy intrusion.

May 19, 2012

type generic functions taking pointers or arrays

Filed under: C11, language, P99 — Jens Gustedt @ 14:26

Every C programmer knows about the implicit conversion an array object A undergoes in most contexts: it is converted to a pointer to its first element, as if the programmer had written `&A[0]`. The technical term of the standard for that is “lvalue conversion”, and the corresponding section describes what precisely is going on when an object (an lvalue) is evaluated for its contents (the rvalue).

One particular place where this conversion takes place is when an array is passed as an argument to a function. So in that case all additional information about the array, in particular its length, is not made available to the function. One could always pass the size of the array (sizeof A) as additional argument to the function, but this then would always require to pass this information also in case the function is called with what its interface suggest, just a pointer. In this post I will describe a new possibility that is provided by the _Generic keyword in C11 that allows us to create type generic interfaces that knows how to distinguish between the case being called with an array or with a pointer as an argument.

April 20, 2012

struct tags in C++ are even weirder

Filed under: C++, C11, C99 — Jens Gustedt @ 22:14

I already discussed that fact that struct tags are not identifiers in C++, and in particular that a tag name can be used as the name of a types. Today I learned that the rules for that are even more complicated than I thought. In C an identifier that has been used as a tag (for struct, union or enum) can freely be used as another identifier (variable, typedef, label). In C++ only almost: there is one sort of identifiers it can’t be used for, typedef unless it refers to the same type as the tag.


April 17, 2012

C11 defects: underspecification of tss_t

Filed under: C11, defects, language — Jens Gustedt @ 15:32

Section 7.26.6 “Thread-specific storage functions” of C11 is severely underspecified since it uses terms that are not introduced (so far) in the context of C. This is really a pity, since POSIX also has pthread_key_t that is completely feature equivalent and for which the specification is much more complete.

Jacob Navia had observed that at several occasions in comp.std.c but it seems that he had not got enough attention such that this had made it in a defect report.


March 21, 2012

C11 defects: initialization of atomic_flag

Filed under: C11, defects, language — Jens Gustedt @ 16:44

With this post I will be starting some random collection of things that I consider being defects of the C11 standard. Perhaps someone of the committee could pick them up or send me a mail to discuss them and eventually formulate a formal defect report.

C11 expresses the intention to have atomic_flag as a primitive that should allow to emulate all other atomic types and operations, 7.17.8 p3 in a note says:

The remaining types can be emulated with atomic_flag, though with less than ideal properties.


March 12, 2012

C data model, simplified view

Filed under: C11, C99, language — Jens Gustedt @ 23:55

As we have seen in the earlier post, if you dig deeper into it the C data model is quite complex and has corner cases that are a bit bizarre. This might make C weirder than it need to be at a first approach.

As I said, the two most important properties of objects (or values) in C are if they are mutable (or not) and if they are addressable (or not), leaving us with 4 principal categories of objects that we have to deal with.


« Newer PostsOlder Posts »

Create a free website or blog at