Kayo Hisatomi <[EMAIL PROTECTED]> wrote: Hi Nico,

>> 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.

I thought on this too, but I could not think in another area than the
.text section...

This is a good question: are the static strings allocated in the 
.text area?

Regards,
Kayo.


----- Original Message ----
From: Nico Heinze 
To: [email protected]
Sent: Wednesday, April 11, 2007 2:15:11 PM
Subject: [c-prog] Re: Segmentation fault in a simple function.

--- In [email protected], Kayo Hisatomi  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.


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.

If we use char* p="  hi  ", p could be stored in read-only mem.
Is it the case even if we use char[] p = "  hi  " ?

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.

Why? It can be an inout parameter. But this should be clearly mentioned at the 
function definition beginning so callers know what they are getting into. And 
then there no need for a return value as the input is modified _in place_.

Regards,
Nico



To unsubscribe, send a blank message to . 
Yahoo! Groups Links








       
____________________________________________________________________________________
Expecting? Get great news right away with email Auto-Check. 
Try the Yahoo! Mail Beta.
http://advision.webevents.yahoo.com/mailbeta/newmail_tools.html 


To unsubscribe, send a blank message to . 
Yahoo! Groups Links







       
---------------------------------
We won't tell. Get more on shows you hate to love
(and love to hate): Yahoo! TV's Guilty Pleasures list.

[Non-text portions of this message have been removed]

Reply via email to