f



When will exit(0) fail to exit?

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.

0
junw2000 (224)
1/20/2008 5:45:02 AM
comp.lang.c 30656 articles. 5 followers. spinoza1111 (3246) is leader. Post Follow

17 Replies
802 Views

Similar Articles

[PageSpeed] 41

Jack wrote:
> 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?

-- 
Ian Collins.
0
ian-news (10155)
1/20/2008 6:15:05 AM
On Jan 19, 9:45=A0pm, Jack <junw2...@gmail.com> wrote:
> 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[])
> {
> =A0 =A0 if( argc =3D=3D 1 )
> =A0 =A0 {
> =A0 =A0 =A0 =A0 printf("NO ARGUEMENT\n");
> =A0 =A0 =A0 =A0 exit(0);
> =A0 =A0 }
>
> =A0 =A0 //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.

0
kkylheku (2498)
1/20/2008 6:28:10 AM
On Jan 19, 10:28 pm, Kaz Kylheku <kkylh...@gmail.com> wrote:
> On Jan 19, 9:45 pm, Jack <junw2...@gmail.com> wrote:
>
>
>
> > 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.
0
junw2000 (224)
1/20/2008 7:53:28 AM
Jack wrote:
> 
> 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.

-- 
Ian Collins.
0
ian-news (10155)
1/20/2008 8:22:09 AM
Jack <junw2000@gmail.com> writes:
> 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()?

-- 
Keith Thompson (The_Other_Keith) <kst-u@mib.org>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
0
kst-u (21963)
1/20/2008 8:56:26 AM
Jack wrote:
> 
> 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.

-- 
pete
0
pfiland (6613)
1/20/2008 8:58:17 AM
Ian Collins wrote, On 20/01/08 08:22:
> Jack wrote:
>> 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.

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.
-- 
Flash Gordon
0
spam331 (4048)
1/20/2008 10:50:32 AM
In article <fd4b644b-51d6-4120-997f-992319cbf395@m34g2000hsb.googlegroups.com>,
Jack  <junw2000@gmail.com> wrote:

>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
-- 
:wq
0
richard91 (3692)
1/20/2008 10:58:13 AM
On Jan 20, 10:58 am, pete <pfil...@mindspring.com> wrote:
> Jack wrote:
>
> > 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.
0
vippstar (1211)
1/20/2008 1:55:17 PM
On Sun, 20 Jan 2008 05:55:17 -0800, vippstar wrote:
> 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.
0
truedfx (1926)
1/20/2008 2:08:36 PM
On Jan 20, 4:08 pm, Harald van D=A9=A6k <true...@gmail.com> wrote:
> On Sun, 20 Jan 2008 05:55:17 -0800, vippstar wrote:
> > 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. :-)
0
vippstar (1211)
1/20/2008 2:11:16 PM
"Jack" <junw2000@gmail.com> schreef in bericht 
news:fd4b644b-51d6-4120-997f-992319cbf395@m34g2000hsb.googlegroups.com...
> 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;
}


0
ni7369 (72)
1/20/2008 5:34:50 PM
Jack wrote:
> 
.... snip ...
>
> The exit(0) can not exit. Why exit(0) fails to exit from main()?

Did you #include <stdlib.h> ?

-- 
 [mail]: Chuck F (cbfalconer at maineline dot net) 
 [page]: <http://cbfalconer.home.att.net>
            Try the download section.



-- 
Posted via a free Usenet account from http://www.teranews.com

0
cbfalconer (19194)
1/20/2008 5:54:17 PM
Jack wrote:
> 
.... 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++.

-- 
 [mail]: Chuck F (cbfalconer at maineline dot net) 
 [page]: <http://cbfalconer.home.att.net>
            Try the download section.



-- 
Posted via a free Usenet account from http://www.teranews.com

0
cbfalconer (19194)
1/20/2008 5:56:50 PM
In article <47938AC9.953C9FD0@yahoo.com>,
CBFalconer  <cbfalconer@maineline.net> wrote:

>> The exit(0) can not exit. Why exit(0) fails to exit from main()?

>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
-- 
:wq
0
richard91 (3692)
1/20/2008 11:30:28 PM
Richard Tobin wrote:
> CBFalconer  <cbfalconer@maineline.net> wrote:
> 
>>> The exit(0) can not exit. Why exit(0) fails to exit from main()?
> 
>> 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?

The tar.  Definitely the tar.

-- 
 [mail]: Chuck F (cbfalconer at maineline dot net) 
 [page]: <http://cbfalconer.home.att.net>
            Try the download section.



-- 
Posted via a free Usenet account from http://www.teranews.com

0
cbfalconer (19194)
1/21/2008 2:28:59 AM
Jack <junw2000@gmail.com> wrote:
>
> [ 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);
    }


-- 
pa at panix dot com
0
pa1184 (403)
1/21/2008 10:49:53 PM
Reply: