Use GCC in your Project

Started by Stan Duraham, June 10, 2026, 09:23:04 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Stan Duraham

This is just something I came across a while back, looked interesting, so I kept a note. I'm not sure where GCC fits in with your process. Might be useful, might not. I think it's C11 or greater.
Possibly be used to trigger a destructor when a UDT goes out of scope? Possibly varptr(UDT)?
---
The __attribute__((cleanup(...))) is a compiler extension supported  by GCC and Clang. It allows you to specify a function that is automatically called when a variable to which it is applied goes out of scope.  This provides a form of automatic resource management in C, similar to the Resource Acquisition Is Initialization (RAII) idiom in C.
- Trigger: The cleanup function executes when execution leaves the block where the variable is defined.
- Argument: The cleanup function must accept exactly one argument: a pointer to the variable being cleaned up.
- Return Type: The cleanup function must return void.

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

// 1. Define the cleanup function.
// It receives a pointer to the variable (FILE** in this case).
void close_file_pointer(FILE **fp) {
    if (fp && *fp) {
        printf("[Cleanup] Automatically closing file...\n");
        fclose(*fp);
    }
}

int main(void) {
    printf("Starting main function.\n");

    // 2. Apply the attribute to the variable.
    // Pass the name of your cleanup function.
    __attribute__((cleanup(close_file_pointer))) FILE *my_file = fopen("msys2_test.txt", "w");

    if (!my_file) {
        perror("Failed to open file");
        return EXIT_FAILURE;
    }

    // 3. Use the resource normally.
    fprintf(my_file, "Hello from MSYS2 MINGW-W64!\n");
    printf("Wrote data to file. Leaving block now...\n");

    // Notice: We do NOT explicitly call fclose(my_file).
    // The compiler inserts the call to close_file_pointer right here.
    return EXIT_SUCCESS;
}


Forget about __attribute__((cleanup(...))), for GCC on windows. Broken.
DeepSeek is smarter than Google AI on this.
There goes a wasted day. This was going to be a killer. Assign a function to automatically free the memory when you declare the variable. I spent a lot of time with Google AI to get a nice simple way to do this. I was giving all kinds of different code, but it didn't seem to work. Finally, I asked DeepSeek. Will work on Linux, broken on Windows.
  •  

Theo Gottwald

@Stan Duraham
QuoteAssign a function to automatically free the memory when you declare the variable

You want a constructor and destructor for variables?
Switch to O2 compiler, at least for UDT's maybe charles has it already?

I am also planning something like that, its a #HOOK - Directive (may be intergrated in some compiler already) that will be called whenever a Variable is going to be changed or a datatype is going to be used.

If you need it to define own datatypes like HLIB, we would need more support possibly, which will have to be designed like a custom VARIANT Type with own code for all cases from initialization to Parameter overloading (+/-* usw.) etc.
THis sounds interesting for the future but not right now.