Re: garray beginner problem
- From: "David Necas (Yeti)" <yeti physics muni cz>
- To: Kim Adil <ksadil bigpond net au>
- Cc: gtk-app-devel-list gnome org
- Subject: Re: garray beginner problem
- Date: Sat, 28 May 2005 08:53:42 +0200
On Fri, May 27, 2005 at 11:50:44PM -0400, Kim Adil wrote:
I am trying to figure out how to use GArray containers. This gives me
rubbish in the printf output. I think it relates to how I am accessing
the data nce it is in the array rather than what is stored in the array.
Any comments and ideas would be appreciated.
It seem you'd better use GPtrArray to store the pointers,
instead of GArray that stores the full structures.
GArray *garray;
garray = g_array_new (FALSE, FALSE, sizeof (Machine));
for (i = 1; i < 10000; i++){
Machine *newMach = machine_new(i,"trd11","d11","joe");
g_array_append_val (garray, newMach);
Two problems here: You pass a *pointer* to Machine, not
a Machine, so a pointer is stored into garray. And newMach
is probably never freed.
You could copy the contents of newMach into the array,
provived it's a constant-size structure with
Machine *newMach = machine_new(i,"trd11","d11","joe");
Machine m = *newMach;
g_array_append_val(garray, m);
machine_free(newMach)
But this is so obviously awkward (and incorrect, if Machine
contains any dynamically allocated data) that you don't want
to do this. Instead, use GPtrArray:
GPtrArray *array;
...
Machine *newMach = machine_new(...);
g_ptr_array_add(array, newMach);
Yeti
--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]