Re: please excuse very basic questions: gchar and g_free()



On Tue, 2004-11-23 at 18:35, F. Kater wrote:
Hi,

I've got some basic questions about gchar:

What's actually the size of the array: gchar g[10]? 10 bytes (0-9) or 11
(0-11)?

gchar g[10] has 10 elements 0-9.


Is the last byte occupied by a 0 to terminate the array?

No, you must NULL terminate your character arrays.  Some functions will
create an array and null out all the elements (g_new0).

How would g_free() behave if I had created the array with the new
operator and would then overwrite the last byte (zero) with another
value?

g_free would deallocate the amount of memory allocated by the g_new
function (or g_malloc etc.)

Is it okay to do reinterpreting casts from gchar* to guint8* (and vice
versa) or do I loose the terminating 0 byte? 

You can cast anything to anything, it doesn't effect the memory layout
just how your program interprets the data.  It is dangerous to do if you
don't know what you are doing.  

Further, could a single
gchar be 2 bytes long in some contexts (maybe because of a two-byte
character table)?

There are specific functions to deal with utf8.  If you are dealing with
utf8 two gchars would represent one character I believe. 

Finally, how does g_free know from a dynamically created guint8 array
where is its end in memory?

When you allocate the memory the size you allocated is stored and used
to determine how much memory to free.

--
J5




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