Error in output by cout from main arguments

0

Good afternoon; I have a loop to get the main arguments that have been used in the command line, however, after removing them, the following cout are not shown. I understand that cout with < < receives reference variables. If I try to take out the argument argv [argc] that must be null (as indicated in, for example Stroustrup "The c ++ programming language" Special Ed. Point 6.1.7 Pag. 122), the following cout do not come out. It seems that the last parameter (null == argv [argc]) invalidates cout for some reason. I put a simple code that does not show End Adios.

#include <iostream>
using namespace std;
int main(int argc, char* argv[]) {
    cout<<"Numero de parametros: "<<argc<<"\n";
    for (int i=0; i<=argc; i++)  // Aqui si ponemos argc-1  si sale bien
        cout<<"valor de "<<i<<": "<<argv[i];
    cout<<"\nFin\n";
    cout<<"Adios\n";
    return 0;
}

Could someone tell me what character or variable sends cout the last exit that makes the output of cout blocked?

    
asked by jose_luis 07.06.2017 в 21:12
source

1 answer

1

Apart from what is indicated in the comments (the dot-and-comma omitted and the maximum value of argc ), what happens is that std::basic_ostream does not support sending pointers to characters with NULL value (or nullptr ). This is what is known as undefined behavior .

That means that the behavior under that circumstance depends on the implementation ; some compilers will do what you indicate; others, can directly generate a general protection error, segment errors, or anything else.

In summary: do not try using cout to show pointers to strings with NULL value.

Note that behavior does not occur for pointers void * with NULL value. In that case, its value is displayed correctly.

    
answered by 07.06.2017 / 21:24
source