I'm hoping that the C samples/tutorials I've been working on will be able to benefit newcomers as well as seasoned programmers by giving them that practice.
It has been more than a month since the start of my internship (wow, time flies!). With every sample, comes a new challenge, and something new is always learned. One instance that immediately comes to mind is when, a while back when I worked on the button.c sample, there was an issue involving object ownership. Specifically within this section of code:
const char *old_label;
char *new_label;
old_label = gtk_button_get_label (button);
new_label = g_utf8_strreverse (old_label, -1);
gtk_button_set_label (button, new_label);In this case I created a new char variable named "new_label" in which belongs to me, not the button I had created, and essentially duplicated the button's label and then proceeded to reverse it/replace it. Making sure to "free" the memory afterwards (since the function g_utf8_strreverse returns newly allocated memory). Originally I hadn't created a new label, and had somehow modified the label belonging to the button, in which put my program in risk of crashing. There is always a question of who should "own" a return value of a function in C-based gtk programming, and it is important to keep track of memory to avoid crashes. As Ryan (desrt) put it when he first introduced me to the concept, the rules are as follows (more or less):
g_free (new_label);
- When passing something to a function as an argument, you almost always have to retain ownership of it, it's very rare to have it taken from you.
- Return values from functions can go either way, either you will own the value returned or the function will continue to own it (and you can't modify it).
Furthermore, here is a good blog post (a random resource really) I found that helped explain most of this for me at the time.
I'm hoping to actually create a tutorial within C Beginner Tutorials on this concept, along with examples and lots of goodies. Let me know what you think! Of course suggestions are always welcome.
Now with my test last Monday out of the way, my next goal is to go into overdrive with more C samples! :) 18 down, 23 to go! (I also wish everyone to have a wonderful time at GUADEC!)

