Re: Memory allocation using g_malloc



3saul wrote:
Thanks for the response. Let me elaborate a little. I have a list of files in
a dir (without knowing how many)

a.txt
b.txt
c.txt

I want to be able to put the names of the files into an array so that I can
refer to them later like this

array[0][0] = a.txt
array[0][1] = b.txt

and so on...Perhaps there is a better way to go about this but this seems as
good a way as any to me.
Thanks for your help so far...
--
View this message in context: http://www.nabble.com/Memory+allocation+using+g_malloc-t1708289.html#a4638715
Sent from the Gtk+ - Apps Dev forum at Nabble.com.

_______________________________________________
gtk-app-devel-list mailing list
gtk-app-devel-list gnome org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list

Just my 2 cents although they might be on another currency... How about using a GSList (gtk single list) with GStrings??? So you won't have to worry about the size of neither the list or of any array... If you still want to use an array I am sure I have read somewhere in my textbook that there is a dynamic way of defining it (or declare it... never understood the difference). If I recall even better you have to scan the dir once to get the number of elements, declare your array, rescan to get the actual contents and then you can do even more dynamic mallocation if you use another gchar[] to hold each of the values... OK, it does not even make sense to me... in pseudocode:

gint filesNum=0;
scan directory and count the files in filesNum;
gchar *array[filesNum];
gchar buf[512]; //if you really "hate" GString
gint i=0;

while (!end of files in the dir)
{
   read filename to buf;
*array[i] = (gint) malloc(sizeof(gchar)*length_of_str(buf)); //this probably is wrong, but I am sure you can figure it out with a good textbook or some examples on *argv[]
   strcpy(*array[i], buf);
}


BTW someone posted:
gchar *myArray = g_malloc (sizeof (gchar) * 20 * 512);

which is only a string and not an array of strings so what you basically do is just define a 20 times your default string length character long string...

Having said all that I still think that a GSList* of GStrings* would be much easier...



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