Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Output of a program

Reply
Thread Tools

Output of a program

 
 
sonu
Guest
Posts: n/a
 
      03-08-2006
main()
{
char *ptr1,*ptr2;

ptr1=(char*)malloc(sizeof(ptr1));
gets(ptr1);

ptr2=(char*)malloc(sizeof(ptr2));
gets(ptr2);

printf("%s",ptr1);
printf("%s",ptr2);

}

suppose ptr1= aaa bbb ccc ddd eee fff ggg
&
ptr2=kkk


output is :-aaa bbb ccc & kkk

why its not printing complete the first string

Pls any one help me bcoz i need it Urgent

Thanks
sonu

 
Reply With Quote
 
 
 
 
pemo
Guest
Posts: n/a
 
      03-08-2006
sonu wrote:
> main()
> {
> char *ptr1,*ptr2;
>
> ptr1=(char*)malloc(sizeof(ptr1));
> gets(ptr1);
>
> ptr2=(char*)malloc(sizeof(ptr2));
> gets(ptr2);
>
> printf("%s",ptr1);
> printf("%s",ptr2);
>
> }
>
> suppose ptr1= aaa bbb ccc ddd eee fff ggg
> &
> ptr2=kkk
>
>
> output is :-aaa bbb ccc & kkk
>
> why its not printing complete the first string
>
> Pls any one help me bcoz i need it Urgent


You're allocating just enough memory to hold a character pointer, i.e.,
malloc(sizeof(ptr1)) is likely to be something like malloc(4).

If you now enter 'aaa bbb ccc ddd eee fff ggg' and use gets() to read that
into the memory you've allocated, you've over written stuff, and from that
point on, it's not possible to say what the program will do - it's
undefined.

So, you ought to be allocating more memory than you have - try something
like malloc(100), and then use fgets() to read your input lines.

Lastly, main() ought to be int main(void) in your case, you don't need to
cast malloc's return, you should free(ptr1) and ptr2 when you're done with
them, and you should add return 0; [or something like]. You should also
include both stdio.h and stdlib.h if you haven't.


--
==============
Not a pedant
==============


 
Reply With Quote
 
 
 
 
Vladimir S. Oka
Guest
Posts: n/a
 
      03-08-2006
sonu wrote:
> main()


int main(void)

is much better style.

> {
> char *ptr1,*ptr2;
>
> ptr1=(char*)malloc(sizeof(ptr1));


You've just masked a bug. You did not include <stdlib.h>, and have cast
the return value of `malloc`. Without prototype (in <stdlib.h>) the
compiler has to assume `malloc` returns an `int`, which you then force
into a `char *`. *Never* cast the return value of `malloc`.

Aside from that, you allocate the space for `ptr1` the size of the
pointer to `char` on your implementation. Is this really what you
wanted? Probably not, as you then go on and input "aaa bbb ccc ddd eee
fff ggg" which is most likely much bigger.

Congratulations! You've just created a buffer overflow vulnerability.

> gets(ptr1);
>
> ptr2=(char*)malloc(sizeof(ptr2));


Exactly the same problems as above. You manage not to overflow your
buffer (assuming sizeof(char *) >= 4, which is reasonable on modern
hosted implementations), as you input "kkk", which is 4 bytes long
(remember that pesky terminating \0).

> gets(ptr2);
>
> printf("%s",ptr1);
> printf("%s",ptr2);


Not terminating `printf` with `\n` (or doing `fflush(stdout)`) may
result in nothing at all being output (output is line buffered -- no
end of line, no output, maybe).

> }
>
> suppose ptr1= aaa bbb ccc ddd eee fff ggg
> &
> ptr2=kkk
>
>
> output is :-aaa bbb ccc & kkk
>
> why its not printing complete the first string


See comments above. BTW, I'm surprised you get the output you claim you
do.

> Pls any one help me bcoz i need it Urgent


The only urgent things for you right now would be to go back to your C
text book.

Someone may be able/willing to help you further if you specify exactly
what the code above is supposed to achieve.

--
BR, Vladimir

 
Reply With Quote
 
sonu
Guest
Posts: n/a
 
      03-08-2006
Thanx pemo

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      03-08-2006
"sonu" <> writes:
> main()
> {
> char *ptr1,*ptr2;
>
> ptr1=(char*)malloc(sizeof(ptr1));
> gets(ptr1);
>
> ptr2=(char*)malloc(sizeof(ptr2));
> gets(ptr2);
>
> printf("%s",ptr1);
> printf("%s",ptr2);
>
> }

[...]
> Pls any one help me bcoz i need it Urgent


A few things not yet mentioned in other responses:

Proper indentation is your friend, even for a small program like this
one. See any decent C textbook, or most of the code posted here, for
examples of proper indentation.

Please don't use silly abbreviations like "Pls" for "Please", or
"bcoz" for "because". They only make it more difficult to read what
you write, especially for readers whose native language isn't English.

And finally, never ever ever use gets(). Use fgets() instead.
(You'll need to deal with the '\n' character that fgets() stores in
the string, and with its behavior if the input line is longer than
your buffer.)

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
Banfa
Guest
Posts: n/a
 
      03-08-2006

Keith Thompson wrote:
> And finally, never ever ever use gets(). Use fgets() instead.
> (You'll need to deal with the '\n' character that fgets() stores in
> the string, and with its behavior if the input line is longer than
> your buffer.)


What is the reason that you should "never ever ever use gets(). Use
fgets()"?

 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      03-08-2006
Banfa said:

>
> Keith Thompson wrote:
>> And finally, never ever ever use gets(). Use fgets() instead.
>> (You'll need to deal with the '\n' character that fgets() stores in
>> the string, and with its behavior if the input line is longer than
>> your buffer.)

>
> What is the reason that you should "never ever ever use gets(). Use
> fgets()"?


Because you tell fgets how long your buffer is, it can (and does) protect
your buffer against overflow.

You have no way to tell gets how long your buffer is.


--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      03-08-2006
"Banfa" <> wrote in
news: oups.com:

>
> Keith Thompson wrote:
>> And finally, never ever ever use gets(). Use fgets() instead.
>> (You'll need to deal with the '\n' character that fgets() stores in
>> the string, and with its behavior if the input line is longer than
>> your buffer.)

>
> What is the reason that you should "never ever ever use gets(). Use
> fgets()"?



http://c-faq.com/stdio/getsvsfgets.html

--
A. Sinan Unur <>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
write a single line C program whose output is the program itself Puneet C Programming 16 03-20-2005 08:15 AM
parse output screen ok but cant get desired output new file! chuck amadi Python 1 06-23-2004 02:16 PM
Sony Precision Cinema Progressive Output vs Component 480p Output Otto Pylot DVD Video 1 04-18-2004 09:49 PM
Is Fuji S3000 3.2m/pixel output, or 6 m/pixel interpolated output? Peter H Digital Photography 43 12-04-2003 02:35 PM
Output / Debug window output bug? John Bentley ASP .Net 0 09-10-2003 07:38 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57