mastodon.zunda.ninja is one of the many independent Mastodon servers you can use to participate in the fediverse.
Zundon is a single user instance as home of @zundan as well as a test bed for changes of the code.

Administered by:

Server stats:

1
active users

#include

1 post1 participant0 posts today

Wow, __builtin_dump_struct is an amazing clang feature, how did I never hear about this before?

$ cat test.c
#include <stdio.h>

struct nested {
int n;
};
struct foo {
int member_a;
unsigned long member_b;
char *str;
void *ptr;
struct nested nested;
};

int main(void) {
struct foo f = {
.member_a = 123,
.member_b = 0x4141414141414141,
.str = "foobar",
.ptr = &f,
.nested = {.n = 42}
};
__builtin_dump_struct(&f, printf);
}
$ clang -o test test.c && ./test
struct foo {
int member_a = 123
unsigned long member_b = 4702111234474983745
char * str = "foobar"
void * ptr = 0x7fff1df41b78
struct nested nested = {
int n = 42
}
}

The original version of this feature was introduced back in 2018 (though it was reimplemented since in 2022).

clang.llvm.orgClang Language Extensions — Clang 21.0.0git documentation
Continued thread

これはきもいなあw

$ cat main.c
<stdio.h>

int main(void) {
fputs(
"answer.txt"
"\n", stdout
);
return 0;
}
$ cat answer.txt
"42"
$ gcc -Wall main.c
(警告なし)
$ ./a.out
42

Continued thread

-Wsign-conversionとかこれを含む-Wconversionだと教えてもらえました。-Wallに入れておいてもらってもよかったよね…

$ cat main.c
<unistd.h>

int main(void)
{
size_t x;
char buf[42];

x = read(0, buf, sizeof(buf));
return (x < 1);
}
$ gcc -Wsign-conversion main.c
main.c: In function ‘main’:
main.c:8:13: warning: conversion to ‘size_t’ {aka ‘long unsigned int’} from ‘ssize_t’ {aka ‘long int’} may change the sign of the result [-Wsign-conversion]
8 | x = read(0, buf, sizeof(buf));
| ^~~~

明示的にキャストすれば怒られない。

$ cat main.c
<unistd.h>

int main(void)
{
size_t x;
char buf[42];

x = (size_t) read(0, buf, sizeof(buf));
return (x < 1);
}
$ gcc -Wconversion main.c

$ gcc --version
gcc (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0
Copyright (C) 2023 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

man 2 readによると

<unistd.h>
ssize_t read(int fd, void buf[.count], size_t count);

なんだけど

$ cat main.c
<unistd.h>

int main(void)
{
size_t x;
char buf[42];

x = read(0, buf, sizeof(buf));
return (x < 1);
}
$ gcc -Wall -Wextra main.c

は何の警告もくれないんだねえ…

$ gcc --version
gcc (GCC) 14.2.1 20240910
Copyright (C) 2024 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

うーん、Debian12ですら、これかあ。
[ 25%] Building CXX object libtermbench/CMakeFiles/termbench.dir/termbench.cpp.o
/home/uaa/termbench-pro/libtermbench/termbench.cpp:17:10: fatal error: format: そのようなファイルやディレクトリはありません
17 | #include <format>
| ^~~~~~~~
compilation terminated.

"2"へのポインタの内容を*演算子?で取り出してるんだ

$ cat c-js.c
<stdio.h>

int main() {
printf("%d\n", *"2");
}
$ gcc c-js.c && ./a.out
50

Continued thread

UBだ♪

$ cat return.c
<stdlib.h>
<stdio.h>

int function(void){
return;
}

int main(void)
{
printf("%d\n", function());
return 0;
}
$ gcc return.c && ./a.out && ./a.out && ./a.out
return.c: In function ‘function’:
return.c:5:3: warning: ‘return’ with no value, in function returning non-void
5 | return;
| ^~~~~~
return.c:4:5: note: declared here
4 | int function(void){
| ^~~~~~~~
-198725291
1404641621
1343713621

Continued thread

#include "Cello.h"

int main(int argc, char** argv) {

/* Stack objects are created using "$" */
var i0 = $(Int, 5);
var i1 = $(Int, 3);
var i2 = $(Int, 4);

/* Heap objects are created using "new" */
var items = new(Array, Int, i0, i1, i2);

/* Collections can be looped over */
foreach (item in items) {
print("Object %$ is of type %$\n",
item, type_of(item));
}

/* Heap objects destructed via Garbage Collection */
return 0;
}

やったことメモ:

RP2040向けの、RTCから時刻を取得するコードを
github.com/adafruit/circuitpyt

RP2350の場合はpowmanを使って取得するようにしてみた

#ifdef PICO_RP2350
#include "hardware/powman.h"
time_t rp2_rtctime_seconds(time_t *timer) {
return powman_timer_get_ms() / 1000ULL;
}
#endif

が、CircuitPython側でRuntimeError: pystack exhaustedで死ぬので、たぶんまだなんかおかしい

GitHubcircuitpython/lib/mbedtls_config/mbedtls_port.c at main · adafruit/circuitpythonCircuitPython - a Python implementation for teaching coding with microcontrollers - adafruit/circuitpython

#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int a, b, c, x;
printf("1 から 100 までの整数を1つ考えてください.\n");
printf("それを 3 で割った余り? "); scanf("%d", &a);
printf("それを 5 で割った余り? "); scanf("%d", &b);
printf("それを 7 で割った余り? "); scanf("%d", &c);
x = (70 * a + 21 * b + 15 * c) % 105;
printf("あなたの考えた数は %d でしょう!\n", x);
return 0;
}

2024-{07-02} Day{9}
#100DaysOfCode

#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int a, b, c, x;
printf("1 から 100 までの整数を1つ考えてください.\n");
printf("それを 3 で割った余り? "); scanf("%d", &a);
printf("それを 5 で割った余り? "); scanf("%d", &b);
printf("それを 7 で割った余り? "); scanf("%d", &c);
x = (70 * a + 21 * b + 15 * c) % 105;
printf("あなたの考えた数は %d でしょう!\n", x);
return 0;
}

2024-{06-28} Day{6}
#100DaysOfCode