|  | doubt on char * |  | |
| | | Fastro |  |
| Posted: Mon Jun 09, 2008 3:49 pm Post subject: doubt on char * |  |
int myfun(char *sourcebuf) { char *destbuf; char ch; while (*sourcebuf != '\0') { *destbuf++=*sourcebuf++; --> crashed here }; return 1; }
Can any one explain why it is crashed? Thankyou... |
| |
| | | Eric Sosman |  |
| Posted: Mon Jun 09, 2008 3:49 pm Post subject: Re: doubt on char * |  |
Fastro wrote:
| Quote: | int myfun(char *sourcebuf) { char *destbuf; char ch; while (*sourcebuf != '\0') { *destbuf++=*sourcebuf++; --> crashed here }; return 1; }
Can any one explain why it is crashed? Thankyou...
|
It may not look like it, but this is Question 7.1 in the comp.lang.c Frequently Asked Questions (FAQ) list at <http://www.c-faq.com/>.
-- Eric.Sosman@sun.com |
| |
| | | Kenny McCormack |  |
| Posted: Mon Jun 09, 2008 4:06 pm Post subject: Re: doubt on char * |  |
In article <9276334d-4bc0-4255-bfe6-7b8b718d1ae7@y22g2000prd.googlegroups.com>, Fastro <lencastro@gmail.com> wrote:
| Quote: | int myfun(char *sourcebuf) { char *destbuf; char ch; while (*sourcebuf != '\0') { *destbuf++=*sourcebuf++; --> crashed here }; return 1; }
Can any one explain why it is crashed? Thankyou...
|
It may be a bug in your editing software.
When you wrote:
int myfun(char *sourcebuf, char *destbuf)
It came out as:
int myfun(char *sourcebuf) { char *destbuf;
And, unfortunately, the compiler couldn't fix this by itself. |
| |
| | | CBFalconer |  |
| Posted: Mon Jun 09, 2008 7:07 pm Post subject: Re: doubt on char * |  |
Fastro wrote: ** code edited slightly **
| Quote: | int myfun(char *sourcebuf) { char *destbuf; char ch; while (*sourcebuf != '\0') { *destbuf++=*sourcebuf++; --> crashed here }; return 1; }
Can any one explain why it is crashed?
|
Yes. destbuf is uninitialized. It doesn't point to any storage.
-- [mail]: Chuck F (cbfalconer at maineline dot net) [page]: <http://cbfalconer.home.att.net> Try the download section.
** Posted from LINK ** |
| |
| | | rahul |  |
| Posted: Tue Jun 10, 2008 4:32 am Post subject: Re: doubt on char * |  |
On Jun 9, 9:06 pm, gaze...@xmission.xmission.com (Kenny McCormack) wrote:
| Quote: | In article <9276334d-4bc0-4255-bfe6-7b8b718d1...@y22g2000prd.googlegroups.com>,
Fastro <lencas...@gmail.com> wrote: int myfun(char *sourcebuf) { char *destbuf; char ch; while (*sourcebuf != '\0') { *destbuf++=*sourcebuf++; --> crashed here }; return 1; }
Can any one explain why it is crashed? Thankyou...
It may be a bug in your editing software.
When you wrote:
int myfun(char *sourcebuf, char *destbuf)
It came out as:
int myfun(char *sourcebuf) { char *destbuf;
And, unfortunately, the compiler couldn't fix this by itself.
|
Its either this or Fastro really forgot to assign memory to destbuf and dereferenced god knows what. |
| |
| | | Chris Dollin |  |
| Posted: Tue Jun 10, 2008 10:15 am Post subject: Re: doubt on char * |  |
rams wrote:
| Quote: | yes ,allocate memory for destbuf using malloc() &strlen() like destbuf=(char *)malloc(1+ strlen(sourcebuf)*sizeof(char) );
|
(a) there's no need to, and good reasons for not, cast the `malloc` result to `char*`.
(b) `sizeof char` is 1 by definition.
char *destbuf = malloc( 1 + strlen( sourcebuf ) );
is, I think, clearer and safer. (I'd write `+ 1` rather than `1 +`, but I can see the point of marking the one-more-than in this way.)
-- "Shopping." /Barrayar/
Hewlett-Packard Limited registered no: registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England |
| |
| | | santosh |  |
| Posted: Tue Jun 10, 2008 10:17 am Post subject: Re: doubt on char * |  |
rahul wrote:
| Quote: | On Jun 9, 9:06 pm, gaze...@xmission.xmission.com (Kenny McCormack) wrote: In article 9276334d-4bc0-4255-bfe6-7b8b718d1...@y22g2000prd.googlegroups.com>,
Fastro <lencas...@gmail.com> wrote: int myfun(char *sourcebuf) { char *destbuf; char ch; while (*sourcebuf != '\0') { *destbuf++=*sourcebuf++; --> crashed here }; return 1; }
Can any one explain why it is crashed? Thankyou...
It may be a bug in your editing software.
When you wrote:
int myfun(char *sourcebuf, char *destbuf)
It came out as:
int myfun(char *sourcebuf) { char *destbuf;
And, unfortunately, the compiler couldn't fix this by itself.
Its either this or Fastro really forgot to assign memory to destbuf and dereferenced god knows what.
|
Er, Kenny was being his usual sarcastic self. It's extremely unlikely to be a bug in the editor. It's more likely a bug in the OP.  |
| |
| | | rams |  |
| Posted: Tue Jun 10, 2008 11:55 am Post subject: Re: doubt on char * |  |
| |  | |
On Jun 10, 9:32 am, rahul <rahulsin...@gmail.com> wrote:
| Quote: | On Jun 9, 9:06 pm, gaze...@xmission.xmission.com (Kenny McCormack) wrote:
In article <9276334d-4bc0-4255-bfe6-7b8b718d1...@y22g2000prd.googlegroups.com>,
Fastro <lencas...@gmail.com> wrote: int myfun(char *sourcebuf) { char *destbuf; char ch; while (*sourcebuf != '\0') { *destbuf++=*sourcebuf++; --> crashed here }; return 1; }
Can any one explain why it is crashed? Thankyou...
It may be a bug in your editing software.
When you wrote:
int myfun(char *sourcebuf, char *destbuf)
It came out as:
int myfun(char *sourcebuf) { char *destbuf;
And, unfortunately, the compiler couldn't fix this by itself.
Its either this or Fastro really forgot to assign memory to destbuf and dereferenced god knows what.
|
yes ,allocate memory for destbuf using malloc() &strlen() like destbuf=(char *)malloc(1+ strlen(sourcebuf)*sizeof(char) ); and finally after coming out of while loop make sure to NULL the last character of destbuf i.e .., *destbuf='\0'; and at last don't forget to free the memory |
| |
| | | Joachim Schmitz |  |
| Posted: Tue Jun 10, 2008 12:55 pm Post subject: Re: doubt on char * |  |
CBFalconer wrote:
| Quote: | Fastro wrote: ** code edited slightly **
int myfun(char *sourcebuf) { char *destbuf; char ch; while (*sourcebuf != '\0') { *destbuf++=*sourcebuf++; --> crashed here }; return 1; }
Can any one explain why it is crashed?
Yes. destbuf is uninitialized. It doesn't point to any storage. A step further: because it is uninitialized, it'll contain some garbage |
value, that, if interpreted as a pointer, will try to access some random memory location. If you're lucky, you don't own that and the OS will carsh the program, if you're unlucky, you'll own it and accessing it will corrupt some data structuturs and you'll only notice the harm it did much later and have major difficulties to find were it went wrong. So be hapy it crashed on you right away...
Bye, Jojo |
| |
| | | Richar |  |
| Posted: Tue Jun 10, 2008 1:03 pm Post subject: Re: doubt on char * |  |
| |  | |
"Joachim Schmitz" <nospam.jojo@schmitz-digital.de> writes:
| Quote: | CBFalconer wrote: Fastro wrote: ** code edited slightly **
int myfun(char *sourcebuf) { char *destbuf; char ch; while (*sourcebuf != '\0') { *destbuf++=*sourcebuf++; --> crashed here }; return 1; }
Can any one explain why it is crashed?
Yes. destbuf is uninitialized. It doesn't point to any storage. A step further: because it is uninitialized, it'll contain some garbage value, that, if interpreted as a pointer, will try to access some random memory location. If you're lucky, you don't own that and the OS will carsh the program, if you're unlucky, you'll own it and accessing it will corrupt some data structuturs and you'll only notice the harm it did much later and have major difficulties to find were it went wrong. So be hapy it crashed on you right away...
Bye, Jojo
|
Look up using something like splint. Also consider using a debugger. Both will make it very clear where the error is.
LINK
I use this all the time and have bound it to me emacs IDE so any emacs users out there, here you are:
,---- | (defun do-lint() | (interactive) | (set (make-local-variable 'compile-command) | (let ((file (file-name-nondirectory buffer-file-name))) | (format "%s %s %s" | "splint" | "+single-include -strict -compdef -nullpass -preproc +matchanyintegral -internalglobs -I/usr/include/gtk-2.0/ -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include -I/usr/include/cairo/ -I/usr/include/pangomm-1.4/pangomm/" | file | ))) | (message compile-command) | (compile compile-command) | ) `---- |
| |
| Page 1 of 3 .:. Goto page 1, 2, 3 Next | |
|
|