Strange strcpy crash
I've been porting a Linux/Mac command-line application to Windows XP using Dev-C++. It's all working other than one particular part where command line arguments are copied to strings. The command line options are processed by argtable in the same way as the first piece of example code. Here's the struct that concerns a "key file" which the users must supply.
The code pops up a box asking if I'd like to report it's crash to Microsoft when it reaches this part:
struct arg_file *key = arg_file0("k",NULL,"<key>", "key file (<username>_pass.pem)");
N.B. I have already defined a "char keyfile" and the keyfile's name is shorter than that. Strangely, the following line prints out the key file name exactly as expected:
Also if I replace the key->filename with a quoted file name in the strcpy above then the rest of the code runs as it should. Can anyone suggest what the problem might be?
Can you use GDB to inspect your key->filename?
Unfortunately I can't persuade Dev-C++ to show me the contents of that variable. When the crash occurs, "Microsoft Development Environment" can be called up to tell me the following:
Is that of any use at all?
Unhandled exception at 0x77c460cb in download_w32.exe: 0xC0000005: Access violation reading location 0x00000000.
It means that you tried to read or write to a NULL pointer.
Either keyfile or key or key->filename or key->filename is NULL.
Yes, it means that key->filename is NULL (because it's trying to READ from there).
You didn't initialize with a valid memory location.
Found it - there was indeed a bug elsewhere that was setting that value to null before I tried to copy it.