Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C equivalent of Java static block (Run once at startup)

Tags:

c

static

I have found plenty of answers to this question in C++, but I am strictly interested in C.

I have a circular linked list in C, and I want to initialize it with a dummy header node before the first time it is accessed. The dummy header node is a statically allocated global variable. This is my current solution:

static once = 1;
if(once){
    once = 0;
    // Setup code
}

This works, but I have to put it in every function that uses this linked list. (Of course, I can put this code in its own function, so I only have to call that function in every other function, but this isn't much better) Is there a better way? For example, if I have the following struct:

struct node {
    int value;
    struct node *next;
}

Is there any way to initialize one of these structs as a literal, such that its next value points to itself?

By "Initialize as a literal", I mean this: (Excuse my probably incorrect terminology)

struct test {
    int i;
    double d;
};

struct test magic = {1, 2.3}; // This can just be done at the top of the c file, outside of any functions

int someFunction(){...}
like image 870
ItsTimmy Avatar asked Feb 10 '17 00:02

ItsTimmy


People also ask

Does static block get executed only once?

Unlike C++, Java supports a special block, called a static block (also called static clause) that can be used for static initialization of a class. This code inside the static block is executed only once: the first time the class is loaded into memory.

What is static block in C?

A static block implementation for C++ This is a single-header-file repository which allows for writing static blocks in C++. That is: Blocks of code, outside of any function, which can't be called from a function, and are executed once when the program starts (before main() ).

What is static initialization in C?

The default initialization value of a static variable is zero, even if it is not assigned, which is not the case in a local variable. · It is mandatory to initialize the static variable using the static keyword in C else it will return an error.

Which static block will execute first?

The static blocks always execute first before the main() method in Java because the compiler stores them in memory at the time of class loading and before the object creation. Here, the compiler executes all the static blocks first, and after finishing the static block execution, it invokes the main() method.


1 Answers

You can just do exactly what you say, and make it point to itself, if it's a file-scope variable:

#include <stdio.h>

struct node {
    int value;
    struct node * next;
};

struct node mylist = { 1, &mylist };

int main(void)
{
    printf("%d %d\n", mylist.value, mylist.next->value);
    return 0;
}

with output:

paul@horus:~/src/sandbox$ ./list
1 1
paul@horus:~/src/sandbox$ 

You can also, of course, add "run once at startup" code in main() before you do anything else, and achieve the same end:

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

struct node {
    int value;
    struct node * next;
};

struct node * mylist;

int main(void)
{
    mylist = malloc(sizeof *mylist);
    if ( !mylist ) {
        perror("couldn't allocate memory");
        return EXIT_FAILURE;
    }

    mylist->value = 1;
    mylist->next = mylist;

    /*  Rest of your program here  */

    return 0;
}
like image 186
Crowman Avatar answered Nov 05 '22 13:11

Crowman