GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rating: Thread Rating: 2 votes, 5.00 average.
  #1  
Old 05-Dec-2004, 23:28
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Things to Avoid in C/C++ -- gets() , Part 1


I just posted a GID Community blog entitled: Things to Avoid in C/C++ -- gets() , Part 1.

Quote:
C/C++ programmers are allowed to do some things they shouldn't. We are given functions that are supposed to be useful but aren't because of hidden faults, or taught ways to do things that are bad, wrong, not necessary. These posts will discuss many of these as time goes on.

The code in this collection of electronic bits is specifically written in C. I'm using the free Borland 5.5 compiler for the code. For any code that is designed to show the errors, your results may be a little different. But rest assured, the problem exists in many if not all compilers. And if most compilers show these anomalies, wouldn't you rather not use the feature even if it works on your current compiler? read more...

Please let me know what you think.

This is an auto-generated message posted by GIDNetwork™ on behalf of the author.
__________________

Age is unimportant -- except in cheese
  #2  
Old 09-Dec-2004, 21:58
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

void main();


Things to avoid in C/C++
void main()

main() is an integer function, and should be specified as such. Now I can get into long-winded explanation of why, but it's already been done, with examples.

I first turn your attentopn to this article by Steve Summit: http://www.eskimo.com/~scs/readings/...in.960823.html. He explains what could happen and why. How the improper form started and why it's been allowed to continue.

The second is http://users.aber.ac.uk/auj/voidmain.shtml by Alun Jones, with a real world example of an error.

Another good explanation is at http://home.att.net/~jackklein/ctips01.html#int_main.


So the bottom line is void main()
1) is wrong according to the standards.
2) is not guaranteed to work properly.
3) may cause problems for the operating system.
Last edited by WaltP : 09-Dec-2004 at 22:40.
  #3  
Old 15-Jun-2007, 02:03
seprich seprich is offline
Member
 
Join Date: Jun 2007
Posts: 110
seprich has a spectacular aura aboutseprich has a spectacular aura about

Re: Things to Avoid in C/C++ -- gets() , Part 1


Hi,

In your blog:
GIDNetwork > Things to Avoid in C/C++ -- gets() , Part 1

there is the following example:
Quote:
CPP / C++ / C Code:
puts("Enter some characters:");
fgets(b2, 5, stdin);  // 5 is the size of buffer b2
if (b2[strlen(b2)-1] == '\n') 
{   // full input line read
    b2[strlen(b2)-1] = '\0';  // remove the new-line
}
else
{   // parial input line read
    b2[0] = 0;  // empty the b2 buffer
    do
    {   // loop until the new-line is read
        fgets(dummy, 50, stdin);
        strcat(b2, dummy);  // Save input but be sure
                            // sure to test your buffer size
    } while (dummy[strlen(dummy)-1] != '\n');
}
where the "b2[0] = 0;" will cause the 4 chars already read to the buffer to be lost. Probably supposed to be "b2[4] = 0;"

Also if "fgets(b2, 5, stdin); // 5 is the size of buffer b2" we cannot catenate the dummy to the b2. Rather in the spirit of the article I understood that we just want to clean the input stream by reading to dummy and that's all. "dummy" just acts as trash bin.
  #4  
Old 15-Jun-2007, 11:51
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Things to Avoid in C/C++ -- gets() , Part 1


If you read the description carefully, is says
Quote:
To deal with both options at once to either remove the trailing '\n' or clear the buffer, whichever situation exists, define a dummy buffer of say 50 characters (you choose the size) and use this code:
You'll also see that the code itself says:
CPP / C++ / C Code:
else
{   // parial input line read
    b2[0] = 0;  // empty the b2 buffer
The code works as advertised.

Of course, you could just leave the buffer as you suggest, it's just not what was designed.
__________________

Age is unimportant -- except in cheese
  #5  
Old 20-Jun-2007, 01:55
it career it career is offline
New Member
 
Join Date: Jun 2007
Location: askexpert.info
Posts: 4
it career is on a distinguished road

Re: void main();


Quote:
Originally Posted by WaltP
Things to avoid in C/C++
void main()

main() is an integer function, and should be specified as such. Now I can get into long-winded explanation of why, but it's already been done, with examples.

I first turn your attentopn to this article by Steve Summit: www.eskimo.com. He explains what could happen and why. How the improper form started and why it's been allowed to continue.

The second is users.aber.ac.uk by Alun Jones, with a real world example of an error.

Another good explanation is at home.att.net.


So the bottom line is void main()
1) is wrong according to the standards.
2) is not guaranteed to work properly.
3) may cause problems for the operating system.
My compiler throws an error in case i declare main as void. So I guess this is taken care by compiler now.
  #6  
Old 21-Jun-2007, 12:13
mikhail mikhail is offline
Junior Member
 
Join Date: Mar 2004
Location: Dublin, Ireland
Posts: 73
mikhail is on a distinguished road

Re: void main();


Quote:
Originally Posted by it career
My compiler throws an error in case i declare main as void. So I guess this is taken care by compiler now.
Varies from compiler to compiler.
 
 

Recent GIDBlogToyota - 2008 September Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Floating point exception error (Note-long post) crystalattice C++ Forum 16 11-Sep-2004 07:37
Vilitas Merger - any problems? nickbeee Web Hosting Forum 13 09-Sep-2004 05:04
Some small things Allowee GIDSearch™ 6 14-Jul-2004 01:40
A Comprehensive Digest of C++ mithunjacob C++ Forum 39 20-Jun-2004 19:09
Creating a search engine friendly forum BobbyDouglas Search Engine Optimization Forum 14 20-Nov-2003 16:31

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 17:09.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.