Re: Memory allocation using g_malloc
- From: Tristan Van Berkom <tristan van berkom gmail com>
- To: 3saul <saul_lethbridge hotmail com>
- Cc: gtk-app-devel-list gnome org
- Subject: Re: Memory allocation using g_malloc
- Date: Tue, 30 May 2006 23:06:22 -0400
3saul wrote:
I need a little help with memory allocation. What I'm wanting to do is create
a multi dimensional array that contains a list of file names from a specific
directory. So it needs to be something like this
int numfiles = 20;
g_malloc (myArray [numfiles] [512]);
Is this close to correct? I'm getting a segfault
The above code is perfectly safe.
I am ofcourse assuming that "int myArray[][]" is already allocated and that
the integer value at: &myArray[20][512] holds the amount of memory you
want to allocate in bytes.
I would also recommend you trap the returned allocated memory segment :)
Note that if you are segfaulting; my assumptions are probably wrong and
you are probably looking for something more like:
/* g_malloc() is used exactly the same as good old malloc(), only it
uses the glib slab */
gchar *myArray = g_malloc (sizeof (gchar) * 20 * 512);
At this point; you want to break out that dusty C bible in the corner
and review
the pointer arithmetic section, otherwise you have more segfaults ahead.
Cheers,
-Tristan
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]