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 <[EMAIL PROTECTED]> 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 <[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 To unsubscribe, send a blank message to <mailto:[EMAIL PROTECTED]>. 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 <mailto:[EMAIL PROTECTED]>. Yahoo! Groups Links <*> To visit your group on the web, go to: http://groups.yahoo.com/group/c-prog/ <*> Your email settings: Individual Email | Traditional <*> To change settings online go to: http://groups.yahoo.com/group/c-prog/join (Yahoo! ID required) <*> To change settings via email: mailto:[EMAIL PROTECTED] mailto:[EMAIL PROTECTED] <*> To unsubscribe from this group, send an email to: [EMAIL PROTECTED] <*> Your use of Yahoo! Groups is subject to: http://docs.yahoo.com/info/terms/
