Retro C++ quiz #31
Given the following in C and C++
// Assume we have math.h
#include <math.h>
// file or global scope
double d = sqrt(2.0);
This is, without checking:
A. Valid C and C++
B. Valid C
C. Valid C++
D. Neither Valid C or C++
Retro C++ quiz #31
Given the following in C and C++
// Assume we have math.h
#include <math.h>
// file or global scope
double d = sqrt(2.0);
This is, without checking:
A. Valid C and C++
B. Valid C
C. Valid C++
D. Neither Valid C or C++
@ljs The libdrgn API is unstable, so there's not even a distro package anywhere that you could do #include <drgn.h> or -ldrgn right now.
That said, I know it gets internal use at Meta, and there are examples in the repo.
I've actually thought it would be interesting to combine libdrgn with a userspace BPF JIT that supports CO-RE, so we could use drgn's type system to resolve type references and execute BPF/kernel like (read-only) code at native speeds in userspace with drgn
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#define MAX_ARGS 16
int main(int argc, char *argv[])
{
int i, fds[MAX_ARGS];
memset(fds, ~0, sizeof(fds));
for (i = 1; i < argc && i < MAX_ARGS; i++) {
fds[i] = open(argv[i], O_RDONLY);
if (fds[i] >= 0) printf("%d\n", fds[i]);
}
for (i = 1; i < argc && i < MAX_ARGS; i++)
if (fds[i] >= 0) close(fds[i]);
return 0;
}
@leo But linux-man-pages is good at consistently telling you to not just `#include <foo.h>` but also when you need to `#define GNU_SOURCE` or `#define POSIX_SOURCE 2000XXXXL` or whatever or even sometimes getting different versions of the function based on which you define (even if the official GNU info manual isn't great at noting such things).
when I look up FooBarA() and MSDN says it's in winfoo.h, I assume that I need to do just #include <winfoo.h> for it to work.
that's how C headers work basically everywhere else, after all
Okay so it's all very new to this #C quiz author so pls excuse dumb mistakes in this quiz, but assuming I didn't make any dumb mistakes, what's wrong in the following C program?
EDIT: as predicted, the quiz was rubbish and has been edited
#include <pthread.h>
_Atomic char * _Atomic p;
void *thr1(void *arg) {
_Atomic char *my_p = p;
if (my_p) *my_p = 0;
return 0;
}
void *thr2(void *arg) {
_Atomic char c = ((p = &c), 0);
while (1);
}
int main(void) {
pthread_t t1, t2;
pthread_create (&t1, 0, thr1, 0);
pthread_create (&t2, 0, thr2, 0);
}
#include <primalscream.h>
And here is what those changes do:
Changes made:
#include <stdio.h> – Includes the standard I/O library needed for printf.
int main(void) – Specifies that main returns an int and takes no arguments.
return 0; – Returns 0 to indicate successful program execution.
C++ Hello World
#include <iostream>
using namespace std;
int main() {
cout << "C++ Example Code" << endl;
return 0;
}
It looks like CW by default auto-includes any Mac headers you need, but at the same time stdlib stuff like "strlen" isn't working even with a manual #include <strings.h> which is the point where I'm starting to look at "maybe I should read a book instead of just poking at things randomly!" (2/2)
How to politely use assert in a header (gcc/clang/msvc/what else?):
#pragma push_macro("NDEBUG")
#undef NDEBUG
#include <assert.h>
// your asserts should properly assert
#pragma pop_macro("NDEBUG")
// nobody has 2 know
Your vendor’s assert.h should be able to be included multiple times with NDEBUG defined or not.
@jduck I mean, if it convinces you...
$ cat blub.c
#include <stdlib.h>
#include <stdio.h>
struct foo {
long a;
int b;
int arr[];
};
int main(void) {
struct foo *a = malloc(sizeof(struct foo) + sizeof(int)*3);
struct foo *b = malloc(sizeof(struct foo) + sizeof(int)*3);
if (!a || !b)
return 1;
a->a = 1;
a->b = 2;
for (int i=0; i<3; i++)
a->arr[i] = i + 123456;
b->a = 101;
b->b = 102;
for (int i=0; i<3; i++)
b->arr[i] = i + 100;
*b = *a;
for (int i=0; i<3; i++)
printf("%d ", b->arr[i]);
printf("\n");
}
$ gcc -o blub blub.c
$ ./blub
123456 101 102
Why does the C standard not mention any UB in its definition of offsetof()? Is it implicit somewhere else that doing offsetof(struct foo, arr[SIZE_MAX])
on a struct containing a flexible array member is UB?
Both GCC and clang don't diagnose anything here and compile it to a "return 0":
#include <stddef.h>
#include <inttypes.h>
struct foo {
int a;
int arr[];
};
unsigned long func(void) {
return offsetof(struct foo, arr[SIZE_MAX]);
}
Funny standard C behavior - assigning a struct with flexible array member can copy parts of the flexible array member:
$ cat vla_assign.c
#include <stdlib.h>
#include <stdio.h>
struct foo {
long a;
int b;
int arr[];
};
int main(void) {
struct foo *a = malloc(sizeof(struct foo) + sizeof(int)*3);
struct foo *b = malloc(sizeof(struct foo) + sizeof(int)*3);
if (!a || !b)
return 1;
a->a = 1;
a->b = 2;
for (int i=0; i<3; i++)
a->arr[i] = i;
b->a = 101;
b->b = 102;
for (int i=0; i<3; i++)
b->arr[i] = i + 100;
*b = *a;
for (int i=0; i<3; i++)
printf("%d ", b->arr[i]);
printf("\n");
}
$ gcc -Wall -Wextra -pedantic -o vla_assign vla_assign.c
$ ./vla_assign
0 101 102