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.
Activity for alx
Type | On... | Excerpt | Status | Date |
---|---|---|---|---|
Answer | — |
A: PGP sign emails sent with git-send-email(1) git-send-email(1) uses sendmail(8) as the MTA by default. However, this can be changed by passing the `--sendmail-cmd` option. ``` $ man git-send-email | sed -n '/--sendmail-cmd=/,/^$/p' --sendmail-cmd= Specify a command to run to send the email. The command should be s... (more) |
— | 12 days ago |
Question | — |
When should I parenthesize macro arguments in C? I've seen macros use parentheses to enclose its arguments. Most of these make sense, as in ```c #define sum(a, b) ((a) + (b)) ``` The outer prevents the following: ```c #define sumbad(a, b) (a) + (b) s = sumbad(x, y) z; // (x) + (y) z ``` The inner prevents the following: ... (more) |
— | 6 months ago |
Answer | — |
A: Is strcpy dangerous and what should be used instead? strcpy(3) can be safe. Some compilers, such as GCC and Clang, use a feature test macro, `FORTIFYSOURCE`, (see featuretestmacros(7) ), to ask the compiler to add some checks to make sure that buffer overflow doesn't happen. If the bug is detected at compile time, it will raise a warning. If the b... (more) |
— | 7 months ago |
Question | — |
Storing more bytes than a union member has, but less than the union size, with memcpy(3) Let's say we have an object, we store it in a union (into some other narrower type, but with memcpy(3), so it's allowed --I guess--), and then read it from the union via it's original type (so no alignment issues or anything. ```c $ cat union.c #include struct s { int a; int b;... (more) |
— | 12 months ago |
Answer | — |
A: How to properly use malloc? There are several things to consider when calling the malloc(3) family of functions: - `nelem sizeof(elem)` or `sizeof(elem) nelem`? - Use the type or the pointer name in `sizeof()`? - To cast or not to cast? TL;DR: Use a simple and readable interface, and let it handle the safety for... (more) |
— | about 1 year ago |
Question | — |
Can freed pointers undergo lvalue conversion? ``` char p, q; p = malloc(1); free(p); q = p; // lvalue conversion ``` Is the last lvalue conversion (`= p;`) Undefined Behavior or not? We didn't take the address of the local `p`. C11::6.3.2.1/1 contains the following sentence regarding lvalue conversions: > If the lvalue des... (more) |
— | about 1 year ago |
Question | — |
memcmp(3) memory containing invalid values What does it mean that we can use memcmp(3) on invalid values? ISO C allows comparing an invalid value through memcmp(3), because it doesn't read the value, but rather its representation, and "reading the representation is never Undefined Behavior" (or so I've been told). The following code inv... (more) |
— | about 1 year ago |
Question | — |
Is partial allocation of an object Undefined Behavior? Is it valid to partly allocate an object, as long as you only use the allocated part of it? ```c #include #include struct s { int i[100]; }; int main(void) { struct s s; s = malloc(50 sizeof(int)); s->i[30] = 7; printf("%d\n", s->i[30]); free(s); } ``` ```sh al... (more) |
— | over 1 year ago |
Question | — |
Strict aliasing rules and function boundaries Let's analyze this code, assuming an architecture where the alignment of `int64t` is the same as that of `double`: ```c void bar(double f, int64t j) { (int64t )f = j; } void foo(void) { int64t i = 1; bar((double ) &i, &i); } ``` - The object is of type `int64t`. - ... (more) |
— | over 1 year ago |
Answer | — |
A: stpecpy(): Design a better string copy function that truncates After addressing @Lundin 's suggestions: - Implemented in terms of libc functions for performance. - const correctness - style improvements I have a few more that I realized after trying to replace some existing code: Much code out there has `char end = buf + nitems(buf);` (although that s... (more) |
— | over 1 year ago |
Answer | — |
A: How to write a macro that discards the const qualifier, for any type? I developed this macro (using GNU C) similar to C++'s constcast(). ```c #define constcast(t, p) \ ({ \ staticassert(builtintypescompatiblep(typeof(&(p)), const t)); \ ... (more) |
— | over 1 year ago |
Answer | — |
A: Assert that some code is not present in the final binary, at compile or link time. Calling an undefined function will have that behavior at link time: `landmine.c`: ```c #ifndef CONSTANT #define CONSTANT 0 #endif #define assertnotinbinaryif(e) do \ { \ if (e) \ undefinedfunction(); \ } while (0) void undefinedfunction(void); [[gnu::noipa]] static void foo0(vo... (more) |
— | almost 2 years ago |
Question | — |
Assert that some code is not present in the final binary, at compile or link time. I'd like to assert that some code can be optimized out, and is not present in the final binary object. ```c #define CONSTANT 0 #if (!CONSTANT) [[landmineA]] #endif static int foo(void); void bar(void) { if (CONSTANT) { foo(); } } static int foo(void) { if (... (more) |
— | almost 2 years ago |
Answer | — |
A: PGP sign emails sent with git-send-email(1) It can't be done with `git-send-email`(1), but there's a tool that integrates with it, and is very simple to use: `patatt`(1). Install the tool: ```sh $ sudo apt-get install patatt ``` And then for each repo in which you want to sign patches, run: ```sh $ cd /some/git/repo/ $ patatt ins... (more) |
— | almost 2 years ago |
Question | — |
array of arrays vs array of pointers to store array of string literals Let's consider the following code: ```c const char a[][4] = {"aa", "aaa"}; const char b[] = {"bb", "bbb"}; const char const c[] = {"cc", "ccc"}; ``` For shared libraries, both `b` and `c` arrays require the array of pointers to be generated at runtime, which implies performance costs. Se... (more) |
— | almost 2 years ago |
Question | — |
How to write a macro that discards the const qualifier, for any type? How to write a macro that discards the const qualifier, for any type? I hope some combination of `typeof` and a cast will do, but haven't found the combination. I tried this, without luck: ```c #define discardconst(x) ((typeof(x + 0)) (x)) struct t { char s; }; char foo(const... (more) |
— | almost 2 years ago |
Question | — |
PGP sign emails sent with git-send-email(1) How can we use git-send-email(1) to sign patches (emails) with the gpg(1) keyring? I've heard it can be done, but couldn't find anything in the git-send-email(1) documentation nor in a web search. (more) |
— | almost 2 years ago |
Question | — |
Is `-isystem` a POSIX cc option? Is `-isystem/path/to/sys/includes` a standard compiler option, or is it a compiler extension implemented by gcc, clang, and maybe other compilers? Can I rely on its availability? I couldn't find the POSIX specification for cc(1). (more) |
— | about 2 years ago |
Question | — |
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: ``` c noreturn void foo(void x) { pthreadexit(x); } ``` ISO C (N2731, C2x) says: > 6.7... (more) |
— | about 2 years ago |
Question | — |
stpecpy(): Design a better string copy function that truncates I was directed a few days ago to a post about a string copy function, which IMO improves the commonly known string copy functions, including strlcpy(3BSD), strlcat(3BSD), and strscpy(9). It defines a function, `char strecopy(char dst, char src, char end)`, where end really means one past the end o... (more) |
— | about 2 years ago |