main

Why check if (*argv == NULL)? [duplicate]

点点圈 提交于 2019-12-05 00:40:24
This question already has answers here : When can argv[0] have null? (4 answers) Closed 2 years ago . In the data structures class that I am currently taking, we have been tasked with writing a web crawler in C++. To give us a head start, the professor provided us with a program to get the source from a given URL and a simple HTML parser to strip the tags out. The main function for this program accepts arguments and so uses argc/argv. The code used to check for the arguments is as follows: // Process the arguments if (!strcmp(option, "-h")) { // do stuff... } else if (!strcmp(option, "")) { //

Declaring main as friend considered harmful?

倖福魔咒の 提交于 2019-12-04 23:34:27
Discussion I know that main can be a friend of a class : #include <iostream> class foo { friend int main(); int i = 4; }; int main() { foo obj; std::cout << obj.i << std::endl; } LIVE DEMO However, I feel that although this is perfectably allowable it conceals many dangers. Questions Are there any valuable uses in making main a friend of a class? Are there any reasons that declaring main as friend of a class should be considered harmful? Ben Voigt The choice whether to use or avoid a legal feature becomes moot if the feature is not, in fact, legal. I believe there's serious doubt surrounding

Why main() in C++ is not overloaded to use std::string?

こ雲淡風輕ζ 提交于 2019-12-04 22:26:21
I was trying to create a program that takes arguments by command line, using main() function arguments. As a (basic) C++ programmer (even if I know quite well pointers and array in C-style) I hardly ever used char* strings and C-arrays. I spent some to take main() arguments and transform it in std::string ... So asked myself: why in C++ the main() function is not overloaded to take an std::vector<std::string> argv instead of the old char* argv[] ? For "overload" I mean the coexistence of main() functions like int main() and int main(int argc, char *argv[]) , not the overloading of a normal

Exception handling before and after main

落爺英雄遲暮 提交于 2019-12-04 20:11:52
问题 Is it possible to handle exceptions in these scenarios: thrown from constructor before entering main() thrown from destructor after leaving main() 回答1: You can wrap up your constructor withing a try-catch inside of it. No, you should never allow exception throwing in a destructor. The funny less-known feature of how to embed try-catch in a constructor: object::object( int param ) try : optional( initialization ) { // ... } catch(...) { // ... } Yes, this is valid C++. The added benefit here

Where does the OS store argv and argc when a child process is executed?

99封情书 提交于 2019-12-04 12:45:59
问题 I'm having some difficulty understanding how the OS passes data from the address space of a parent process to the address space of a child process. Namely, in a C program, where is argc and argv stored upon being passed into main? I understand how argv is essentially a double pointer. What I'm not understanding is what the OS does with those values after loading them into the kernel. After creating an address space for the child process does it push these values on the stack of the new space?

Why is main() argument argv of type char*[] rather than const char*[]?

人盡茶涼 提交于 2019-12-04 10:16:51
问题 When I wrote the following code and executed it, the compiler said deprecated conversion from string constant to char* int main() { char *p; p=new char[5]; p="how are you"; cout<< p; return 0; } It means that I should have written const char * . But when we pass arguments into main using char* argv[] we don't write const char* argv[] . Why? 回答1: Because ... argv[] isn't const. And it certainly isn't a (static) string literal since it's being created at runtime. You're declaring a char *

Java Beginner question about String[] args in the main method

可紊 提交于 2019-12-04 09:43:33
So I just tried excluding String[] args from the main method It compiled alright ! But JVM is showing an exception Why did it compile when String[] args HAS to be included every time ? What is going on here ? Why won't it show a compilation error ? typing this made me think that may be compiler did not see it as THE main method ..is that so ? If that is the case..why not ? I mean isn't there supposed to be just one main method that MUST have String[] args as the argument ? typing this made me think that may be compiler did not see it as THE main method ..is that so ? Correct. There is no

Java - start another class' main in a different process

a 夏天 提交于 2019-12-04 09:08:33
I need a clean way to start many instances of a Java program with a GUI, and I want to do it programmatically. The "program" i want to run is just a .class file (a compiled .java file with a main method), it should show a GUI and run independently from the others (as its own process). I also need to pass that program some arguments. Check EDIT5 for the complete working solution code. Here's the class that's supposed to start the many processes package startothermain; import java.io.IOException; import java.util.logging.Level; import java.util.logging.Logger; public class Starter { public

IntelliJ build wrong JAR: Could not find or load main class

十年热恋 提交于 2019-12-04 07:31:02
I have a simple example public class FileSystemReadFile { public static void main(String[] args) throws IOException { System.out.println("Reading the file" + args[0]); } } which is created in IntelliJ where I want to build JAR file; So what I did: Added Artifact with dependencies (presumably I have some); Ensure that MANIFEST.MF is located in src\main\resources\META-INF\ as it is already mentioned somewhere here on the site. Run Artifact build which gave me JAR file in out folder and I run that jar file that said me "Could not find or load main class" java <name>.jar You may see that main

Java Puzzler- What is the reason? [closed]

眉间皱痕 提交于 2019-12-04 07:06:13
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center . Closed 6 years ago . I wrote following code. class String { private final java.lang.String s; public String(java.lang.String s){ this.s = s; } public java.lang.String toString(){ return s; } public static void main(String[] args) { String s = new String("Hello world"); System