Re: Memory allocation using g_malloc

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.


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