--- In [email protected], Kayo Hisatomi <[EMAIL PROTECTED]> wrote:
>
> Hi Roberto,
> 
> The problem is in this line in the main() function:
>    char *s0 = "   bye 1   ";
> 
> You are not allocating memory for the string, but just for
> the pointer. So, try this:
>    char s0[] = "   bye 1   ";
> 
> And the program will work.
> 
> Regards,
> Kayo.
<snip>

As far as I know this is not necessarily correct. It might well happen
that the string "  bye 1  " is stored in a read-only part of memory
during process initialisation.

A better way to perform the ltrim() operation is to allocate some
memory within the ltrim() function dynamically, namely just as much as
is needed to store the trimmed string (plus trailing '\0' byte).
However, this of course has the disadvantage that this piece of memory
has to be deallocated later on, e.g. using delete[] or free().
On the other hand this has the advantage that you will never mess
around with memory belonging to some other part of your application;
in your ltrim() function you change the memory that the parameter
points to, and this is really bad practice.

Regards,
Nico

Reply via email to