Z
zombek
Hi.
When I comipile my program (source at the bottom) with : g++ -O2
-static it gives me 'memory access violation' warning and when I dont
use -O2 it doesn't. I need to use -O2 because my professor will use it
(automated tests). The error looks like I went out of array bounds but
I don't. How to solve it?
Szymon
--------------------------CODE-----------------------
#include <cstdio>
unsigned short nwd (unsigned short a, unsigned short b);
int main () {
unsigned short ile;
scanf ("%u", &ile);
unsigned short trojka [ile] [3];
for (int i = 0; i < ile; i++) {
scanf ("%u %u %u", &trojka [0], &trojka [1], &trojka
[2]);
}
for (int i = 0; i < ile; i++) {
printf ("trojka [%d] : %u %u %u\n", i, trojka [0], trojka
[1], trojka [2]);
}
unsigned short n;
for (int i = 0; i < ile; i++) {
n = nwd (trojka [0], trojka [1]);
printf ("nwd %u i %u : %u\n", trojka [0], trojka [1],
n);
}
return 0;
}
unsigned short nwd (unsigned short a, unsigned short b) {
unsigned short tmp;
while (b) {
tmp = a % b;
a = b;
b = tmp;
}
return a;
}
When I comipile my program (source at the bottom) with : g++ -O2
-static it gives me 'memory access violation' warning and when I dont
use -O2 it doesn't. I need to use -O2 because my professor will use it
(automated tests). The error looks like I went out of array bounds but
I don't. How to solve it?
Szymon
--------------------------CODE-----------------------
#include <cstdio>
unsigned short nwd (unsigned short a, unsigned short b);
int main () {
unsigned short ile;
scanf ("%u", &ile);
unsigned short trojka [ile] [3];
for (int i = 0; i < ile; i++) {
scanf ("%u %u %u", &trojka [0], &trojka [1], &trojka
[2]);
}
for (int i = 0; i < ile; i++) {
printf ("trojka [%d] : %u %u %u\n", i, trojka [0], trojka
[1], trojka [2]);
}
unsigned short n;
for (int i = 0; i < ile; i++) {
n = nwd (trojka [0], trojka [1]);
printf ("nwd %u i %u : %u\n", trojka [0], trojka [1],
n);
}
return 0;
}
unsigned short nwd (unsigned short a, unsigned short b) {
unsigned short tmp;
while (b) {
tmp = a % b;
a = b;
b = tmp;
}
return a;
}