main

Where does the returned value for 'main' function go?

只谈情不闲聊 提交于 2019-12-04 01:58:15
问题 In C, a function always returns its value to the calling function and never to itself ( if return type is not void ). Like, int main() But since ' main ' function is called by the Operating System , which is not a function . So, whom does the 'main' function returns its value? Where does the value go, when its returned using the expression return(0); at the end of the program? 回答1: This is completely OS-specific, but usually the OS invokes a program by Setting up the program's address space,

How can I start a 'main' in a new process in Java?

一个人想着一个人 提交于 2019-12-04 00:31:47
The question is rather simple. How can I start a main method in another java process? Now I do it like this: startOptions = new String[] {"java", "-jar", "serverstart.jar"}; new ProcessBuilder(startOptions).start(); But they asked me to do it not with an external .jar file. The serverstart.jar obviously has a main method, but it it possible to call that main method in another process, without calling the .jar file? I'm thinking of something like this: new ProcessBuilder(ServerStart.main(startOptions)).start(); But I don't know if anything like that exists. Kind regards, Assuming a new thread

How to test the main package functions in golang?

雨燕双飞 提交于 2019-12-03 20:45:01
问题 I want to test a few functions that are included in my main package, but my tests don't appear to be able to access those functions. My sample main.go file looks like: package main import ( "log" ) func main() { log.Printf(foo()) } func foo() string { return "Foo" } and my main_test.go file looks like: package main import ( "testing" ) func Foo(t testing.T) { t.Error(foo()) } when I run go test main_test.go I get # command-line-arguments .\main_test.go:8: undefined: foo FAIL command-line

Purpose of 'if __name__ == “__main__”:' [duplicate]

拥有回忆 提交于 2019-12-03 19:09:18
问题 This question already has answers here : What does if __name__ == “__main__”: do? (29 answers) Closed 5 years ago . I am trying to understand some code I found which reads command line arguments (attached below). My concern is what purpose of the "if __name__ == __main__" line is... Why would I use that line instead of just using the code below, main(sys.argv[1:]) . What extra use does it provide? import sys, getopt def main(argv): inputfile = '' outputfile = '' try: opts, args = getopt

Scala App val initialization in main method

妖精的绣舞 提交于 2019-12-03 15:09:56
问题 I have some code: object Main extends App { val NameTemplate = """^([A-Za-z]+)_(\d+)\.png""".r override def main (args:Array[String]) { // Why is NameTemplate null here? } } Why is NameTemplate not initialized within the main method? 回答1: If you are using App trait, then you don't need to override main method - just write your code in the body of the object : object Main extends App { val NameTemplate = """^([A-Za-z]+)_(\d+)\.png""".r println(NameTemplate) val NameTemplate(name, version) =

Calling a function immediately before main

穿精又带淫゛_ 提交于 2019-12-03 13:17:47
Is is possible to register a function to be run immediately before main is entered? I know that all global objects are created before entering main , so I could put the code in the constructor of a global object, but that does not guarantee any particular order. What I would like to do is put some registration code into the constructor, but alas, I don't know what to put there :) I guess this is highly system-specific? If you're using gcc , you can use the constructor attribute on a function to have it called before main (see the documentation for more details). constructor destructor The

Is it well-defined behaviour to exit the program before main?

。_饼干妹妹 提交于 2019-12-03 12:32:14
It's definitely possible to execute code before main is called, as seen by many examples in this question . However, what if in that pre-main code, the program is told to exit via std::exit or std::abort ? Since main is defined as the start of a program, what consequences are there from exiting before the start? Upon printing something in each section, I get the following results: Format: Section : output Main : main Init (called before main) : init Exit (set up with std::atexit inside Init) : exiting Sample runs: Init called without exiting: init main returns 0 Init calls std::exit(0): init

Why main method is static in java [duplicate]

元气小坏坏 提交于 2019-12-03 11:13:51
This question already has answers here : Why is the Java main method static? (37 answers) I have heard some people saying " if main is not static then JVM could create an object of class containing main and call that main through object. But the problem is how JVM knows which constructor to call in case of overloaded constructors or even if there is only one paramaterized constructor, then what to pass." Is that the correct reason? Because how can object of class be created without entering into main function? Please give your views on this. If that is not the right reason, then what's the

Can argc be zero on a POSIX system?

戏子无情 提交于 2019-12-03 10:22:52
问题 Given the standard definition for the main program: int main(int argc, char *argv[]) { ... } Under which circumstances can argc be zero on a POSIX system? 回答1: Yes, it is possible. If you call your program as follows: execl("./myprog", NULL, (char *)NULL); Or alternately: char *args[] = { NULL }; execv("./myprog", args); Then in "myprog", argc will be 0. The standard also specifically allows for a 0 argc as noted in section 5.1.2.2.1 regarding program startup in a hosted environment: 1 The

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

妖精的绣舞 提交于 2019-12-03 09:24:48
问题 This question already has answers here : Closed 8 years ago . Duplicate: What is the proper declaration of main? What do we mean by the arguments in this main function? What are they trying to tell us? int main(int argc, char** argv) UPDATE: And, is the preceding line of code similar to this int main(int argc, char* argv[]) ? If so, how can we say that char** argv is similar to char* argv[] as they don't look similar from an array point of view? How is it compared with int main() which does