Strange strcpy crash

This is a discussion on Strange strcpy crash within the Windows Programming forums, part of the Platform Specific Boards category; 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 ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Location
    Oxford, England
    Posts
    27

    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?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Can you use GDB to inspect your key->filename[0]?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Location
    Oxford, England
    Posts
    27
    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?

  4. #4
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    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.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Jan 2008
    Location
    Oxford, England
    Posts
    27
    Edit:

    Found it - there was indeed a bug elsewhere that was setting that value to null before I tried to copy it.
    Thanks!
    Last edited by knirirr; 07-10-2008 at 02:19 AM. Reason: Correction

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange crash.....
    By azjherben in forum C++ Programming
    Replies: 13
    Last Post: 05-21-2009, 04:58 PM
  2. A Full Program to analyze.
    By sergioms in forum C Programming
    Replies: 2
    Last Post: 12-30-2008, 08:42 AM
  3. Strcpy
    By Godders_2k in forum C Programming
    Replies: 17
    Last Post: 12-12-2007, 11:34 AM
  4. Can not debug a crash
    By hannibar in forum Windows Programming
    Replies: 2
    Last Post: 06-30-2007, 10:02 AM
  5. Strange results using dnsapi and windns
    By Niara in forum Networking/Device Communication
    Replies: 3
    Last Post: 08-13-2005, 10:21 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21