int ** b;
b = (int **)(new int[5 * 12]);
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 12; j++) {
b[i][j] = 0;
}
}
I’m getting access violation error in the row b[i][j] = 0;
Where am I doing it wrong?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Since
bhas typeint**, the expressionb[i]points to the offsetsizeof(int*)*iand(b[i])[j]adds the offsetsizeof(int)*j. In total you are accessing a byte at offsetsizeof(int*)*i + sizeof(int)*jwhich is not at all identical to the offsetsizeof(int)*(i*j)which would be used to determine the the index in your flat one dimensional array.You are requesting a one dimensional array but treating it like a two dimensional array. That cannot work. As usual casting is to blame.