Get pointer to argv element Boost Program Options - c++

I'm making the assumption that the array of strings passed into the main function as argv in void main( int argc, char* argv[] ) remains in scope for the duration of the application.
My question is therefore whether it is possible, using boost::program_options to get a pointer to the actual element in argv which represents a particular option.
That is, rather than allocating a std::string when parsing, it is possible to just get a pointer to the start of a string option value.

Related

What does each of these `const`s mean?

Normally people write the main function like this:
int main( int argc, char** argv )
However, this came to my mind:
int main( const int argc, const char* const* const argv )
or maybe I should write it like this cause it seems more intuitive:
int main( const int argc, const char *const *const argv )
What does each of the consts mean in argv (I think I understand them all but am still not sure)?
Also is this a valid code? What issues/limitations can it cause when using argv inside main?
Now what's the difference between this one and the latter:
int main( const int argc, const char* const argv[] )
The prototype is defined as "int main( int argc, char** argv )"
There is really no point in using a const pointer to access the parameters later unless you don't want to get it changed, which is up to you
The purpose of const pointers is to make sure that they are not changed throughout the code. You can live without them, but it helps avoid other issues, bugs for example.
On the other side, there is no performance gain (Optimizing_compiler)
1 The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters:
int main(void) { /* ... */ }
or with two parameters (referred to here as argc and argv, though any names may be used, as they are local to the function in which they are declared):
int main(int argc, char *argv[]) { /* ... */ }
or equivalent;9) or in some other implementation-defined manner.
2 If they are declared, the parameters to the main function shall obey the following
constraints:
— The value of argc shall be nonnegative.
— argv[argc] shall be a null pointer.
— If the value of argc is greater than zero, the array members argv[0] through argv[argc-1] inclusive shall contain pointers to strings, which are given implementation-defined values by the host environment prior to program startup. The intent is to supply to the program information determined prior to program startup from elsewhere in the hosted environment. If the host environment is not capable of supplying strings with letters in both uppercase and lowercase, the implementation shall ensure that the strings are received in lowercase.
— If the value of argc is greater than zero, the string pointed to by argv[0] represents the program name; argv[0][0] shall be the null character if the program name is not available from the host environment.
If the value of argc is greater than one, the strings pointed to by argv1 through argv[argc-1] represent the program parameters.
— The parameters argc and argv and the strings pointed to by the argv array shall be modifiable by the program, and retain their last-stored values between program startup and program termination
so, in short, adding the const is illegal and probably technically not possible (depending on the compiler)
Checking the standard you see the following rules: (ISO/IEC 9899:TC3), go to 5.1.2.2.1

C++ Emulating int main(int argc, char *argv[]) from node

Basically I am working on integrating an existing C++ file with our javascript / node using their NAPI, functionality. I have it working on a test C++ file so I know I have the setup working. However on the actual C++ file it is designed to run from the command line with argc and argv from the command line. Basically I just need to invoke the main method in C++ from inside of my other function, which means there is no command line. So I have to pass in values for argc and argv. argc is just an int, that is easy enough, but argc is a char ** type, which from my research looks like it is an array of character arrays aka strings?
This is my current code at the bottom of my c++ file
void Init(Env env, Object exports, Object module) {
exports.Set("main", Function::New(env, main(2,{"test","test2"})));
}
NODE_API_MODULE(addon, Init)
The argc value is working fine
I am trying to create a temporary / test value for argv to pass in but I am having an issue figuring out how to make an array of type char ** with my values.
argv is an array of pointers to strings (actually, NUL-terminated character arrays), where element 0 is the name of the program, elements 1 ... argc-1 are the program arguments, and element argc must be NULL1.
There's no array literal in C++, so you have to create explicitly an array variable to pass it to a function. Even worse, main is allowed to modify the passed arguments, so you cannot even build an array of string literals, as they are read only; thus, you have to explicitly allocate read/write space for each argument. A barebones solution can be to have single buffers for each argument and build the pointer array out of them:
char argv0[] = "test_program";
char argv1[] = "arg1";
char argv2[] = "arg2";
char *argv[] = {argv0, argv1, argv2, NULL};
main(3, argv);
A more flexible one (especially if you have to build your arguments dynamically) can be to use an std::vector<std::string>:
std::vector<std::string> args = { "test_program", "arg1", "arg2" };
// ... here you may add other arguments dynamically...
args.push_back("arg3"); // whatever
// build the pointers array
std::vector<char *> argv;
for(std::string &s: args) argv.push_back(&s[0]);
argv.push_back(NULL);
main(argv.size()-1, argv.data());
Now, coming to your code:
void Init(Env env, Object exports, Object module) {
exports.Set("main", Function::New(env, main(2,{"test","test2"})));
}
NODE_API_MODULE(addon, Init)
besides the fact that you cannot build argv to pass to main like that, you are trying to invoke main and pass its result as second argument to Function::New, while Function::New wants some callable type (e.g. a function pointer) to register as handler for the export named main! Copying from the Function::New documentation:
/// Callable must implement operator() accepting a const CallbackInfo&
/// and return either void or Value.
So, as a simple example, you could export your main as a parameterless JS function that returns nothing (undefined, I guess?) by registering a callback like this:
void MainCallback(const CallbackInfo& info) {
char argv0[] = "test_program";
char argv1[] = "arg1";
char argv2[] = "arg2";
char *argv[] = {argv0, argv1, argv2, NULL};
main(3, argv);
}
void Init(Env env, Object exports, Object module) {
exports.Set("main", Function::New(env, MainCallback));
}
NODE_API_MODULE(addon, Init)
Finally, as others said, technically in C++ main is somewhat magic - it's undefined behavior to invoke it from inside the program; in practice, on any platform that I know of that can also run node.js, main is a perfectly regular function that happens to be invoked by the C runtime at startup, so I don't think that this will cause you any problem.
Notes
So, you could say it's a NULL-terminated array of NUL-terminated character arrays. Notice that here NULL = null pointer; NUL = string terminator, i.e. '\0'.

What is a char** argv[] in C++?

I am clear about the difference between char* argv[] and char** argv (as explained, for instance, in this question).
But what kind of type is char** argv[] ? A pointer to a pointer to a pointer of chars? And what would that be, when used to pass arguments to the program?
For the record, the type occurs in the following declaration of a C++ library I am trying to interface to Python:
/** Initialize the Stage library. Stage will parse the argument
array looking for parameters in the conventional way. */
void Init( int* argc, char** argv[] );
If main definition is main(int argc, char * argv[]) and you want to send argv via pointer, you will do Init( &argc, &argv ). And the corresponding parameter can be char ** argv[].
I assume that this Init function will make changes to the argc and argv that main has received, so it needs pointers to them.
If the library uses some program arguments, the Init function can handle them, and then remove those arguments from the argument list before it is processed by the rest of the program.
char** argv[] is an array of char**, IOW an array of pointers to pointers to chars, or more likely an array of pointers to null-terminated character strings, eg:
char *str1 = "...";
char *str2 = "....";
char** argv[2];
argv[0] = &str1;
argv[1] = &str2;
However, if argv is coming from main() itself, then char** is a typo, it must be char* instead:
char* argv[]
But if argv is coming from some library instead, it very well could be wrapping the original argv array from main() inside its own array, in which case char**[] might make sense so it can point at the original strings and not have to copy them.

Pass argv[1] by reference

I've an issue with passing argv to a function. Basically, I want to pass &argv[1] to the function and work from that. However, I don't understand why the following does work:
void fun(char**&argv, const int& argc);
int main(int argc, char** argv)
{
fun(++argv, --argc);
return 0;
}
yet the following doesn't work:
void fun(char**&argv, const int& argc);
int main(int argc, char** argv)
{
fun(&argv[1], --argc);
return 0;
}
Prototype:
void myfunc (char **argv, int argc)
Example usage:
myfunc (argv, argc)
If you wanted to pass ONLY one string (argv[1]):
void myfunc (char *arg, int argc)
...
myfunc (argv[1], argc)
And if you wanted to pass an array of strings starting at argv[1]:
void myfunc (char **arg, int argc)
...
myfunc (&argv[1], argc)
The error I get when I try this is that argv[1] is a temporary value and you can't assign that to a non-const reference. If you instead do this:
void fun( char** const&argv, const int& argc);
Then either ++argv or &argv[1] will work.
Obviously, if you planned to actually change the value of argv itself, this won't work, but I presume that you aren't planning to do this since you're not passing a reference to argv itself but rather to the memory location of the second entry in argv.
The reason that the first one works and the second doesn't is that the ++ operator at the front modifies the variable and then returns not just its value after the modification, but the variable itself. As a result, the variable can then be passed into fun as a reference. But the &argv[1] is just the value of a memory location. It's a computed value, not a variable in its own right, so it can't be passed in as a non-const reference.
If you're planning to try to change the value of argv itself (to point it to a different array of character arrays), then &argv[1] doesn't work for that anyway, since it's not argv. ++argv, however, is argv, just after it's had its value adjusted.
You could also handle all of this without references at all, which would make things much easier, but that's up to you. If it were my code, though, I wouldn't be using references here. The const reference to an int could just be passed by value (which would be easier) and passing a reference to a double pointer doesn't save you any null checking (as it could still well be null). So there isn't much point to it. But if you like your code to be complex, feel free.

What happens when you don't follow the practice of argv and argc [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
main(int argc, char *argv[])
Main's Signature in C++
If i write:
int main(int argc, char** argv)
i get proper commandline input.
What would happen if i wrote say,
int main(int foo, double fooDouble, fooType FooVar)
Is this OS-dependent or does it depend on the compiler?
Given that it does compile, it will still only be called with the argc and argv arguments.
So your fooDouble will get the pointer value of argv, and FooVar will get whatever value is in that register/stack space used for that argument position (which may not have been initialized by the callee, so it may hold any undefined value).
This code doesn't even have to compile. If it does, undefined behaviour may occur.
The effect of adding a third (or fourth, or fifth...) argument to main indeed depends on the operating system. For instance, on Windows (and I believe on Unix as well) you can have a third argument which grants access to the environment variables:
int main( int argc, char **argv, char **env )
The C standard (BS ISO/IEC 9899:1999) gives two alternatives for main:
int main(void) { /* ... */ }
and
int main(int argc, char *argv[]) { /* ... */ }
It also allows equivalents, so an argv of char ** argv for example. Additional arguments are "neither blessed nor forbidden by the Standard". Such addtions will be compiler and runtime (not operating system) specific.
The arguments are passed by the C runtime, which calls main(). Passing any other type would be problematic on general environments like Windows and UNIX, so quite how you would expect to pass a double or fooType is beyond me.
Invoking a program from either a command-line or using interfaces like execve (UNIX) or CreateProcess (Win32) involves passing zero delimited strings. In theory you could hack it to pass a binary value and then cast it in main, provided it does not contain a '\0' anywhere except at the end, which would be challenging.
EDIT: it occurs to me that you can call main() from within your program - the well known obvuscated C code "The twelve days of Christmas" does this. In this case there is no reason why you can pass anything the prototype allows.