pete
pete

Reputation: 2046

Visual Studio C++ 2010 how can __argv be a null pointer?

int _tmain(int argc, _TCHAR* argv[])
{

    if (argc != 3) {
        printf("Format is straightline.exe <EO records file> <output file>");
        return 1;
    }
    string eoPath = string(__argv[1]);
    //...other stuff ...
}

If __argc == 3, how can __argv be a null pointer?

My debugger is telling me that __argv is pointing to 0x00000000 after the program crashed when I was trying to reference __argv[1] (and I have verified that __argc == 3). This is a minimal program and it happened in the beginning before I did any sort of processing.

Upvotes: 4

Views: 3434

Answers (3)

QuantumBlack
QuantumBlack

Reputation: 1546

The pointer to __argv can (and will) be null in Unicode configurations.

Upvotes: 3

Necrolis
Necrolis

Reputation: 26181

__argc, __argv/__wargv and __envp/__wenvp are special globals used by the CRT init, you shouldn't ever touch these, rather just stick to the variables passed to your main/wmain/_tmain function, these are derived from the aforementioned globals, and they are guaranteed to be correct, and thanks to Microsofts macro's for the _tmain variant, they will also use the correct character encoding (UNICODE vs MBCS/ASCII).

Upvotes: 1

zastrowm
zastrowm

Reputation: 8243

If this is complied as unicode, then __argv will be null, while __wargv will contain what you want. I believe that there is a __targv that should contain the command line arguments regardless of unicode or ascii.

But why use any of these if you can just use argv passed in as a parameter to _tmain?

Upvotes: 6

Related Questions