Re: Is this OK?



Hi,

I'm a huge fan of using the g_new family, so I might do it like this:

KStudent*
k_student_new ()
{
        KStudent *student = g_new0(KStudent, 1); 
        student->profile = g_new0(KEducationProfile, 1);
        student->father = g_new0(KParent, 1); 
        student->mother = g_new0(KParent, 1);
        student->foster = g_new0(KParent, 1);
        return student;
}

g_new0() is a macro to allocate n structures (the 1 in this case) of a
type and zero it out.  You can free the structure just as you said.
Your code would work but is a little less concise (ie setting a pointer
to NULL and immediately assinging to it with g_malloc()).  I try to make
glib do as much as it can because it has some wonderful features that
aid in writing clean, readable code.  Check out the manual section about
its memory allocation features.

(If there are any reasons to *not* do it this way, please point them out
to me :)

-Scott



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]