Thread: Why am I having an access violation?

1. 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.

2. 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. 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.

4. 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?

5. In fact, why not
Code:
`strtok(..., ",;")`
? strtok()'s second parameter is a const pointer, so it won't be modified.

6. >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. 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).

8. 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

9. >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. 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. That would point input to a read only string, which would explain the access violation.

12. and make a memory leak.

13. That too.

Popular pages Recent additions