Show newer

@Absinthe @alex Yes we have a discourse server. No sadly discourse doesnt federate however there has been talk about writing a plugin for it that does. Since it is the most likely service to federate int he future and the easiest one to hack to make it federate it is usually what we and other instances in the fediverse use.

Oh, yeah, on that line right above the free(ptr); second from the bottom... That is where you might want to do something with ptr :) Sorry, I meant to put that in there.

Show thread

/* If you are going to use realloc, don't make this common mistake! */

<stdlib.h>

int main (void) {

size_t size = 16;

char * ptr = malloc(size);

size = 32;

/* This is wrong!

ptr = realloc(ptr, size);

ptr is allocated. If this call fails ptr will be set to NULL, and
the allocated memory will not be freed.
*/

char * tmp_ptr = realloc(ptr, size);
if (NULL != tmp_ptr) {
ptr = tmp_ptr;
} else {
/* This is the error case */
free(ptr);
ptr = NULL;
}

free(ptr);
}

Cute little trick for gcc or clang users: (run valgrind on it afterwards)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifdef __cplusplus
extern "C" {
#endif

void raii_char_free(char **);

#define raii_char_pointer char * __attribute__ ((__cleanup__(raii_char_free)))

#ifdef __cplusplus
}
#endif

void raii_char_free(char **p) {
free(*p);
}

int main (void) {
raii_char_pointer ptr = strdup("abcdefg");
printf("%s\n", ptr);
return 0;
}

use a Makefile early and often.
Learn how to use a Makefile... Well.

Write unit tests before code.
Write documentation before code.
Use autodocumenting code like Doxygen.

use godbolt.org/ to test code on multiple compilers without having to have them installed locally

If you are programming in C consider adding c++ guards around your file scope variables and prototypes, and use the c++ compiler. You will find additional errors and warnings the c compilers alone won't find. Also, compile with both gcc and clang and use as many others as well as static analysis tools as you can. cppcheck can be your friend. valgrind can be your best friend.

when developing use -Wall and
-Wextra with gcc and -Weverything with clang... as a minimum. Consider pedantic also.

do not use dynamically allocated arrays on the stack. Do so on the heap only

if you allocate memory, you need to free it. strdup() allocates memory.... damnit!

String functions such as *printf() str*cmp() and others are meant to take string pointers. Under no circumstances should you use a function call, that returns NULL to indicate an error (like getenv()), as a parameter.

Unless you are working with an 8 bit compiler 0, '\0', and NULL are not synonyms.

When a C compiler gives you a warning, there is no reason to ignore it. If your code base is full of warnings how can you ever tell which poorly written piece of code is causing your current problem?

IBM XLC C compiler on AIX will treat a NULL as though it were an empty string "". There is a special circle of hell reserved for people that code counting on that behavior.

Show older
Qoto Mastodon

QOTO: Question Others to Teach Ourselves
An inclusive, Academic Freedom, instance
All cultures welcome.
Hate speech and harassment strictly forbidden.