-
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.
Code:
struct arg_file *key = arg_file0("k",NULL,"<key>", "key file (<username>_pass.pem)");
The code pops up a box asking if I'd like to report it's crash to Microsoft when it reaches this part:
Code:
strcpy(keyfile,key->filename[0]);
N.B. I have already defined a "char keyfile[32]" and the keyfile's name is shorter than that. Strangely, the following line prints out the key file name exactly as expected:
Code:
printf("%s\n",key->filename[0]);
Also if I replace the key->filename[0] 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[0]?
--
Mats
-
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:
Code:
Unhandled exception at 0x77c460cb in download_w32.exe: 0xC0000005: Access violation reading location 0x00000000.
Is that of any use at all?
-
It means that you tried to read or write to a NULL pointer.
Either keyfile or key or key->filename or key->filename[0] is NULL.
-
Yes, it means that key->filename[0] is NULL (because it's trying to READ from there).
You didn't initialize with a valid memory location.
-
Edit:
Found it - there was indeed a bug elsewhere that was setting that value to null before I tried to copy it.
Thanks!