Re: Is this OK?
- From: Scott Barron <sb125499 ohiou edu>
- To: gtk-app-devel-list <gtk-app-devel-list gnome org>
- Subject: Re: Is this OK?
- Date: Sun, 13 Oct 2002 18:39:36 -0400
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]