Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - kyle

Pages: 1 2 3 [4]
46
Ideas / Weird ideas #2: variable sized structures
« on: June 19, 2014, 02:50:04 AM »
A common idiom in C is something like this:

typedef struct {
   int foo_count;
   struct foo[0];
} foo_list;

That foo[0] part (GCC syntax, MSVC is foo[]) means "don't allocate any space for this, but let me access the array of elements later".  It is heavily used for binary protocols and things where you want array access semantics to the foo elements, but you do not know in advance how many there might be and you are passed a blob of data.

I find the 0 or empty brackets ([]) to be something of a hack.  I would prefer a small syntactic change to use foo[...] or something like that.    At least to my eye, this means more clearly "we do not know how many there will be at this time".

Note that this is different from a dynamically sized array.  For that, you need a hidden count of the actual array size.  In fact, the implementation of a dynamic array might very well look like the example above.  I am not sure if using the same syntax for both would be a good idea.

Best,
Kyle

47
Ideas / Weird ideas #1: primitive data type sizes
« on: June 19, 2014, 02:41:27 AM »
Having ported code back and forth between platforms in C, one of the things that is very frustrating is the very vague definitions of "int", "short", "char", "long", "float" and "double".  While it is true that there were CPUs where a machine word was not 8, 16, 32 or 64 bits, that is extremely rare now.  I can't think of a single processor (that is actually used) where that is not the case now.  Chuck Moore's FORTH CPUs are the only ones I can think of and those are not exactly mainstream.

I find myself never using int, short, or char, and instead I always include stdint.h and use int32_t, int16_t and int8_t.  I need to know what size those integral types are.

Java made this mandatory.  "int" in Java is a signed 32 bit in.  Period.  No exceptions.  "long" is a signed 64-bit int.  Again, no exceptions.  While it is very annoying that Java decided that unsigned integers were not interesting, this conformity across platforms is quite handy.    I can use an int in a for loop in Java without worrying about what might happen if it is actually 16-bits on that platform.

I think it would be interesting if C2 would define char, short, int and long (and float and double) to be what people usually think they are (unless you are Microsoft): 8, 16, 32 and 64-bit words.  The translation to IR is straightforward if I understood the LLVM docs correctly (very possibly not!).  Any translation to C would be a simple mapping to one of the intX_t types.

Thoughts?

Best,
Kyle



48
Ideas / Apple's proposed addition of modules to C/C++
« on: June 19, 2014, 02:27:42 AM »
I know this has been hashed over, but Apple made a serious proposal for modules for C.  I thought that it was fairly well thought out.

http://llvm.org/devmtg/2012-11/Gregor-Modules.pdf?=submit

I thought that there was something else to see, but this is what I find with Google...

From what I remember at the time, the C and C++ language committees are (or at least were) seriously considering this proposal.  It seems pretty well thought out. 

Obviously C2 does not need the backward compatible parts, which is a large part of it!  But, there may be something to be learned.  If you've already seen this, feel free to ignore this post :-)

Best,
Kyle

Pages: 1 2 3 [4]