0
typedef struct
{
   int age;
   int number;
} person;

static person self;//global declaration 

person *Self(){
    return &self;
}

I have been trying to understand the above similiar code which is used in a large code base any help would be appreciated.

  1. Why do we need such kind of function like above Self() function in C ?
  2. What are the benefits ?
  3. Is there any drawbacks ?
3
  • There's really no good way to answer your first question without knowing the use-case. It could be a way to implement the singleton pattern. Or it could be a way to have some global data without cluttering up the global namespace. Or something else completely. Commented Apr 12, 2022 at 10:13
  • 1
    This is a common pattern. Global mutable data is considered a true anti-pattern. So global mutable variables are declared static and accessed through accessor functions. In this form there's practically no benefit except in some tools it may be easier to trace all accesses to the the data. Commented Apr 12, 2022 at 10:15
  • @Someprogrammerdude The use case "passing self object" to others functions like some kind of initialization, can you please look around in line 77 here Commented Apr 12, 2022 at 10:24

2 Answers 2

5

This specific snippet doesn't make much sense.

static can be used as "poor man's private encapsulation", if an object is guaranteed to be a "singleton" - that is, only one instance exists and it is not accessed by multiple threads.

In this case the scope of the variable is reduced to the local .c file by static. And Self acts as a "getter" function. (As a singleton in a multi-threaded environment, you could place mutex/semaphores in the setter/getter functions, so that would be another use for them.)

However, while such a design might make sense in some cases, returning a non-const qualifier pointer to a private variable is just as bad design as skipping private encapsulation and using globals/file scope variables.

To answer your questions based on the above:

  1. Why do we need such kind of function like above Self() function in C ?

If it returned a const qualified pointer then it could make sense as a getter function. As it stands, it's just bloat and bad design though.

  1. What are the benefits ?
  2. Is there any drawbacks ?

As the code stands, no benefits, only drawbacks. It looks like a botched attempt to achieve private encapsulation.


To do proper, multi-instance private encapsulation in C, you wouldn't use static but so-called "opaque types". Here is an example: How to do private encapsulation in C?

Sign up to request clarification or add additional context in comments.

3 Comments

To obtain polymorphism in C those are required ? Or Different types of functionality acting on same self object ? e.g:- Like interface in other language
And Self() is not used with const qualifier @Lundin
@mohammedyaqub If you mean opaque types, yes they can help with polymorphism, if you include an instance of the parent as first member. You can also do it in other ways: function pointer tables, building up manual "vtables" etc.
-1

This a way to have a global variable without using an actual global variable. This is certainly a pattern to avoid because now you can change the value of the struct from anywhere is this can lead to various bug hard to detect.

A cleaner way would be to return a copy of the struct or a pointer on a const variable to avoid being able to modify the variable from outside the module. If you need to modify the struct, create set/reset functions.

person Copy_of_self() {
    return self;
}

const person *Self() {
    return (const person *)&self;
}

void Set_self(int age, int number) {
    // Here you could add some checking on input values
    self.age = age;
    self.number = number;
}

2 Comments

Why the unneeded cast in return (const person *)&self;?
Cleaner: use copy = self; instead of memcpy(&copy, &self, sizeof(copy)); or even person Copy_of_self() { return self; }.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.