Firstly, this
strings[0] = malloc(strAuth)
strings[0] = strAuth;
is defintely broken. What is the type of strAuth
? How did you manage to use strAuth
as argument of malloc
(i.e. size) and then immediately as the right-hand size of assignment? malloc
s argument must be an integer, while strings[0]
has pointer type. Aside from being completely self-contradictory, this sort of usage will trigger diagnostic messages from the compiler. Did you just ignore those messages?
If strAuth
is a string and if you are trying to allocate memory for a copy of strAuth
, then the typical memory allocation idiom would be
strings[0] = malloc(strlen(strAuth) + 1);
Secondly, why are you even attempting to assign anything to strings[0]
after malloc
? The pointer in strings[0]
is your only connection to the freshly allocated memory, which you should cherish and preserve by all means. Instead you immediately clobber that pointer by assigning a new value to strings[0]
, turning the memory you just allocated into a memory leak.
Again, if you are trying to create a copy of strAuth
in strings[0]
, then the typical idiom would be
strings[0] = malloc(strlen(strAuth) + 1);
strcpy(strings[0], strAuth);
(of course, in real code one should always remember to check whether malloc
succeeded or not).
On many platforms a non-standard strdup
function is available, which wraps exactly the above allocate-and-copy functionality, meaning that the above two lines can be replaced with simple
strings[0] = strdup(strAuth);
And finally, thirdly, what is g_free
? Are you sure it is applicable to memory allocated by standard malloc
(as opposed to, say, g_malloc
). And even if it happens to be applicable, it is still not a good idea to mix APIs like that. If you want to allocate memory by standard malloc
(or strdup
), then it is a good idea to stick to standard free
for freeing it.