473,668 Members | 2,577 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

doubt on char *

int myfun(char *sourcebuf)
{
char *destbuf;
char ch;
while (*sourcebuf != '\0')
{
*destbuf++=*sou rcebuf++; --crashed here
};
return 1;
}

Can any one explain why it is crashed?
Thankyou...
Jun 27 '08 #1
21 1393
Fastro wrote:
int myfun(char *sourcebuf)
{
char *destbuf;
char ch;
while (*sourcebuf != '\0')
{
*destbuf++=*sou rcebuf++; --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/>.

--
Er*********@sun .com
Jun 27 '08 #2
In article <92************ *************** *******@y22g200 0prd.googlegrou ps.com>,
Fastro <le*******@gmai l.comwrote:
>int myfun(char *sourcebuf)
{
char *destbuf;
char ch;
while (*sourcebuf != '\0')
{
*destbuf++=*sou rcebuf++; --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.

Jun 27 '08 #3
Fastro wrote: ** code edited slightly **
>
int myfun(char *sourcebuf) {
char *destbuf;
char ch;
while (*sourcebuf != '\0') {
*destbuf++=*sou rcebuf++; --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 http://www.teranews.com **
Jun 27 '08 #4
On Jun 9, 9:06 pm, gaze...@xmissio n.xmission.com (Kenny McCormack)
wrote:
In article <9276334d-4bc0-4255-bfe6-7b8b718d1...@y2 2g2000prd.googl egroups.com>,

Fastro <lencas...@gmai l.comwrote:
int myfun(char *sourcebuf)
{
char *destbuf;
char ch;
while (*sourcebuf != '\0')
{
*destbuf++=*sou rcebuf++; --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.
Jun 27 '08 #5
On Jun 10, 9:32*am, rahul <rahulsin...@gm ail.comwrote:
On Jun 9, 9:06 pm, gaze...@xmissio n.xmission.com (Kenny McCormack)
wrote:
In article <9276334d-4bc0-4255-bfe6-7b8b718d1...@y2 2g2000prd.googl egroups.com>,
Fastro *<lencas...@gma il.comwrote:
>int myfun(char *sourcebuf)
>{
* *char *destbuf;
* *char ch;
* *while (*sourcebuf != '\0')
* *{
* **destbuf++=*so urcebuf++; --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(sourcebu f)*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
Jun 27 '08 #6
rahul wrote:
On Jun 9, 9:06 pm, gaze...@xmissio n.xmission.com (Kenny McCormack)
wrote:
>In article
<9276334d-4bc0-4255-bfe6-7b8b718d1...@y2 2g2000prd.googl egroups.com>,

Fastro <lencas...@gmai l.comwrote:
>int myfun(char *sourcebuf)
{
char *destbuf;
char ch;
while (*sourcebuf != '\0')
{
*destbuf++=*sou rcebuf++; --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. :-)

Jun 27 '08 #7
rams wrote:
yes ,allocate memory for destbuf using malloc() &strlen()
like destbuf=(char *)malloc(1+ strlen(sourcebu f)*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

Jun 27 '08 #8
CBFalconer wrote:
Fastro wrote: ** code edited slightly **
>>
int myfun(char *sourcebuf) {
char *destbuf;
char ch;
while (*sourcebuf != '\0') {
*destbuf++=*sou rcebuf++; --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
Jun 27 '08 #9
"Joachim Schmitz" <no*********@sc hmitz-digital.dewrite s:
CBFalconer wrote:
>Fastro wrote: ** code edited slightly **
>>>
int myfun(char *sourcebuf) {
char *destbuf;
char ch;
while (*sourcebuf != '\0') {
*destbuf++=*sou rcebuf++; --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.

http://www.splint.org/

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 +matchanyintegr al -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)
| )
`----

Jun 27 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1995
by: Guilherme Pinto | last post by:
Hello. I am reading the book written by Bjarne Stroustrup called " The C++ Programming Language - Special Edition" and had a doubt which a think is really important to distinguish between the main features of modules, namespaces, and User-Defined types. The text above was copied from page 31. --------------------------------------------------------
1
1699
by: SK | last post by:
Hi all, I have a doubt in C++ Templates by Nicolai M. Josuttis. On Page 17 there is a line "In general, it is a good idea not to change more than necessary when overloading function templates. You should limit your changes to the number of parameters or to specifying template parameters explicitly." Can anyone explain the meaning of these lines to me?
9
4974
by: grid | last post by:
Hi, I have a function which takes a variable number of arguments.It then calls va_start macro to initialize the argument list.But here in my case I have a special builtin va_start provided by the compiler which I use for performance gains.Then the variable arguments are passed to another varargs function which then uses a builtin va_arg macro to get the arguments. Iam getting an error from the compiler in the called function when a local...
2
1901
by: priya | last post by:
Hi All, Currently I am working in Socket Progrm written in C. Here i pasted Both Server and client code.. Server.c code
77
3667
by: muttaa | last post by:
Hello all, My doubt is going to be so primitive that i ask you all to forgive me beforehand.... Here's the code snippet: int main() { int x=5;
38
2701
by: edu.mvk | last post by:
Hi I am using strcpy() in my code for copying a string to another string. i am using static char arrays. for the first time it is exected correctly but the second time the control reaches then the SEGMENTATION FAULT is occuring. Please tell me what are all the cases this problem may occur if we use strcpy().
6
2151
by: reji_thomas | last post by:
Hi, I have a doubt in the following code: struct xyz { int x; long l; float f; };
13
2113
by: deepak | last post by:
Hi In the following function how the memory 'll be allocated. 1) Will it allocate memory for all the char's together or allocate for first char. then for int then for float and after this only second char in the function gets memory. 2) If i declare variables where the size of al variables gone beyond sizeof stack (assume it is 1000) what 'll happen? voud foo( int a, innt b)
13
1584
by: Kamui Shirow | last post by:
Hello! I will write a snippet to illustrate my doubt: snippet 1: void foo( int l ) { char* tmp; tmp = (char*) malloc( l * sizeof( char ) );
0
8367
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8790
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8570
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8650
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7391
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4372
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2781
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2017
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1779
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.