[H-GEN] malloc() causing a segfault

Anthony Towns aj at azure.humbug.org.au
Thu Aug 31 00:19:19 EDT 2000


On Thu, Aug 31, 2000 at 02:03:46PM +1000, Andrae Muys wrote:
> void stripLeadingSpacesInPlace(char *str) {
> 	char *in, *out;
> 	in = str; 
> 	out = str;
> 	while (*in) {
> 		if (!isspace(*in)) {
> 			*out++ = *in++;
> 		} else {
> 			in++;
> 		}
> 	}
> 	*out = '\0';
> }

Erm, that'll strip *all* spaces, not just leading ones. I assume that's
not what's wanted.

void strip_leading_spaces(char *str) {
    char *pch = str;
    while (isspace(*pch)) pch++;       /* \0 isn't a space */
    memmove(str, pch, strlen(pch)+1);
}

(Using inbuilt functions like memmove() and memcpy() should generally
be much more efficient than doing it by hand since those functions have
probably already been optimised for the local architecture's wordsize.)

Cheers,
aj

-- 
Anthony Towns <aj at humbug.org.au> <http://azure.humbug.org.au/~aj/>
I don't speak for anyone save myself. GPG signed mail preferred.

  ``We reject: kings, presidents, and voting.
                 We believe in: rough consensus and working code.''
                                      -- Dave Clark
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 350 bytes
Desc: not available
URL: <http://lists.humbug.org.au/pipermail/general/attachments/20000831/abacbda3/attachment.sig>


More information about the General mailing list