When will exit(0) fail to exit?

J

Jack

My program is about several thousands lines. It takes arguements from
the command line.
If no arguement is given, it should simply exist. Below is the
architectureof main() :

int main(int argc, char *argv[])
{
if( argc == 1 )
{
printf("NO ARGUEMENT\n");
exit(0);
}

//Other parts of the code

}

It also links with other libraries.
If no arguement is given on command line, it only outputs "NO
ARGUEMENT", then hangs there.
The exit(0) can not exit. Why exit(0) fails to exit from main()?

Thanks.
 
I

Ian Collins

Jack said:
My program is about several thousands lines. It takes arguements from
the command line.
If no arguement is given, it should simply exist. Below is the
architectureof main() :

int main(int argc, char *argv[])
{
if( argc == 1 )
{
printf("NO ARGUEMENT\n");
exit(0);
}

//Other parts of the code

}

It also links with other libraries.
If no arguement is given on command line, it only outputs "NO
ARGUEMENT", then hangs there.
The exit(0) can not exit. Why exit(0) fails to exit from main()?
Did you include <stdlib.h>?

If so, what does the exact code you posted do?
 
K

Kaz Kylheku

My program is about several thousands lines. It takes arguements from
the command line.
If no arguement is given, it should simply exist. Below is the
architectureof main() :

int main(int argc, char *argv[])
{
    if( argc == 1 )
    {
        printf("NO ARGUEMENT\n");
        exit(0);
    }

    //Other parts of the code

}

It also links with other libraries.
If no arguement is given on command line, it only outputs "NO
ARGUEMENT", then hangs there.
The exit(0) can not exit. Why exit(0) fails to exit from main()?

The other libraries may hook into platform-specific initialization
systems; they can gain control before main runs and gain control at
exit time.

(Obvious example: C++ libraries with global constructors and
destructors).

Use whatever tools you have at your disposal on the platform to
investigate. E.g. attach a debugger to the hung program, interrupt it
and examine where it is.
 
J

Jack

My program is about several thousands lines. It takes arguements from
the command line.
If no arguement is given, it should simply exist. Below is the
architectureof main() :
int main(int argc, char *argv[])
{
if( argc == 1 )
{
printf("NO ARGUEMENT\n");
exit(0);
}
//Other parts of the code

It also links with other libraries.
If no arguement is given on command line, it only outputs "NO
ARGUEMENT", then hangs there.
The exit(0) can not exit. Why exit(0) fails to exit from main()?

The other libraries may hook into platform-specific initialization
systems; they can gain control before main runs and gain control at
exit time.

(Obvious example: C++ libraries with global constructors and
destructors).

Use whatever tools you have at your disposal on the platform to
investigate. E.g. attach a debugger to the hung program, interrupt it
and examine where it is.

Yes. It links some C++ libraries. It only hangs on AIX systems. But
for Linux, it works fine. Why?

I find that maybe a C++ library causes the problem. What should I do?
I have to link that library. Otherwise, the linker complains
"Undefined symbols", although some symbols are not needed in my code,
but they are used in other libraries.

Thanks.
 
I

Ian Collins

Jack said:
Yes. It links some C++ libraries. It only hangs on AIX systems. But
for Linux, it works fine. Why?
Use your debugger and find out. The cause is platform specific, not C
language specific.
 
K

Keith Thompson

Jack said:
My program is about several thousands lines. It takes arguements from
the command line.
If no arguement is given, it should simply exist. Below is the
architectureof main() :

int main(int argc, char *argv[])
{
if( argc == 1 )
{
printf("NO ARGUEMENT\n");
exit(0);
}

//Other parts of the code

}

It also links with other libraries.
If no arguement is given on command line, it only outputs "NO
ARGUEMENT", then hangs there.
The exit(0) can not exit. Why exit(0) fails to exit from main()?

Has there been a call to atexit()?
 
P

pete

Jack said:
My program is about several thousands lines. It takes arguements from
the command line.
If no arguement is given, it should simply exist. Below is the
architectureof main() :

int main(int argc, char *argv[])
{
if( argc == 1 )
{
printf("NO ARGUEMENT\n");
exit(0);
}

What if argc == 0 ?

Try
if (2 > argc)
instead.
 
F

Flash Gordon

Ian Collins wrote, On 20/01/08 08:22:
Use your debugger and find out. The cause is platform specific, not C
language specific.

It may, of course, be a C++ specific problem where the C++ libraries are
doing something before you have explicitly called anything in them. So
it may be worth asking in comp.lang.c++ whether this could be the case.
 
R

Richard Tobin

Jack said:
My program is about several thousands lines. It takes arguements from
the command line.
If no arguement is given, it should simply exist. Below is the
architectureof main() :

Does this exact program fail, or does only your "several thousand line"
program fail?

-- Richard
 
V

vippstar

Jack said:
My program is about several thousands lines. It takes arguements from
the command line.
If no arguement is given, it should simply exist. Below is the
architectureof main() :
int main(int argc, char *argv[])
{
if( argc == 1 )
{
printf("NO ARGUEMENT\n");
exit(0);
}

What if argc == 0 ?

Try
if (2 > argc)
instead.

AFAIK argc cannot be 0.
argv[0] is guaranteed to be a string and argv[argc] to be NULL.
 
H

Harald van Dijk

AFAIK argc cannot be 0.
argv[0] is guaranteed to be a string and argv[argc] to be NULL.

argv[0] is guaranteed to hold a pointer to a string only if argc is
greater than zero.
 
V

vippstar

AFAIK argc cannot be 0.
argv[0] is guaranteed to be a string and argv[argc] to be NULL.

argv[0] is guaranteed to hold a pointer to a string only if argc is
greater than zero.


Thanks for the correction,
guess I shouldn't repeat what I've heard from others. :)
 
S

Serve Laurijssen

Jack said:
My program is about several thousands lines. It takes arguements from
the command line.
If no arguement is given, it should simply exist. Below is the
architectureof main() :

int main(int argc, char *argv[])
{
if( argc == 1 )
{
printf("NO ARGUEMENT\n");
exit(0);
}

//Other parts of the code

}


Does this hang too? No exit call anymore, main returns normally

int main(int argc, char *argv[])
{
if( argc == 1 )
{
printf("NO ARGUEMENT\n");
}
else
{
//Other parts of the code
}
return 0;
}
 
C

CBFalconer

Jack said:
.... snip ...

I find that maybe a C++ library causes the problem. What should I
do? I have to link that library. Otherwise, the linker complains
"Undefined symbols", although some symbols are not needed in my
code, but they are used in other libraries.

This group deals with C, not C++. Try comp.lang.c++.
 
R

Richard Tobin

The exit(0) can not exit. Why exit(0) fails to exit from main()?
[/QUOTE]
Did you #include <stdlib.h> ?

Do you have some realistic scenario where not including <stdlib.h>
causes the described behaviour, or are you just appealing to the
"undefined behaviour" tar pit?

-- Richard
 
C

CBFalconer

Richard said:
Do you have some realistic scenario where not including <stdlib.h>
causes the described behaviour, or are you just appealing to the
"undefined behaviour" tar pit?

The tar. Definitely the tar.
 
P

Pierre Asselin

Jack said:
[ C program hangs ... ]

Yes. It links some C++ libraries. It only hangs on AIX systems. But
for Linux, it works fine. Why?

If it links with any C++ you may need to write the main() in C++
as well, and compile and link with the C++ compiler. I don't know
if this is the case on AIX, but their docs will tell you.

Worst case, you'll end up with a trivial wrapper,

// main.cpp
extern "C" int the_real_main(int, char **);
int main(int argc, char **argv)
{
return the_real_main(argc, argv);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,955
Messages
2,570,117
Members
46,705
Latest member
v_darius

Latest Threads

Top