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

If you allocated memory for 512 byte filenames then you must copy the
string into the array at the right address, something like:

/* Note that the 512th byte must be '\0', to ensure null terminated results. */
strncpy (&array[0][0], "a.txt", 512 - 1);
strncpy (&array[0][1], "b.txt", 512 - 1);

My advice is:
    o Before you do anything else, learn your pointer arithmetic
    o Once you know your pointer arithmetic, dont bother allocating
      your array by hand and doing the math by hand, just use GArray;
      at least you will know what is going on under the hood of GArray
      (which is the most important part).

Cheers,
                 -Tristan



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