12

Sometimes, in C, you do this:

typedef struct foo {
   unsigned int some_data;
} foo; /* btw, foo_t is discouraged */

To use this new type in an OO-sort-of-way, you might have alloc/free pairs like these:

foo *foo_alloc(/* various "constructor" params */);
void foo_free(foo *bar);

Or, alternatively init/clear pairs (perhaps returning error-codes):

int foo_init(foo *bar, /* and various "constructor" params */);
int foo_clear(foo *bar);

I have seen the following idiom used, in particular in the MPFR library:

struct foo {
   unsigned int some_data;
};
typedef struct foo foo[1]; /* <- notice, 1-element array */
typedef struct foo *foo_ptr; /* let's create a ptr-type */

The alloc/free and init/clear pairs now read:

foo_ptr foo_alloc(/* various "constructor" params */);
void foo_free(foo_ptr bar);
int foo_init(foo_ptr bar, /* and various "constructor" params */);
int foo_clear(foo_ptr bar);

Now you can use it all like this (for instance, the init/clear pairs):

int main()
{  
   foo bar; /* constructed but NOT initialized yet */
   foo_init(bar); /* initialize bar object, alloc stuff on heap, etc. */
   /* use bar */
   foo_clear(bar); /* clear bar object, free stuff on heap, etc. */
}

Remarks: The init/clear pair seems to allow for a more generic way of initializing and clearing out objects. Compared to the alloc/free pair, the init/clear pair requires that a "shallow" object has already been constructed. The "deep" construction is done using init.

Question: Are there any non-obvious pitfalls of the 1-element array "type-idiom"?

2 Answers 2

15

This is very clever (but see below).

It encourages the misleading idea that C function arguments can be passed by reference.

If I see this in a C program:

foo bar;
foo_init(bar);

I know that the call to foo_init does not modify the value of bar. I also know that the code passes the value of bar to a function when it hasn't initialized it, which is very probably undefined behavior.

Unless I happen to know that foo is a typedef for an array type. Then I suddenly realize that foo_init(bar) is not passing the value of bar, but the address of its first element. And now every time I see something that refers to type foo, or to an object of type foo, I have to think about how foo was defined as a typedef for a single-element array before I can understand the code.

It is an attempt to make C look like something it's not, not unlike things like:

#define BEGIN {
#define END }

and so forth. And it doesn't result in code that's easier to understand because it uses features that C doesn't support directly. It results in code that's harder to understand (especially to readers who know C well), because you have to understand both the customized declarations and the underlying C semantics that make the whole thing work.

If you want to pass pointers around, just pass pointers around, and do it explicitly. See, for example, the use of FILE* in the various standard functions defined in <stdio.h>. There is no attempt to hide pointers behind macros or typedefs, and C programmers have been using that interface for decades.

If you want to write code that looks like it's passing arguments by reference, define some function-like macros, and give them all-caps names so knowledgeable readers will know that something odd is going on.

I said above that this is "clever". I'm reminded of something I did when I was first learning the C language:

#define EVER ;;

which let me write an infinite loop as:

for (EVER) {
    /* ... */
}

At the time, I thought it was clever.

I still think it's clever. I just no longer think that's a good thing.

2
  • 1
    Interestingly, this question was born out of the same likely frustration that you mention. Namely, that as a user of the object type Foo, you would probably need to understand the underlying typedef definition. I was trying to use the MPFR library, and was confused because of this. But I forgot that - and I found it clever only because it took a long time for me to understand it :-) In that library, it might make a bit more sense, since you would need to easily create and initialize small objects (numbers) on the stack. But more fancy (clever) stuff gets tricky. Sep 1, 2013 at 20:21
  • 2
    Nice one, Keith! It was very much a pleasure reading your answer. SO needs more answers, like this one, that have flair, style, and extras like fun personal memories which give the reader more than a mere answer. Sep 2, 2013 at 0:48
3

The only advantage to this method is nicer looking code and easier typing. It allows the user to create the struct on the stack without dynamic allocation like so:

foo bar;

However, the structure can still be passed to functions that require a pointer type, without requiring the user to convert to a pointer with &bar every time.

foo_init(bar);

Without the 1 element array, it would require either an alloc function as you mentioned, or constant & usage.

foo_init(&bar);

The only pitfall I can think of is the normal concerns associated with direct stack allocation. If this in a library used by other code, updates to the struct may break client code in the future, which would not happen when using an alloc free pair.

4
  • 1
    Exactly - that's my view on it too. It makes notation a bit simpler. Sep 1, 2013 at 16:59
  • 1
    And also less obvious as it decreases readability, and breaks our internal parsers (brains). Please do not do this.
    – user529758
    Sep 1, 2013 at 20:20
  • @H2CO3 I agree, sticking with standard language syntax and usage is usually best even if it involves a little more typing. Sep 1, 2013 at 21:07
  • 1
    But typedef struct Foo Foo; seems to be rather accepted? We can see it, rather simplified, as removing the word "struct" from the type name. Other uses of typedef that hides specific language features (such as use of arrays) should probably be discouraged. Sep 2, 2013 at 6:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.