Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
Is it legal ISO C to declare a function as noreturn with a non-void return type (but of course not actually returning)? As far as I can read from the standard, it seems legal. Example: noreturn ...
#4: Post edited
- Is it legal ISO C to declare a function as `noreturn` with a non-`void` return type (but of course not actually returning)?
- As far as I can read from the standard, it seems legal.
- Example:
```- noreturn void *foo(void *x)
- {
- pthread_exit(x);
- }
- ```
- ISO C [(N2731, C2x)](http://www.open-std.org/JTC1/SC22/WG14/www/docs/n2731.pdf) says:
- > **6.7.4 Function specifiers**
- >
- > 8 A function declared with a `_Noreturn` function specifier shall not return to its caller.
- >
- > **Recommended practice**
- >
- > 9 The implementation should produce a diagnostic message for a function declared with a `_Noreturn`
- function specifier that appears to be capable of returning to its caller.
- >
- > 12 **EXAMPLE 2**
```- _Noreturn void f () {
- abort(); // ok
- }
- _Noreturn void g (int i) { // causes undefined behavior if i <= 0
- if (i > 0) abort();
- }
- ```
- > **J.2 Undefined behavior**
- > - A function declared with a _Noreturn function specifier returns to its caller (6.7.4)
- There's some doubt in 6.7.4/9, where it says that it's recommended to diagnostic a function that appears to be capable of returning to its caller (`noreturn void *foo(void *x)`, from seeing only it's prototype, "appears to be capable of returning to its caller"), but the other text (and the example) seems to allow it as long as the function body doesn't seem to return.
- If it's valid, it can be useful to pass `&foo` to `pthread_create(3)`, which expects a function that returns `void *`, while marking it as `noreturn`, since it won't return.
- [`pthread_create(3):`](https://man7.org/linux/man-pages/man3/pthread_create.3.html)
```- #include <pthread.h>
- int pthread_create(pthread_t *restrict thread,
- const pthread_attr_t *restrict attr,
- void *(*start_routine)(void *),
- void *restrict arg);
- ```
- Current GCC and Clang seem to not warn about it, with `-Wall -Wextra` (and Clang `-Weverything`), so it looks good.
- Is it legal ISO C to declare a function as `noreturn` with a non-`void` return type (but of course not actually returning)?
- As far as I can read from the standard, it seems legal.
- Example:
- ``` c
- noreturn void *foo(void *x)
- {
- pthread_exit(x);
- }
- ```
- ISO C [(N2731, C2x)](http://www.open-std.org/JTC1/SC22/WG14/www/docs/n2731.pdf) says:
- > **6.7.4 Function specifiers**
- >
- > 8 A function declared with a `_Noreturn` function specifier shall not return to its caller.
- >
- > **Recommended practice**
- >
- > 9 The implementation should produce a diagnostic message for a function declared with a `_Noreturn`
- function specifier that appears to be capable of returning to its caller.
- >
- > 12 **EXAMPLE 2**
- ``` c
- _Noreturn void f () {
- abort(); // ok
- }
- _Noreturn void g (int i) { // causes undefined behavior if i <= 0
- if (i > 0) abort();
- }
- ```
- > **J.2 Undefined behavior**
- > - A function declared with a _Noreturn function specifier returns to its caller (6.7.4)
- There's some doubt in 6.7.4/9, where it says that it's recommended to diagnostic a function that appears to be capable of returning to its caller (`noreturn void *foo(void *x)`, from seeing only it's prototype, "appears to be capable of returning to its caller"), but the other text (and the example) seems to allow it as long as the function body doesn't seem to return.
- If it's valid, it can be useful to pass `&foo` to `pthread_create(3)`, which expects a function that returns `void *`, while marking it as `noreturn`, since it won't return.
- [`pthread_create(3):`](https://man7.org/linux/man-pages/man3/pthread_create.3.html)
- ``` c
- #include <pthread.h>
- int pthread_create(pthread_t *restrict thread,
- const pthread_attr_t *restrict attr,
- void *(*start_routine)(void *),
- void *restrict arg);
- ```
- Current GCC and Clang seem to not warn about it, with `-Wall -Wextra` (and Clang `-Weverything`), so it looks good.
#3: Post edited
- Is it legal ISO C to declare a function as `noreturn` with a non-`void` return type (but of course not actually returning)?
- As far as I can read from the standard, it seems legal.
- Example:
- ```
- noreturn void *foo(void *x)
- {
- pthread_exit(x);
- }
- ```
- ISO C [(N2731, C2x)](http://www.open-std.org/JTC1/SC22/WG14/www/docs/n2731.pdf) says:
- > **6.7.4 Function specifiers**
- >
- > 8 A function declared with a `_Noreturn` function specifier shall not return to its caller.
- >
- > **Recommended practice**
- >
- > 9 The implementation should produce a diagnostic message for a function declared with a `_Noreturn`
- function specifier that appears to be capable of returning to its caller.
- >
- > 12 **EXAMPLE 2**
- ```
- _Noreturn void f () {
- abort(); // ok
- }
- _Noreturn void g (int i) { // causes undefined behavior if i <= 0
- if (i > 0) abort();
- }
- ```
- > **J.2 Undefined behavior**
- > - A function declared with a _Noreturn function specifier returns to its caller (6.7.4)
There's some doubt in 6.7.4/9, where it says that it's recommended to diagnostic a function that appears to be capable of returning to its caller (`noreturn void *foo(void *x)`, from seeing only it's prototype, "appears to be capable of returning to its caller"), but the other text seems to allow it as long as the function body doesn't seem to return.- If it's valid, it can be useful to pass `&foo` to `pthread_create(3)`, which expects a function that returns `void *`, while marking it as `noreturn`, since it won't return.
- [`pthread_create(3):`](https://man7.org/linux/man-pages/man3/pthread_create.3.html)
- ```
- #include <pthread.h>
- int pthread_create(pthread_t *restrict thread,
- const pthread_attr_t *restrict attr,
- void *(*start_routine)(void *),
- void *restrict arg);
- ```
- Current GCC and Clang seem to not warn about it, with `-Wall -Wextra` (and Clang `-Weverything`), so it looks good.
- Is it legal ISO C to declare a function as `noreturn` with a non-`void` return type (but of course not actually returning)?
- As far as I can read from the standard, it seems legal.
- Example:
- ```
- noreturn void *foo(void *x)
- {
- pthread_exit(x);
- }
- ```
- ISO C [(N2731, C2x)](http://www.open-std.org/JTC1/SC22/WG14/www/docs/n2731.pdf) says:
- > **6.7.4 Function specifiers**
- >
- > 8 A function declared with a `_Noreturn` function specifier shall not return to its caller.
- >
- > **Recommended practice**
- >
- > 9 The implementation should produce a diagnostic message for a function declared with a `_Noreturn`
- function specifier that appears to be capable of returning to its caller.
- >
- > 12 **EXAMPLE 2**
- ```
- _Noreturn void f () {
- abort(); // ok
- }
- _Noreturn void g (int i) { // causes undefined behavior if i <= 0
- if (i > 0) abort();
- }
- ```
- > **J.2 Undefined behavior**
- > - A function declared with a _Noreturn function specifier returns to its caller (6.7.4)
- There's some doubt in 6.7.4/9, where it says that it's recommended to diagnostic a function that appears to be capable of returning to its caller (`noreturn void *foo(void *x)`, from seeing only it's prototype, "appears to be capable of returning to its caller"), but the other text (and the example) seems to allow it as long as the function body doesn't seem to return.
- If it's valid, it can be useful to pass `&foo` to `pthread_create(3)`, which expects a function that returns `void *`, while marking it as `noreturn`, since it won't return.
- [`pthread_create(3):`](https://man7.org/linux/man-pages/man3/pthread_create.3.html)
- ```
- #include <pthread.h>
- int pthread_create(pthread_t *restrict thread,
- const pthread_attr_t *restrict attr,
- void *(*start_routine)(void *),
- void *restrict arg);
- ```
- Current GCC and Clang seem to not warn about it, with `-Wall -Wextra` (and Clang `-Weverything`), so it looks good.
#2: Post edited
- Is it legal ISO C to declare a function as `noreturn` with a non-`void` return type (but of course not actually returning)?
- As far as I can read from the standard, it seems legal.
- Example:
- ```
- noreturn void *foo(void *x)
- {
- pthread_exit(x);
- }
- ```
ISO C (N2731, C2x) says:- > **6.7.4 Function specifiers**
- >
- > 8 A function declared with a `_Noreturn` function specifier shall not return to its caller.
- >
- > **Recommended practice**
- >
- > 9 The implementation should produce a diagnostic message for a function declared with a `_Noreturn`
- function specifier that appears to be capable of returning to its caller.
- >
- > 12 **EXAMPLE 2**
- ```
- _Noreturn void f () {
- abort(); // ok
- }
- _Noreturn void g (int i) { // causes undefined behavior if i <= 0
- if (i > 0) abort();
- }
- ```
- > **J.2 Undefined behavior**
- > - A function declared with a _Noreturn function specifier returns to its caller (6.7.4)
- There's some doubt in 6.7.4/9, where it says that it's recommended to diagnostic a function that appears to be capable of returning to its caller (`noreturn void *foo(void *x)`, from seeing only it's prototype, "appears to be capable of returning to its caller"), but the other text seems to allow it as long as the function body doesn't seem to return.
- If it's valid, it can be useful to pass `&foo` to `pthread_create(3)`, which expects a function that returns `void *`, while marking it as `noreturn`, since it won't return.
`pthread_create(3):`- ```
- #include <pthread.h>
- int pthread_create(pthread_t *restrict thread,
- const pthread_attr_t *restrict attr,
- void *(*start_routine)(void *),
- void *restrict arg);
- ```
- Current GCC and Clang seem to not warn about it, with `-Wall -Wextra` (and Clang `-Weverything`), so it looks good.
- Is it legal ISO C to declare a function as `noreturn` with a non-`void` return type (but of course not actually returning)?
- As far as I can read from the standard, it seems legal.
- Example:
- ```
- noreturn void *foo(void *x)
- {
- pthread_exit(x);
- }
- ```
- ISO C [(N2731, C2x)](http://www.open-std.org/JTC1/SC22/WG14/www/docs/n2731.pdf) says:
- > **6.7.4 Function specifiers**
- >
- > 8 A function declared with a `_Noreturn` function specifier shall not return to its caller.
- >
- > **Recommended practice**
- >
- > 9 The implementation should produce a diagnostic message for a function declared with a `_Noreturn`
- function specifier that appears to be capable of returning to its caller.
- >
- > 12 **EXAMPLE 2**
- ```
- _Noreturn void f () {
- abort(); // ok
- }
- _Noreturn void g (int i) { // causes undefined behavior if i <= 0
- if (i > 0) abort();
- }
- ```
- > **J.2 Undefined behavior**
- > - A function declared with a _Noreturn function specifier returns to its caller (6.7.4)
- There's some doubt in 6.7.4/9, where it says that it's recommended to diagnostic a function that appears to be capable of returning to its caller (`noreturn void *foo(void *x)`, from seeing only it's prototype, "appears to be capable of returning to its caller"), but the other text seems to allow it as long as the function body doesn't seem to return.
- If it's valid, it can be useful to pass `&foo` to `pthread_create(3)`, which expects a function that returns `void *`, while marking it as `noreturn`, since it won't return.
- [`pthread_create(3):`](https://man7.org/linux/man-pages/man3/pthread_create.3.html)
- ```
- #include <pthread.h>
- int pthread_create(pthread_t *restrict thread,
- const pthread_attr_t *restrict attr,
- void *(*start_routine)(void *),
- void *restrict arg);
- ```
- Current GCC and Clang seem to not warn about it, with `-Wall -Wextra` (and Clang `-Weverything`), so it looks good.
#1: Initial revision
noreturn function with non-void return type
Is it legal ISO C to declare a function as `noreturn` with a non-`void` return type (but of course not actually returning)? As far as I can read from the standard, it seems legal. Example: ``` noreturn void *foo(void *x) { pthread_exit(x); } ``` ISO C (N2731, C2x) says: > **6.7.4 Function specifiers** > > 8 A function declared with a `_Noreturn` function specifier shall not return to its caller. > > **Recommended practice** > > 9 The implementation should produce a diagnostic message for a function declared with a `_Noreturn` function specifier that appears to be capable of returning to its caller. > > 12 **EXAMPLE 2** ``` _Noreturn void f () { abort(); // ok } _Noreturn void g (int i) { // causes undefined behavior if i <= 0 if (i > 0) abort(); } ``` > **J.2 Undefined behavior** > - A function declared with a _Noreturn function specifier returns to its caller (6.7.4) There's some doubt in 6.7.4/9, where it says that it's recommended to diagnostic a function that appears to be capable of returning to its caller (`noreturn void *foo(void *x)`, from seeing only it's prototype, "appears to be capable of returning to its caller"), but the other text seems to allow it as long as the function body doesn't seem to return. If it's valid, it can be useful to pass `&foo` to `pthread_create(3)`, which expects a function that returns `void *`, while marking it as `noreturn`, since it won't return. `pthread_create(3):` ``` #include <pthread.h> int pthread_create(pthread_t *restrict thread, const pthread_attr_t *restrict attr, void *(*start_routine)(void *), void *restrict arg); ``` Current GCC and Clang seem to not warn about it, with `-Wall -Wextra` (and Clang `-Weverything`), so it looks good.