22

I've noticed in some source code the line:

if(pthread_create((pthread_t[]){}, 0, start_thread, pthread_args)) {
...

It works correctly, but how to understand the first argument? It seems, that curly braces converts to pthread_t[] type.

P.s. I googled, but didn't find answer, only some guesses (some form of initialization, or legacy feature of c?)

2

4 Answers 4

19

This is a compound literal, with a constraint violation since initializer braces cannot be empty:

(pthread_t[]){}

Using gcc -std=c99 -Wall -Wextra -Wpedantic this produces the warning:

compound_literal_pthread.c:6:36: warning: ISO C forbids empty initializer braces [-Wpedantic]
     pthread_t *ptr = (pthread_t []){};

The result seems to be a pointer to pthread_t, though I don't see this behavior documented in the gcc manual. Note that empty braces are allowed as initializers in C++, where they are equivalent to { 0 }. This behavior seems to be supported for C, but undocumented, by gcc. I suspect that is what is happening here, making the above expression equivalent to:

(pthread_t[]){ 0 }

On my system, pthread_t is a typedef for unsigned long, so this expression would create an array of pthread_t containing only a 0 element. This array would decay to a pointer to pthread_t in the function call.

3
  • 6
    And as a perverted side-effect, the thread is not joinable because the code doesn't save the pthread_t accessibly. In effect, the thread is detached, without the benefit of being formally detached. As a way of creating a thread, it leaves a lot to be desired. Commented May 31, 2017 at 14:25
  • I think every type in C can be properly initialized with { 0 }, but it's not clear to me why the empty braces aren't allowed: given that any individual element of the object being initialized is allowed to be implicit, why not make that true for them all at once? (and { 0 } might be technically permissible for many types, but not "right" in the sense of being clean to read) Commented May 31, 2017 at 18:21
  • @Leushenko-- Offhand I can't think of a type that { 0 } would not work for, though there may be warnings about missing braces. IAC, I think that what you say about initializers with missing elements makes sense, if I understand you correctly. I believe that some feel like this is a "bug" in the C spec that C++ gets right. Incidentally, your observation that { 0 } may not be "right" for a particular type is why I double-checked that pthread_t is an unsigned long! Commented May 31, 2017 at 18:45
1

It's a compound literal as mentioned by @some-programmer-dude.

In this specific case it is use to create an array to store the thread_id and discharge it later without the need of creating an extra variable. That is necessary because pthread_create does not accept NULL as argument for thread_id.

0

You're creating an array with pthread[]. You can pass values in the curly braces if you define a length for the argument.

0

What you're dealing with there is an array initializer, which happens to be the empty array. You'd usually find it as:

int my_array[] = (int[]){10, 20, 30};

And it would initialize my_array to contain three elements. Here there are no elements, hence the awkward syntax.

5
  • 2
    is an empty brace-enclosed list considered a valid syntax? AFAIR, it must contain at least one element. Commented May 31, 2017 at 11:57
  • Yes, it's a valid syntax. You don't need to write the curly braces in that case but you can. Commented May 31, 2017 at 11:59
  • 3
    @JohannesMols-- it is not valid syntax, it is a constraint violation to use empty braces with an initializer. Commented May 31, 2017 at 12:22
  • 2
    int my_array[] = (int[]){10, 20, 30}; gives error: array initialized from non-constant array expression. Perhaps you meant int *my_array = (int[]){10, 20, 30};?
    – Spikatrix
    Commented May 31, 2017 at 12:41
  • Note that GCC (7.1.0 tested) permits the compound literal as initializer unless you turn on -pedantic warnings/errors. For example, using gcc -O3 -g -std=c11 -Wall -Wextra -Werror -pedantic -c di59.c generates the error (would be warning without -Werror): di59.c:1:18: error: initializer element is not constant [-Werror=pedantic] int my_array[] = (int[]){10, 20, 30}; but leave the -pedantic out of the options and it is given a clean bill of health. The 'cast' portion of the compound literal should be removed as a basic initializer, of course: int my_array[] = { 10, 20, 30 };. Commented Jun 1, 2017 at 23:34

Your Answer

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

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