English 中文(简体)
C - how to check whether all required fields of a structure are filled? (most elegant method)
原标题:

I have a function, which has a pointer to some structure as its argument. How can I check within this function, whether all required fields of the structure have been filled before a function call?

example:

//lib.c
void f(X_type *x)
{
  ...
}

//user.c
main(){
X_type object;
object.name = "I am X";
object.ID = 1;
...
f(X_type &object);
}
问题回答

Create an initialiser function that takes all the fields as arguments (as if it were an object constructor) and use it instead of setting the fields one by one.

Of course, that won t prevent anybody to keep it doing the wrong way, but if you maintain the discipline of using that function, it will be much more difficult to leave some field uninitialised without noticing. And if you change the fields of the structure (and the function accordingly), the compiler will complain about the mismatching arguments wherever you forgot to update.

You can go a little bit further (but not much more with just plain C) doing some tricks with the preprocessor and the includes to enforce the data encapsulation, but it s somewhat cumbersome. Any book on OOP with C will help you with that issue, but I don t know if the gain is worth the effort.

Is valgrind available on your system? If you run your program through it, it will automatically detect the use of any uninitialized variables. It can also catch quite a few other problems (such as using memory that s already been free d). This won t then become a permanent feature of your program, but it s very nice for debugging.

I would combine this with the asserts mentioned earlier and increased compiler warnings. That s about all you can do—the problem is there s no difference between initialized and uninitialized memory. To the machine, they are all just values.

Use assertions (from assert.h) to check the prerequisites needed by your function. It is up to you to define what is a valid or invalid value for a field of the structure.

example:

assert( NULL != object);
assert( NULL != object->name);

Add another member to the structure to indicate if the rest of the members are filled (and initialize it to "false"). Then just check that new member.

struct whatever {
  int datagood;
  /* more members; */
};

struct whatever bar;
bar.datagood = 0;

foo(&bar); /* foo() will see datagood == 0 */

/* bar.a = 42; */
/* bar.b = 24; */
bar.datagood = 1;

foo(&bar); /* foo() will see datagood == 1 */

I do not think there is a solution in C itself. As others suggested, you may use valgrind, but it is another story. I always memset() the struct:

memset(&object, 0, sizeof(struct mystruct));

or

struct mystruct *p = calloc(1, sizeof(struct mystruct));

Then you can check the members against 0. I believe this is a good habit.

By static analysis. Parasoft C++test has BD-PB-NOTINIT (Avoid usage of not initialized data) flowanalysis rule for that.

Your best bet is to write a function that validates the contents of the struct manually and returns a True/False (however you define it), this way you can easily use the method wherever you utilize this struct.

This is usually solved by careful programming, but if you have to be sure, here s an overkill idea:

  • Add a checksum/crc field.
  • Add an update function of members that also modifies the checksum field.
  • Upon using, use another function to compare the checksum of the fields with that of the checksum field.




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签