[H-GEN] malloc() causing a segfault

Andrae Muys a.muys at mailbox.uq.edu.au
Thu Aug 31 00:03:46 EDT 2000


[ Humbug *General* list - semi-serious discussions about Humbug and ]
[ Unix-related topics.  Please observe the list's charter.          ]

On Thu, 31 Aug 2000, Nikolai Lusan wrote:

> basically it strips lead spaces from a line of text, yes I know is clumsy but
> part of it was getting me back to pointers :)
> 
Can't see anything wrong with the function itself (except that it's very
clumsy).  So the memory corruption will be outside the function.

A better implementation would be to to the stripping in place and dodge
the two copies, and leave it more idiomatic and easier to read.

void stripLeadingSpacesInPlace(char *str) {
	char *in, *out;

	in = str; 
	out = str;

	while (*in) {
		if (!isspace(*in)) {
			*out++ = *in++;
		} else {
			in++;
		}
	}
	*out = '\0';
}

Yes I know the function name is probably too long, but I've been
programming Java for the past 5 months so I'm corrupted.

Andrae

--
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Andrae Muys <andrae at humbug.org.au> "Never ascribe to malice that which is
Shortech International Ltd          adequately explained by incompetence."
                                                     -Napoleon Bonaparte


--
* This is list (humbug) general handled by majordomo at lists.humbug.org.au .
* Postings to this list are only accepted from subscribed addresses of
* lists 'general' or 'general-post'.



More information about the General mailing list