The C Programming Language
!c_programming
help-circle
rss
Enforce same size for arrays at compile-time?
Let's say I have two arrays that have related data: ``` const char *backend_short[] = { "oal", "pa", "sdl_m" }; const char *backend_long[] = { "openal", "portaudio", "sdl_mixer" }; ``` Does C support a way to "assert" that these two arrays have the same size? And failing compilation if they are different? I want a safeguard in case I'm drunk one day and forget to keep these synchronized. Thanks in advance. EDIT: I found a solution. Here are some enlightening resources on the matter: * https://www.pixelbeat.org/programming/gcc/static_assert.html * https://www.embedded.com/catching-errors-early-with-compile-time-assertions/
fedilink

Readline from stdin
If one has POSIX extensions available, then it seems that defining _POSIX_C_SOURCE and just using getline or detdelim is the way to go. However, for source that is just C, here is a solution I've found from various sources, mainly [here](https://stackoverflow.com/questions/16870485/how-can-i-read-an-input-string-of-unknown-length). I've also tried to make it more safe. ``` // Read line from stdin #include <stdio.h> #include <stdlib.h> #define CHUNK_SIZE 16 // Allocate memory in chunks of this size // Safe self-realloc void *xrealloc(void *ptr, size_t size) { void *tmp = realloc(ptr, size); if (!tmp) free(ptr); return tmp; } // Dynamically allocate memory for char pointer from stdin char *readline(void) { size_t len = 0, chunk = CHUNK_SIZE; char *str = (char *)malloc(CHUNK_SIZE); if (!str) return NULL; int ch; while((ch = getchar()) != EOF && ch != '\n' && ch != '\r') { str[len++] = ch; if (len == chunk) { str = (char *)xrealloc(str, chunk+=CHUNK_SIZE); if (!str) return NULL; } } str[len++] = '\0'; // Ensure str is null-terminated return (char *)xrealloc(str, len); } int main(void) { setbuf(stdout, NULL); // Ensure environment doesn't buffer stdout printf("Enter name: "); char *userName = readline(); if (!userName) return 1; printf("Hello, %s!\n", userName); free(userName); return 0; } ``` The idea is that we allocate in chunks of 16, or whichever size. xrealloc is handy when reallocating a block of memory to itself. ``` int *ptr = malloc(sizeof(int) * 4); ptr = (int *)realloc(ptr, sizeof(int) * 8); ``` If realloc fails, ptr gets assigned `NULL`, and we lose the address that we need to free, causing a memory leak. So, xrealloc allows for safe self re-allocation.

My journey through the first C implementation of (m)alloc
Did some digging in The Unix Heritage Society's copy of Research UNIX v6 and wrote what I learned from reading decade-old C. Quite much, it turned out! Markdown source available at https://ljrk.codeberg.page/unixv6-alloc.md
fedilink




I am *not* the author of dwl. Since dwl is based on wlroots (just like the popular Sway) it already supports making screenshots using [grim][2] and screencasts using [wf-recorder][3]. You can try out dwl from within your current WM. The default modkey is `Alt`. If you want to use the Super key, change `#define MODKEY WLR_MODIFIER_ALT` in `config.h` to `#define MODKEY WLR_MODIFIER_LOGO` and recompile. The default terminal emulator is kitty but you can change `termcmd` to alacritty if you want. Currently, only native Wayland applications run on it. You can enable experimental Wayland support for Firefox with `MOZ_ENABLE_WAYLAND=1`, see [Running programs natively under Wayland in Sway Wiki][1]. Do not hover over the edges of windows – this will crash dwl. [1]: https://github.com/swaywm/sway/wiki/Running-programs-natively-under-wayland [2]: https://github.com/emersion/grim [3]: https://github.com/ammen99/wf-recorder





Looking for a programming buddy to study C together
Hello fellow lemmys (or however do we call us?), as the title suggests, I am looking for someone who wants to learn the C programming language. I have a decent knowledge of computer science and of a couple of programming languages (C#, Java, Python). Currently, I am studying computer science in central/western Europe. In my opinion, studying together keeps each other motivated and is more efficient. In long terms, I hope, that we can contribute to open-source projects together and improve our code quality by reviewing each others code. If any of you has some interest in being my programming buddy, please message me or reply to this post. Preferably from a similar time zone (mine is UTC+01:00). Hoping for some replies Regards

The C Programming Language
!c_programming

    Everything related to the C programming language.

    • 0 users online
    • 1 user / day
    • 1 user / week
    • 1 user / month
    • 2 users / 6 months
    • 391 subscribers
    • 12 Posts
    • 2 Comments
    • Modlog