Terminology for initializing C structures

This will be a simple question, but a googling search doesn't seem to give me an answer. As I understand this in C, we have two ways to initialize the foo object when foo is a structure. Take a look at the code below for an example.

typedef struct foo { int var1; int var2; char* var3; }foo; //initializes and allocates a foo foo* foo_init1(int v1,int v2,const char* v3) { if(..some checks..) return 0; foo* ret = malloc(sizeof(foo)); ret->var1 = v1; ret->var2 = v2; ret-var3 = malloc(strlen(v3)+1); strcpy(ret->var3,v3); return ret; } // initializes foo by ... what? How do we call this method of initialization? char foo_init2(foo* ret,int v1,int v2, const char* v3) { //do some checks and return false if(...some checks..) return false//;assume you defined true and false in a header file as 1 and 0 respectively ret->var1 = v1; ret->var1 = v1; ret->var2 = v2; ret-var3 = malloc(strlen(v3)+1); strcpy(ret->var3,v3); return true; } 

So my question is this. How do we refer to C for these various initialization methods? The first returns an initialized pointer to foo, so it's easy to use if you want the foo object on the heap to look like this:

 foo* f1 = foo_init1(10,20,"hello"); 

But the second requires foo .. what? Take a look at the code below.

 foo f1; foo_init2(&f1,10,20,"hello"); 

Thus, the second method simplifies the initialization of the object on the stack, but what do you call it? This is basically my question of how to access the second initialization method.

The first selects and initializes a pointer to foo. The second initializes foo ... what? Link

As a bonus question, how do you guys work when coding in C? Do you determine the use of the object you are creating and determine whether there should be an initialization function of type 1 or 2, or even both of them?

+7
source share
2 answers

Since none of the people in the comments accepted the proposal to include their comments in the answer, I have to answer my question.

Well, in principle, a possible answer would be that since the AI ​​states that there is no concrete naming convention. Of course, any name method is used, it should be:

  • Agreed for all projects / s for clarity.
  • Recognized by other programmers as a function that does what it actually does.

To achieve this, wonderful recommendations were made in the comments. If the object is foo:

  • Passed for initialization inside the function: foo_init
  • It is allocated inside the function and the pointer is returned: foo_alloc, foo_make, foo_new

All of the above is clear, I suppose, but what most accurately describes what happens in the functions will be foo_init and foo_alloc.

Personally, I really don't like the _alloc solution, because I don’t like the way it looks in my code, so I decided to add the verb _create instead of highlighting after the function to indicate what it does.

But it’s good that the answer comes down to what I think is a personal preference. Everything should be good and acceptable if the functionality of the function becomes clear by reading its name.

+1
source

I'm not sure if there is a specific nomenclature for the two methods,
In the first method, the function dynamically distributes the structure and assigns values ​​to the members,
whereas in the second, the structure is distributed before the function, and the function then simply assigns values ​​to the members.

Do you determine the use of the object you are creating and determine whether there should be an initialization function of type 1 or 2, or even both of them?

The choice of the first or second method depends on an important difference:
The first method is preferable when you need to transfer the returned structure by regions, the memory on the heap should be explicitly freed until the data prevails during the second method, the data on the stack is deleted after the completion of the region of the transferred object.

+2
source

All Articles