ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
check the length of the string and how many iterations of the while loop occur.
Do you have a C programming reference book as you seem to be having a few problems with pointers at the moment.
The SIGBUS occur when executing:
*dst++ = *src; // copy
I don't know why.
dst is a copy of a pointer (src) to some char.
*dst dereferences dst so that it is saying 'the actual memory that dst is pointing to'. Without the * dst is just 'the memory address of a char'. At the beginning of your routine you have:
char *dst = src;
Then later:
*dst++ = *src;
That says 'put whats at src (which can be the same exact location as dst) into dst'. This causes a memory error. Better understanding of pointers and memory locations will help you.
I always say the best book to understand using pointers in the C language is the one written by its creator: Kernighan & Ritche's 'The C Programmming Language'.
Hope this helps.
P.S. This is one of the reasons people shy away from C in certain situations. C doesn't hold your hand. As you found you, your code compiled just fine, but then bombed out with a memory error.
Welcome to the world of dynamic memory allocation.
I put it all in a main function since it was a one off specific example. I hope this helps, but again I must recommend a good book like The C Programming Language to truly get going on the subject. I just wanted to help get you going.
Also, this does not perform explicit error checking, which you should always do. For instance:
Last edited by turbo_spool; 10-22-2007 at 08:24 PM.
Reason: Wow, made a memory error typo myself when copying over here from xemacs. Good thing I checked it :p
The C Programming Language, 270 pages,
by Brian W. Kernighan and Dennis M.Richie, 2nd Edition,
Published by Prentice Hall
ISBN 0-13-110362-8 (paperback)
ISBN 0-13-110370-9
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.