Hello friends,
Please refer to the question
http://www.gtkforums.com/viewtopic.php?f=3&t=178532&p=196942#p196942.
As commented by errol, the seg fault is caused by
"Your error is in the use of strcat() in gs_open() and how you declared buffer.
If you declared buffer as
Code:
char *buffer = "";
Your use of strcat() would case a buffer overflow. The initial buffer is only 1 byte in size and that contains the '\0' terminator. Also if buffer has been used before the contents or even the pointer could also be mangled giving you more problems."
can I get some insight?
"" is an empty string literal. Since C strings are null terminated, it
implicitly contains one null character. The use of a string literal
causes the creation of an unnamed array long enough to contain the
string. The string literal itself is converted into a pointer to the
first element of the unnamed array, and the value of that pointer is
stored in buffer. In other words, the above code is functionally
equivalent to:
char unnamed[1] = {'\0'};
char *buffer = &unnamed[0];
When you call strcat(buffer, gs_text), it tries to catenate the string
pointed at by gs_text onto the end of the string contained in the array
pointed at by buffer. If gs_text is also an empty string, this is fine;
the catenation of two empty strings is an empty string, which needs only
a terminating null character. However, if gs_text isn't empty, which is
presumably the normal case, then it will contain at least two
characters: the first character of that string and the terminating null
character. The array pointed at by buffer isn't big enough to hold that
many characters, and bad things can happen (and apparently did happen)
when strcat() attempts to put more characters into that array than there
is room for. The behavior is undefined in that case, so in principle
just about anything could happen. In your case, strcat() tried to write
into the locations in memory that come after the unnamed array. At least
part of that memory didn't belong to your program, so your attempt to
write to that location triggered a segment violation which aborted your
program. Your operating system works on a policy that says that a
program which attempts to write to memory that it doesn't have a right
to is too dangerous to be allowed to continue running.
If the memory after the end of the array does belong to your program ,
then other kinds of bad behavior are possible. The write will, in
general, cause whatever variables your program is storing in that
location to change their value, causing unpredictable errors in the way
your program behaves. DONT DO THIS.
Always make sure that the first pointer that you pass to strcat() points
to an array that is long enough to store the entire length of the
catenated strings, including the terminating null character.