By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,146 Members | 1,631 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,146 IT Pros & Developers. It's quick & easy.

Memory access violation (only with -O2).

P: n/a
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 [i] [0], &trojka [i] [1], &trojka
[i] [2]);
}
for (int i = 0; i < ile; i++) {
printf ("trojka [%d] : %u %u %u\n", i, trojka [i] [0], trojka
[i] [1], trojka [i] [2]);
}
unsigned short n;
for (int i = 0; i < ile; i++) {
n = nwd (trojka [i] [0], trojka [i] [1]);
printf ("nwd %u i %u : %u\n", trojka [i] [0], trojka [i] [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;
}

Oct 28 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
In article <11**********************@k70g2000cwa.googlegroups .com>,
zo****@gmail.com wrote:
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?
Solve it by using iostream instead of cstdio. "%u" is input for an
unsigned int, not an unsigned short.
Szymon

--------------------------CODE-----------------------
#include <cstdio>
#include <iostreaminstead
>
unsigned short nwd (unsigned short a, unsigned short b);

int main () {
unsigned short ile;
scanf ("%u", &ile);
cin >ile;
unsigned short trojka [ile] [3];
The above line it technically illegal. I suggest you use the Matrix
class from the FAQ instead.
http://www.parashift.com/c++-faq-lit...html#faq-13.10
for (int i = 0; i < ile; i++) {
scanf ("%u %u %u", &trojka [i] [0], &trojka [i] [1], &trojka
[i] [2]);
cin >trojka( i, 0 ) >trojka( i, 1 ) >trojka( i, 2 );
}
for (int i = 0; i < ile; i++) {
printf ("trojka [%d] : %u %u %u\n", i, trojka [i] [0], trojka
[i] [1], trojka [i] [2]);
}
unsigned short n;
for (int i = 0; i < ile; i++) {
n = nwd (trojka [i] [0], trojka [i] [1]);
printf ("nwd %u i %u : %u\n", trojka [i] [0], trojka [i] [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;
}
--
To send me email, put "sheltie" in the subject.
Oct 28 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.