|
-
Jan 26th, 2002, 08:42 PM
#1
int main() or void main ()?
What is really correct way ?
-
Jan 26th, 2002, 09:11 PM
#2
transcendental analytic
int as return value is standard I think, I'll check it up
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Jan 27th, 2002, 07:35 AM
#3
Monday Morning Lunatic
The standard is:
Code:
int main(int argc, char **argv)
(I can't remember whether envp is in the standard or not, but it's supported on quite a few compilers).
You don't have to use all the arguments, so int main() is still valid.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Jan 27th, 2002, 10:01 AM
#4
PowerPoster
I always use int main, but my programming teacher insists on using void
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
-
Jan 27th, 2002, 10:17 AM
#5
Monday Morning Lunatic
Silly fool
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Jan 27th, 2002, 12:27 PM
#6
The ANSI standard suggests int, because void cannot return a value.
-
Jan 27th, 2002, 12:40 PM
#7
Both linux and windows want the app to return a value, so int main() is better, but not necessary, if you do void main() the return value wil usually be some random number if the CRT startup does not catch it.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 27th, 2002, 12:43 PM
#8
Monday Morning Lunatic
Are we talking about what you can get away with on x86, or what the standard says? 
On x86 the only difference return type makes is whether you get a real value or not, since it's returned by register. Some systems expect the value to be pushed onto the stack or something similar, and you can imagine the destruction caused if you didn't return a value
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Jan 28th, 2002, 08:35 AM
#9
Yeah, but I haven't seen anyone on this forum ever talking about any other CPUs than x86 compatible.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 28th, 2002, 01:04 PM
#10
Monday Morning Lunatic
It applies to me at least (the reason why I'm a Nazi about it comes out now ).
At work I use MIPS and SPARC. Since I don't know much about their internals I have to follow things like this "just in case".
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Jan 28th, 2002, 01:35 PM
#11
Gee, what kind of weird job do you have?
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 28th, 2002, 01:37 PM
#12
Monday Morning Lunatic
Writing finite element analysis post-processing software that runs on Sun and SGI workstations 
Admittedly PCs (especially those dual Athlon monsters) are overtaking them now, but that's what we've got and when you've got 8 CPUs you don't talk back
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Jan 28th, 2002, 03:53 PM
#13
Cool! What does that mean? "finite element analysis post-processing". Is it me or just my lack of English?
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 28th, 2002, 05:19 PM
#14
Monday Morning Lunatic
Do a search for finite element analysis and you'll see what I mean
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Feb 2nd, 2002, 02:39 PM
#15
New Member
-
Feb 2nd, 2002, 03:17 PM
#16
transcendental analytic
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Feb 3rd, 2002, 07:19 AM
#17
Monday Morning Lunatic
Originally posted by setnewfocus
Straut-soup
LOL
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Feb 3rd, 2002, 12:13 PM
#18
Fanatic Member
whats the point of returning a value in main()?
i use void 90% of the time, and if i wanna exit i just do exit(1)
Visit www.fragblast.com
Gaming, forums, and a online RPG/Battle system
(__Flagg) DOT NET? is this a Hindi Dating service?
-
Feb 3rd, 2002, 12:15 PM
#19
Monday Morning Lunatic
Because it tells the OS how your program did. Returning a value other than 0 indicates that your program exited abnormally.
PS: I don't think exit is a good idea under C++, it might not call the destructors... *checks*
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Feb 3rd, 2002, 05:49 PM
#20
Look -
Every language has baggage. C grew up under UNIX. Standard ansi C allows void main() as well as int main().
Just like it allows goto.
Does this make goto a good choice?
NO!
Under unix you return 0 for success (ERROR_SUCCESS) and any other number (almost always one) as failure.
EXIT_FAILURE is a macro that is there for every version of c I've ever seen or heard of.
Code:
#define EXIT_FAILURE 1
C images can be chained, therefore the calling image wants to know the status of the called image. _exec() chains images as does about 20 other fuinctions under VC++.
Therefore, use int main() even tho I use void main() for C examples on this board a lot. This give you compatibility you may need later on.
-
Feb 3rd, 2002, 05:52 PM
#21
Monday Morning Lunatic
I thought it was the other way round, i.e. UNIX grew up under C.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Feb 3rd, 2002, 08:00 PM
#22
There seem to be certain situations where goto is ok:
Code:
void x()
{
int* x = NULL;
char* y = NULL;
long* z = NULL;
x = new int[120];
y = new char[480];
z = new long[120];
...
if(failed)
{
delete [] x;
delete [] y;
delete [] z;
}
...
if(failed)
{
delete [] x;
delete [] y;
delete [] z;
}
...
}
As opposed to:
Code:
void x()
{
int* x = NULL;
char* y = NULL;
long* z = NULL;
x = new int[120];
y = new char[480];
z = new long[120];
...
if(failed)
goto err;
...
if(failed)
goto err;
...
return;
err:
delete [] x;
delete [] y;
delete [] z;
}
This creates much cleaner code (no copy/pasting). Is this one of those few places where goto is ok?
Z.
-
Feb 3rd, 2002, 09:09 PM
#23
Fanatic Member
thats where i use it, especially if theres a bunch of cleaning up to do
Visit www.fragblast.com
Gaming, forums, and a online RPG/Battle system
(__Flagg) DOT NET? is this a Hindi Dating service?
-
Feb 4th, 2002, 07:45 AM
#24
transcendental analytic
That's also where I thought goto had a rational use, but it's flawed, exceptions are much neater and prefered.
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Feb 4th, 2002, 09:53 AM
#25
So, if failed, throw an exception?
Z.
[edit]
I really cant type.
-
Feb 4th, 2002, 12:33 PM
#26
Monday Morning Lunatic
Code:
void x() {
int* x = new int[120];
char* y = new char[480];
long* z = new long[120];
try {
// something fails
} catch(std::exception &ex) {
cerr << ex.what() << endl;
delete[] x;
delete[] y;
delete[] z;
}
}
Although that won't protect you against new failing, which technically is supposed to throw an exception of type bad_alloc (derived from the standard exception class). So the better solution here is to have the pointers wrapped into a managed class that will delete the data if others failed.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Feb 4th, 2002, 01:21 PM
#27
transcendental analytic
but parksie, why is that a better solution? Why not catch the bad_alloc specifically?
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Feb 4th, 2002, 06:22 PM
#28
Monday Morning Lunatic
Because you don't know which of the three new statements failed.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Feb 5th, 2002, 10:04 AM
#29
transcendental analytic
That was indeed annoying problem that I didn't take into account, will consider a change in the exception handling rules in BORK.
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|