g_assert() is used for debugging. It's typically used in areas where an expression SHOULD evaluate to true, and if it's not, then it throws out an error to the console. So, if you see this:
Code:
g_assert (value == NULL)
Then the developer is expecting "value == NULL" to be a true expression. If value is not null, then an error message will be seen. You will see this used OFTEN throughout the GTK+ and the GLib libraries. Think of it as us, the programmers, telling our code that we "assert" the following expression to be true, and to let us know otherwise.
Try to call a gtk_widget_xxx function on a widget which has not been initialized yet and before your program crashes (or just doesn't work), you'll see some assertion failure errors. Without them, all you would know is that your program isn't working. But now, you know exactly why.
When your program is ready to release, hopefully you aren't seeing any assertion errors!
So let's look at your snippet:
Code:
g_file_get_contents ("foo.txt", &contents, NULL, &err);
g_assert ((contents == NULL && err != NULL) || (contents != NULL && err == NULL));
At this point, we expect one of two conditions, contents is NULL and error is set, or contents is set and error is NULL. Otherwise, something we hadn't predicted has happened. So we're not expecting that assertion to output any error messages.
Same thing here:
Code:
g_assert (contents == NULL);
fprintf (stderr, "Unable to read file: %s\n", err->message);
g_error_free (err);
When we get into the error handling block, we expect the contents to be NULL because there was an error. That's why we aren't freeing the contents. But if for some reason, it isn't NULL, we're going to spit out an error message about that freakish condition.
Now, This is a bit redundant if you ask me, because this was already checked in the previous assertion. But that's what it's doing.
So in short, removing assertions do not change how the code runs per say, but they assist in debugging critical error conditions--conditions in which the developer intends to never happen.