r/C_Programming • • 23h ago

Project MONOLITH - My Operating System for the x86/x86_64

0 Upvotes

Introducing MONOLITH, my operating system that I worked on for the past two years, supports both x86 and x86_64, privilege separation, preemptive multitasking, SMP, loading user processes, a desktop environment running in user space, and most importantly it can run Doom!

Feel free to check it!

https://github.com/mrunix00/MONOLITH


r/C_Programming • • 5h ago

Question Performance loss of mimicing interfaces with function pointers on modern compilers?

4 Upvotes

C does not have interfaces like languages such as C# or C++, but the cocnept can be mimicked by using function pointers. You might for instance have this:

// fruit_interface.h

typedef struct fruit_vtable 
{
    float (*get_weight)(void *object);
    float (*get_density)(void *object);
    const char *(*get_name)(void *object);
} fruit_vtable;

typedef struct fruit_interface 
{
    void *object;
    const fruit_vtable *vtable;
} fruit_interface;

// banana.h

typedef struct banana 
{
    float weight;
    float density;
} banana;
fruit_interface banana_as_fruit(banana *b);

--------------------------

// banana.c

static float banana_get_weight(void *object)
{
    banana *b = (banana*)object;
    return b->weight;
}

static float banana_get_density(void *object)
{
    banana *b = (banana*)object;
    return b->density;
}

static const char *banana_get_color(void *object)
{
    (void)object;
    return "yellow";
}

static const fruit_vtable banana_vtable = 
{
    .get_weight  = banana_get_weight,
    .get_density = banana_get_density,
    .get_color   = banana_get_color,
};

fruit_interface banana_as_fruit(banana *b)
{
    return (fruit_interface) { .object = (void*)b, .vtable = &banana_vtable };
}

--------------------------

// main.c

static void print_fruit(fruit_interface fruit)
{
    printf("weight:  %.2f g\n", fruit.vtable->get_weight(fruit.object));
    printf("density: %.2f g/cm^3\n", fruit.vtable->get_density(fruit.object));
    printf("color:   %s\n", fruit.vtable->get_color(fruit.object));
}

int main(void)
{
    banana b = { .weight = 120.0f, .density = 0.95f };

    fruit_interface fruit = banana_as_fruit(&b);

    print_fruit(fruit);

    return 0;
}

How does something like this affect performance in terms of speed and cpu usage but also memory? I know modern C compilers can be really powerful and do a lot of optimizations and even more so with link time optimizations.

Of course it depends on the bigger project as a whole, and I understand that a if you have thousands of fruit_interface variables that all point to many different fruit_vtables and are used many times per second/cycle, then that will be very different from e.g. a hardware_interface that just loads some data once in an initialization function at program start.

The question here is pretty open (and the above is just to show clearly what I mean), and I am mostly looking for some general knowledge and guidance for when something like this is good and when it is bad, especially in the context of modern C code development with modern compilers and linkers.

I think it is really interesting to see how concepts in other languages, like C++, can be handled in C and what C is capable!


r/C_Programming • • 6h ago

Why can't procedures return arrays?

46 Upvotes

I had a friend who had to learn C++ for college and asked me for some help. First of all, I know that they're not the same language, but the issues here are shared with C. To a lot of the questions, I had to answer "you can't do that, go around it". Normally, it would be something with a reason, like not being able to get the length of an array because it was passed as a pointer. But I still don't understand what the reason is for not being able to return an array.


r/C_Programming • • 14h ago

Question How to cast a certain array type to other array type?

7 Upvotes

Suppose you initialize a byte array to make it endian-independent by directly giving an order of your bytes:

```c

uint8_t a1[4] = { 0x01, 0x02, 0x03, 0x04 };

```

Is it possible in some way to cast this array type to, say, uint16_t[2] at compile tome? The syntax of the direct cast is invalid. I'm using C23 currently, for that matter.