main() called inside main()

c

    Next

  • 1. The one problem that I have with C...
    ...is that there is no single man page that lists the stdlib functions that I can reference. I'm working in a Unix environment. -- Daniel Rudy Email address has been encoded to reduce spam. Remove all numbers, then remove invalid, email, no, and spam to reply.
  • 2. constants in array size
    I have the following code: const int num_segments = 16; int some_function(void) { int key[num_segments + 2]; ... } My compiler barfs out the warning "ANSI C forbids variable-size array", but lets it pass. Does an expression using a const-variable count for the purposes of "variable-size array"? I was under the impression that declaration is legal ANSI C. -- Gopi Sundaram XXXX@XXXXX.COM
  • 3. OT: So, if you claim to have no free will...
    If you claim that there is no such thing as free will, then you are hereby ordered to state the following: "I release my will to the care of the Mother of Everything, unconditionally." Of course, if you _do_ have free will, you'll possibly have some unpredictable reaction to such an edict. But if you have no free will, then it is ordained that you have no choice whatsoever but to absolutely, irrevocably refuse to claim "I release my will to the care of The Mother Of All." Thank you very much.

main() called inside main()

Postby rahulsinner » Fri, 05 May 2006 14:55:02 GMT

hi,

int main(void)
{
  main();
  return 0;
}

wat does the standard says about the above code snippet?


Re: main() called inside main()

Postby Prasad » Fri, 05 May 2006 15:01:35 GMT

Nothing,
This will result in infinite loop. Eating a lot of processor time, and
you may see your PC hang.

Thanks.


Re: main() called inside main()

Postby Keith Thompson » Fri, 05 May 2006 17:10:14 GMT

 XXXX@XXXXX.COM  writes:

It's a recursive call to main, which is perfectly legal.

Like any infinite recursive call, it will either exceed some system
resource limit, or it will run indefinitely (if the compiler optimizes
tail recursion).

The "return 0;" will never be reached.

-- 
Keith Thompson (The_Other_Keith)  XXXX@XXXXX.COM   < http://www.**--****.com/ ~kst>
San Diego Supercomputer Center             <*>  < http://www.**--****.com/ ~kst>
We must do something.  This is something.  Therefore, we must do this.

Re: main() called inside main()

Postby Flash Gordon » Fri, 05 May 2006 20:11:00 GMT



Wrong, the standard says what will happen if you call main recursively.


Or the compiler could fail to optimise it in which case the loop will 
not be infinite since you will run out of stack space. Or process limits 
might prevent it from eating a log of CPU time. Or the compiler might 
optimise it to some form of sleep forever instruction.


Please provide context when replying, Google is *not* Usenet. See the 
Google section at  http://www.**--****.com/ 
links to.
-- 
Flash Gordon, living in interesting times.
Web site -  http://www.**--****.com/ 
comp.lang.c posting guidelines and intro:
 http://www.**--****.com/ 

Inviato da X-Privat.Org - Registrazione gratuita  http://www.**--****.com/ 

Re: main() called inside main()

Postby Jordan Abel » Fri, 05 May 2006 21:27:20 GMT



It may run forever, or crash. I don't know if a stack overflow (i.e. 
running out of memory in a way that cannot be recovered from) is 
considered undefined behavior.

Re: main() called inside main()

Postby Default User » Sat, 06 May 2006 01:56:20 GMT





See below.


Brian
-- 
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.

Re: main() called inside main()

Postby Keith Thompson » Sat, 06 May 2006 03:51:59 GMT

Jordan Abel < XXXX@XXXXX.COM > writes:



I'm fairly sure it is, since the standard doesn't define what the
behavior is.

-- 
Keith Thompson (The_Other_Keith)  XXXX@XXXXX.COM   < http://www.**--****.com/ ~kst>
San Diego Supercomputer Center             <*>  < http://www.**--****.com/ ~kst>
We must do something.  This is something.  Therefore, we must do this.

Re: main() called inside main()

Postby Jordan Abel » Sat, 06 May 2006 06:38:24 GMT




I guess i'm used to thinking of UB as something that can at least in 
principle be identified by careful examination of the source. that is, 
for any given set of inputs ["inputs" including returns from library 
functions whose outputs are not fully determined by their inputs - e.g. 
malloc returning null or not], a program either does cause UB or 
doesn't. stack overflows are a big hole in this, and i think they're 
unique.

Re: main() called inside main()

Postby Ian Collins » Sat, 06 May 2006 06:45:04 GMT






Are they UB and are they unique?

I'd categorise them as an environment constraint violation, another
example would be opening more files then the operating environment permits.

Stack size is environment specific, so a well formed program that
operates correctly in one environment might violate the constraints of
another.

-- 
Ian Collins.

Re: main() called inside main()

Postby Jordan Abel » Sat, 06 May 2006 06:56:08 GMT








That doesn't cause UB. It causes fopen to return a null pointer.


Which is unique among ALL things which cause UB.


Re: main() called inside main()

Postby Ian Collins » Sat, 06 May 2006 07:06:46 GMT






OK.
How about dereferencing an odd address on a target the doesn't support this?

-- 
Ian Collins.

Re: main() called inside main()

Postby Jordan Abel » Sat, 06 May 2006 08:20:31 GMT









Things that would cause this to happen ALWAYS cause UB. UB doesn't 
always mean something bad happens.

Re: main() called inside main()

Postby Robert Smith » Sat, 06 May 2006 20:07:05 GMT






This is allowed in C and performs the obvious useless recursive call. It is
forbidden however in the C++ standard



Re: main() called inside main()

Postby CBFalconer » Sun, 07 May 2006 02:32:48 GMT





In C it is of primary use in writing obfuscated code.  C++ needs no
assistance in this respect, and thus can dispense with the
capability.

-- 
"If you want to post a followup via groups.google.com, don't use
 the broken "Reply" link at the bottom of the article.  Click on 
 "show options" at the top of the article, then click on the 
 "Reply" at the bottom of the article headers." - Keith Thompson
More details at: < http://www.**--****.com/ ;
Also see < http://www.**--****.com/ ;



Re: main() called inside main()

Postby Richard Heathfield » Tue, 09 May 2006 07:23:59 GMT

 XXXX@XXXXX.COM  said:


Not a lot.

It's legal C, but eventually you'll run out of something or other, depending 
on what system you are using.

-- 
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
 http://www.**--****.com/ 
email: rjh at above domain (but drop the www, obviously)

Similar Threads:

1.int main(int argc, char *argv[] ) vs int main(int argc, char **argv )

Is this a style thing?

int main(int argc, char *argv[] )   or   int main(int argc, char **argv )

i.e. *argv[] or  **argv

Why choose the latter?


2.int main() or int main(void)?

Hi, guys!
   I met a problem:
   Should I use "int main()" or "int main(void)", is that a kind of
"style"
problem?
And which is the standard C99 recommend?
   If it's a "style" one, where can I get the standard or nearly
standard C
style? K&R or what?

   Thanks a lot!

                                               Frederick Ding
                                               2005-12-3

3.int main(void) { return main(); }

Is that in the object line a conforming program?
If so, why?
If not, why?
I'd expect it to be much like
int main(void)
{
    for (;;);
}

But if I compile it with lcc-win32 and run it in its rundos.exe, it says the 
program exits with a return value of -1073741819 (i.e. -2^30 + 5) after 
0.032 seconds or so. Why?

-- 
#include <stdio.h>
#include <stdlib.h>
int main(void) /* Don't try this at home */ {
    const size_t dim = 256; int i;
    for (i=0; malloc(dim); i++) /*nothing*/ ;
    printf("You're done! %zu\n", i*dim);
    puts("\n\n--Army1987"); return 0;
}


4.int main(int argc, char *argv[] ) vs int main(int argc, char **argv )

Sidney Cadot < XXXX@XXXXX.COM > wrote in message news:<budvlg$6v7$ XXXX@XXXXX.COM >...
> August Derleth wrote:
> 
> > [...snip...]
>  
> >   int crc32(char *buf, int len);
> > 
> > Would compute the 32-bit cyclical redundancy check of a buffer 
> > containing character values.
> 
> Wouldn't it be more appropriate if the return type was unsigned? The 
> resulting value denotes a polynome with binary coefficients, and there 
> is nothing to justify a special meaning for the sign bit.
> 
> Forthermore, if you use signed ints inside the routine, you are going to 
> do bitwise operations on signed numbers, which may give headaches 
> concerning portability.
> 
> (By pretty much the same token, I'd make the buf's base type an unsigned 
> char.)

I'd shoot for...

  unsigned long crc32(const void *, size_t);

-- 
Peter

5.What does MAIN_ _MAIN_ __MAIN__ mean?(to invoke clapack)

6. Main procedure inside a package?

7. Calling main() from GNU asm -- Very Urgent

8. Q: recursive call to main, termination of program



Return to c

 

Who is online

Users browsing this forum: No registered users and 56 guest