access violation error

Hello everyone. For some reason when i run the following code i get a access memory violation error.

[\code]
void indexing(list<string> passwordList, char ** pwdAry, int * cudaIndexAry) {
for (int y = 0; y < gridSize; y++) {
//Loop through blocks(x)
for (int x = 0; x < gridSize; x++) {
//Loop through each thread within each block
threadID = 0;
for (int l = 0; l < (blockSize*blockSize); l++) {
//Give each thread a password index
for (int j = 0; j < numPerThread; j++) {

//only runs once
if (runFlag == 1) {
while (temp.size() != 0) {
string tmp = temp.front();
char * val;
val = new char[1024];

const char* cstr;

cstr = tmp.c_str();

strcpy(pwdAry[currItr], tmp.c_str());

/*tmp.copy(pwdAry[currItr], tmp.length(), 0);
pwdAry[currItr] = '\0';*/
/* memset(pwdAry[currItr], 0, 1024);
memcpy(pwdAry[currItr], cstr, (strlen(cstr)));*/
val[currItr] = '\0';
//cout << pwdAry[currItr] << endl;
//block index (x)
cudaIndexAry[currIteration] = x;
currIteration++;
//block index (y)
cudaIndexAry[currIteration] = y;
currIteration++;
//Thread Id
cudaIndexAry[currIteration] = threadID;
currIteration++;
//password array element index
cudaIndexAry[currIteration] = currItr;
currIteration++;
cout << pwdAry[currItr] << endl;
temp.pop_front();
currItr++;

}
runFlag = 0;
}
//grab the first password
string tmp = passwordList.front();
char * val;
val = new char[1024];
const char* cstr;

cstr = tmp.c_str();
strcpy(pwdAry[currItr], tmp.c_str());


cout << pwdAry[currItr] << endl;


/*memset(pwdAry[currItr], 0, 1024);

memcpy(pwdAry[currItr], cstr, (strlen(cstr)));

val[tmp.size()] = '\0';*/

//block index (x)
cudaIndexAry[currIteration] = x;
currIteration++;
//block index (y)
cudaIndexAry[currIteration] = y;
currIteration++;
//Thread Id
cudaIndexAry[currIteration] = threadID;
currIteration++;
//password array element index
cudaIndexAry[currIteration] = currItr;

currIteration++;
currItr++;

//delete the password just popped from the list
passwordList.pop_front();

}
threadID++;
}
}
}
}

any idea why?
Last edited on
You might want to edit your code and use code tags. It is beyond unreadable.
access violation error


These are normally to do with something not being initialised, or access outside of an array boundary. If one uses the STL these problems can be avoided. On the other hand you may have a reason for doing a C program.

Can we also ask what the purpose of the program is?

And please provide a minimal program that we can compile & run with cpp.sh (the gear icon at the top right of a code snippet)

With tags, I just use the <> button on the format menu, but here is the full article on tags:

http://www.cplusplus.com/articles/z13hAqkS/


The other omnipresent advice to is use a debugger. One can quickly solve problems like this.

Also, try not to mix C and C++ coding. With C++ it is easier to use the STL containers std::string and std::vector etc, rather than char arrays and pointers.

val[currItr] = '\0';
Where does currItr get set? This function increments it but never sets it or decrements it. Maybe it just keeps growing and growing until it 1024 or larger, at which time this will be an out-of-bounds array access.

It's hard to say what else might be wrong. If possible, provide a working program that shows the error
Topic archived. No new replies allowed.