473,403 Members | 2,338 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Memory access violation (only with -O2).

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
1 1760
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Bengt Richter | last post by:
Python 2.3.2 (#49, Oct 2 2003, 20:02:00) on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import time >>> time.mktime((1969, 12, 31, 17, 0, 0, 0, 0, 0))...
3
by: Kyle Teague | last post by:
I have a list of pointers to structs as a private member of a class. If I call begin() in the same function as I added the data then no access violation occurs. However, if I try to call begin() in...
1
by: szudor | last post by:
Hi, When I start db2cc, the program hangs with memory access violation in javaw.exe. Strating with trace option, in java trace file I found the following information: 0SECTION TITLE...
1
by: BillyO | last post by:
In the attached code fragment I have a buffer overflow and a memory access violation. When I run the code .Net fails to verify the IL because of the buffer overflow and I get an exception as...
3
by: Binary | last post by:
VC++ .NET 2003: Access violation with /O2 compiler switch; no AV with /O Hi I'm in the process of narrowing down a problem, and I have reduced the code involved to the following If someone could...
12
by: aling | last post by:
Have following code snip: struct struc { int member1; int member2; } ; printf("&((struc*)0)->member2=%p\n", &((struc*)0)->member2); In VC7.1, the output is 4, the offset of member2 in struc.
6
by: mangesh | last post by:
1 - How to cach invalid memory access exception ? Does standard library provide any help ? 2 - Also when one write catch(...) { //........... } what is wirtten inside catch block . How do...
3
by: zombek | last post by:
Hi. I'm quite a begginer. I wanted to learn the usage of tinyxml library so I wrote a little program which compiles on g++ 4.1.1 with -O2 -Wall, but when I run it a get a message "memory access...
39
by: Martin | last post by:
I have an intranet-only site running in Windows XPPro, IIS 5.1, PHP 5.2.5. I have not used or changed this site for several months - the last time I worked with it, all was well. When I tried it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.