RE: how to implement routines that return general strings?

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

 



On 8/11/06, Glynn Clements <glynn@xxxxxxxxxxxxxxxxxx> wrote:

> Avoid the second form. Apart from not being re-entrant, it can lead to
> bugs which can be hard to find, particularly if the function in
> question is relatively general-purpose and may be used from various
> places in your code.

char*
func()
{
	static buf[128] ;	// or whatever size is appropriate
	... copy result into buf ...
	return buf ;
}

It isn't re-entrant, because of the static buffer. In a process, all threads
will get the same copy of the static buffer, so if one thread calls the
function and hasn't copied the string contents before another threads calls
the function, they will both get the string from the 2nd call. If both
threads are inside that function at the same time, they are both going to be
playing with that buf[] at the same time. Icky.

Shorty

-
: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Assembler]     [Git]     [Kernel List]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [C Programming]     [Yosemite Campsites]     [Yosemite News]     [GCC Help]

  Powered by Linux