Thread: Why am I having an access violation?

  1. #1
    cout << "Bye World!";
    Join Date
    Jun 2006
    Posts
    40

    Why am I having an access violation?

    Here is my code:
    Code:
    	double **double_array;
    	double_array = new double*[rows];
    	for(int i=0;i<rows;i++)
    		double_array[i] = new double[columns];
    	char split[3];
    	strcpy(split, ",;");
    	for(int i=0;i<rows;i++)
    		for(int j=0;j<columns;j++)
    			double_array[i][j] = charToDouble(strtok((i==0&&j==0)?&input[1]:NULL,split));
    The rows and columns have already be determined to be positive nonzero integers (actually, they are 2 and 2). The char* input contains "[3,2;1,2]" (I called strcpy(input, "[3,2;1,2]")). When I run it through the MSVC++ 2005 debugger, it tells me that I have an access violation and the line that it points to is line 120 of strtok.c (included by the <string> header I assume), so I figure that there is something wrong with my call to strtok. This happens when i=j=0, but it might also happen afterwards (it never gets that far). charToDouble takes in a char* and returns a double. I should probably rename it to charArrayToDouble or stringToDouble.

    Hopefully, I will figure it out the second I push the "Submit New Thread" button, as has happened before, but I doubt it. So here goes. Any ideas? Thanks in advance.
    Last edited by ldb88; 06-10-2007 at 06:39 PM.

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    If you can tell us what you are trying to do, we may be able to help you with an actual C++ solution opposed to your C-ish code.

  3. #3
    cout << "Bye World!";
    Join Date
    Jun 2006
    Posts
    40
    Sorry I didn't realize it is pretty much C (except new and delete). If someone wants to move it, that'll be fine with me. If it worked correctly, what the code above should do is copy all of the numbers from input into double_array. The purpose of this is so that I can create a matrix (which will be copied from double_array) with the values specified by input. I'm not worried about checking whether there is anything but '0' through '9', '-', '[', ']', ',' and ';' in input right now. My instructor is obsessed with using char arrays instead of strings, so that's why I'm doing this with char arrays.
    Last edited by ldb88; 06-10-2007 at 07:18 PM.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    char split[3];
    strcpy(split, ",;");
    why not

    Code:
    char split[3] = ",;";
    Is input string a writable array?

    Why not to use vectors instead of dynamic arrays?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    In fact, why not
    Code:
    strtok(..., ",;")
    ? strtok()'s second parameter is a const pointer, so it won't be modified.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >The char* input contains "[3,2;1,2]"
    You may need to add ']' to the split list. Otherwise you'll be passing 2] to charToDouble().

  7. #7
    cout << "Bye World!";
    Join Date
    Jun 2006
    Posts
    40
    My instructor had an example in which he created the char array for the second parameter. I changed it to ",;]" (thanks for pointing out ']' swoopy) and got rid of split. But that still doesn't explain the access violation. I can't use STL in any way (per instructor's order). vart, if you mean is input not constant when you say "Is input string a writable array?" then input is not constant, so it is writable. But strtok does not modify what it gets anyway (it just moves its local pointers).
    Last edited by ldb88; 06-11-2007 at 06:25 PM.

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    But strtok does not modify what it gets anyway
    This is wrong.
    StrTok puts the null character in your string...

    Have you tried to debug?
    Or add traces to the code to see what line at what step causes the crash?

    BTW - You should check return value of the strtok before using it. It can be NULL
    Last edited by vart; 06-11-2007 at 09:55 PM.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >The char* input contains "[3,2;1,2]" (I called strcpy(input, "[3,2;1,2]")).
    Did you allocate any memory for your char *? Otherwise you can't strcpy() to it. Or you can simply make it a char array (char input[] = "[3,2;1,2]";)

  10. #10
    cout << "Bye World!";
    Join Date
    Jun 2006
    Posts
    40
    Ok I understand now. I had put
    Code:
    char *input = new char[1000];
    input = "[3,2;1,2]";
    rather than using strcpy. Thanks for all the help.

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    That would point input to a read only string, which would explain the access violation.

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    and make a memory leak.

  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    That too.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Access violation... can't figure it out...
    By Raigne in forum C++ Programming
    Replies: 7
    Last Post: 10-11-2007, 10:52 AM
  2. access violation in int array
    By George2 in forum C Programming
    Replies: 2
    Last Post: 08-02-2007, 11:28 PM
  3. FtpFileFind access violation with MS VC++ 6.0
    By earth_angel in forum C++ Programming
    Replies: 3
    Last Post: 09-22-2005, 07:02 PM
  4. Help! CListCtrl access violation
    By bonkey in forum Windows Programming
    Replies: 4
    Last Post: 11-18-2003, 02:40 PM
  5. 0xC0000005: Access Violation
    By Strider in forum Windows Programming
    Replies: 3
    Last Post: 11-07-2001, 02:46 PM