Implicit declaration of 'memfd_create'

I’m trying to use functions from mman.h in my c code. The code compiles fine with g++/clang++, but when using gcc/clang it says that memfd_create has not been declared, however the code still runs fine. I tried compiling online with godbolt and it’s the same as locally, so I doubt it’s something wrong with my… Read More Implicit declaration of 'memfd_create'

Is there a difference in undefined behaviour in C and C++ when accessing different members of different type in a union for write and read operations?

Let’s start immediately with the example code: #include <stdio.h> #include <stdbool.h> typedef union { bool b; int i; } boolIntUnion; int main(void) { boolIntUnion val; val.i = 0; printf("i = %d; %s\n", val.i, ((val.b) ? "true" : "false")); val.i = 1; printf("i = %d; %s\n", val.i, ((val.b) ? "true" : "false")); val.i = 2; printf("i… Read More Is there a difference in undefined behaviour in C and C++ when accessing different members of different type in a union for write and read operations?

How to do I access an array of structs using ONLY pointers

I am trying to access the students array of structs and modify a "Student" at a given index (using a for loop). However, I am trying to access it using only a pointer, not like this: students[currStudent].theory = thoeryGrade; But something along the lines of: *(studentPtr + currStudent).practical = practicalGrade; Note: The above line doesn’t… Read More How to do I access an array of structs using ONLY pointers

Why are there duplicate C/C++ compilers (e.g g++, gcc)?

According to this answer, gcc and g++ have some functional differences. As confirmed by this script, the commands point to the exact same binary, effectively making them duplicates. Why is that so? $ uname Darwin $ md5 `which cc c++ gcc g++ clang clang++` fac4668657765c8dfe89d8995acfb5a2 /usr/bin/cc fac4668657765c8dfe89d8995acfb5a2 /usr/bin/c++ fac4668657765c8dfe89d8995acfb5a2 /usr/bin/gcc fac4668657765c8dfe89d8995acfb5a2 /usr/bin/g++ fac4668657765c8dfe89d8995acfb5a2 /usr/bin/clang fac4668657765c8dfe89d8995acfb5a2… Read More Why are there duplicate C/C++ compilers (e.g g++, gcc)?

C++ higher order functions: different results when declaring multiple functions

I was plying with higher order functions, and I started getting different results when I created two instances and assigning them to variables. I reduced the issue to the following example: #include <iostream> using ColorGetter = int(*)(void); auto paint(const ColorGetter &f) { return [&]() { return f(); }; } int colorA() { return 10; }… Read More C++ higher order functions: different results when declaring multiple functions

Why can not while(*s++ = *t++); be compiled after checking "out-of-bounds"?

I am currently learning "pointers by following the book "The C Programming Language" by Brian and Dennis. I failed to compile: #include <stdio.h> void _strcpy(char *s, char *t) { while(*s++ = *t++) ; } And the error messages are: warning: using the result of an assignment as a condition without parentheses [-Wparentheses] while(*s++ = *t++)… Read More Why can not while(*s++ = *t++); be compiled after checking "out-of-bounds"?

Running into "error: […] is a c++ extension"

After running: g++ –std=c++11 -ansi -pedantic-errors -Wall -o test_database test_database.cpp I am receiving the following errors: ./database.h:40:10: error: ‘auto’ type specifier is a C++11 extension [-Werror,-Wc++11-extensions] for (auto x:composerMap_) { ^ ./database.h:40:16: error: range-based for loop is a C++11 extension [-Werror,-Wc++11-extensions] for (auto x:composerMap_) { Yet note that I have already added the ‘–std=c++11’ flag… Read More Running into "error: […] is a c++ extension"