I have a two-dimensional array, which needs to be allocated dynamicly, so i wrote this:
int * array;
array = malloc((sizeA * sizeB) * sizeof(int));
It's basicly an one-dimensional array, which acts like it's a two-dimensional one. I'm accessing it like the following:
for(int y = 0; y <= sizeA; y++){
for(int x = 0; x <= sizeB; x++){
array[x * y] = /* some stuff */;
}
}
I thought everything is fine to write it like that, but in some cases this would not work.
Case 1: x * y has the same result multiple times
x = 3, y = 2
x * y = 6
x = 2, y = 3
x * y = 6, aswell
Meaning some values get overwritten and get lost. But there is also another case, which leads to issues.
Case 2: primary number out of range of sizeA or sizeB
Some values can't be used, because primary numbers can only be the result of the multiplication of the number itself and 1. So, while some values in my array get overwritten, some others dont even get used.
What can I do to prevent this from happening?