473,406 Members | 2,843 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,406 software developers and data experts.

strange exception

Hello everybody,

first of all, i want to tell you that i'm a newbie in c++ programming.
I former just used delphi and now i have to learn c++. It's a little
less save than delphi, so i've some difficulties.

But now i'm standing infront of a problem i can't solve on my one. I
tried to build a matrix-class:

class matrix
{
int x,y;
double **field;
double *fieldcol;

public:

matrix(int cols, int rows) //constructor
{
int i;
int j;
x=cols;
y=rows;
field = new double*[x-1];
for (i=0;i<x;i++)
{
field[i]=new double[y-1];
}
for (i=0;i<x;i++)
{
for (j=0;j<x;j++)
{
field[i][j]=0;
}
}
}
}
In the main program i initialize 2 variables:

matrix a(2,2);
matrix b(2,2);

When i now try to run the program i get a windows exeption. But when i
start the program in debugging mode, where i can jump through the
program, it works without any problem. Also when i only initialize a
without b, the program works fine. Or when i initialize both variables
with the parameters (1,1), it also works without any trouble.

Has anybody an idea? I use the Editor Dev-C++ coming along with an
gnu-compiler, both for windows xp.

Thanks for your help!

Chris

May 12 '06 #1
9 1428
Christian Kirsch wrote:
Hello everybody,

first of all, i want to tell you that i'm a newbie in c++ programming.
I former just used delphi and now i have to learn c++. It's a little
less save than delphi, so i've some difficulties.

But now i'm standing infront of a problem i can't solve on my one. I
tried to build a matrix-class:

class matrix
{
int x,y;
double **field;
double *fieldcol;

public:

matrix(int cols, int rows) //constructor
{
int i;
int j;
You should always give variables the smallest scope possible. So remove the
definition here, and below, in your for loop, just do:

for (int i = 0; i < x; ++i)
x=cols;
y=rows;
field = new double*[x-1];
Why one less?
for (i=0;i<x;i++)
{
field[i]=new double[y-1];
}
for (i=0;i<x;i++)
Here, you go past the end of the array, since you are accessing x elements,
but only have space for x-1 elements.
{
for (j=0;j<x;j++)
Same here.
{
field[i][j]=0;
}
}
}
}


May 12 '06 #2
Christian Kirsch wrote:
double **field;
double *fieldcol;
Grab a book called /Accelerated C++/, and then don't use * there. Use
std::vector<double>. Then the Standard Library will manage the low-level
memory concerns for you.
matrix(int cols, int rows) //constructor
{
x=cols;
y=rows;
field = new double*[x-1];
Now field contains cols-1 elements, not cols elements.
for (i=0;i<x;i++)
{
field[i]=new double[y-1];


And now you use the element at field[cols-1], which exists (for a secret
reason) but which cannot be assigned to.

When you corrupt memory, the program will exhibit "undefined behavior". That
term will now play the leading role in your nightmares as you learn C++.

"Undefined behavior" means the program could crash, could limp along and
then crash, could appear to work correctly, or could make the nearest toilet
explode. So you when you run the program two different ways, you see two
different effects.

Use new..[cols], and switch to vector as soon as possible.

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
May 12 '06 #3
On Fri, 12 May 2006 07:04:41 -0700, Christian Kirsch wrote:
Hello everybody,

first of all, i want to tell you that i'm a newbie in c++ programming. I
former just used delphi and now i have to learn c++. It's a little less
save than delphi, so i've some difficulties.

But now i'm standing infront of a problem i can't solve on my one. I tried
to build a matrix-class:

class matrix
{
int x,y;
double **field;
Why not a std::vector<std::vector<double> > ?
double *fieldcol;
And this isn't ever used.

public:

matrix(int cols, int rows) //constructor {
int i;
int j;
x=cols;
y=rows;
field = new double*[x-1];
And if you call create a matrix of (1, 1), you new 0 doubles? I think
your "x - 1" is wrong.
for (i=0;i<x;i++)
{
field[i]=new double[y-1];
Now I know your x-1 is wrong. You've already indexed off the end of your
dynamic memory. Undefined Behaviour. Anything can happen.
}
for (i=0;i<x;i++)
{
for (j=0;j<x;j++)
Uh, shouldn't that by "j < y" ?
{
field[i][j]=0;
}
}
}
}

And the times that it did "work"... were just flukes.
May 12 '06 #4
>Now I know your x-1 is wrong. You've already indexed off the end of your
dynamic memory. Undefined Behaviour. Anything can happen.


Well my thought was this: I want x elements and the index starts with 0
so i have to decrease x one time. Or does new double [5] create an
array with 5 elements 0 to 4? Then of course x-1 should be replaced...
I'll try...

And thats my largest problem, in delphi the debugger tells me that i'm
out of range so I'm a little spoiled in that case ;-)

Thanks!
Chris

May 12 '06 #5
>Why not a std::vector<std::vector<double> > ?

;-), because it was the exercise to do it that way, don't ask why...

Thanks Chris

May 12 '06 #6
Christian Kirsch wrote:
Why not a std::vector<std::vector<double> > ?


;-), because it was the exercise to do it that way, don't ask why...


Regardless what your professor requests, also start learning the Standard
Library.

I would finish each assignment, once exactly as requested, and again using
the best style possible.

--
Phlip
http://www.greencheese.us/ZeekLand <-- NOT a blog!!!
May 12 '06 #7
field = new double*[x-1];
--> this means that you want to allocate x-1 elements double*.

for (i=0;i<x;i++)
{
field[i]=new double[y-1];
your "i" is iterating from 0 to x-1 (including) --> x elements.
But you only allocated x-1 so you probably get an index out of bounds
exception because you're trying to access the element field[x-1] which
doesn't exist.

So to summarize your problem is that in c/c++ the index in an array
starts at 0 (not 1) and when you say field = new double* [x-1] this
means allocate x-1 elements starting from 0.

May 12 '06 #8
On Fri, 12 May 2006 07:41:33 -0700, Christian Kirsch wrote:
Now I know your x-1 is wrong. You've already indexed off the end of your
dynamic memory. Undefined Behaviour. Anything can happen.
Well my thought was this: I want x elements and the index starts with 0 so
i have to decrease x one time. Or does new double [5] create an array with
5 elements 0 to 4? Then of course x-1 should be replaced... I'll try...


Yep counting in C++ (learned from C) starts at 0. So double x[5] declares
an array containing 5 items... numbered from 0 to 4.

And thats my largest problem, in delphi the debugger tells me that i'm
out of range so I'm a little spoiled in that case ;-)


But... there's a run-time cost to that bounds checking. General C++
philosophy... don't pay for what you don't use.
May 12 '06 #9
Yes, that was the problem. I knew, that the index starts with 0, the
thing I didn't know was that you actually have the length of the array
as new - parameter. I thought it would be the Index of the very last
item. So that was the problem.

Thanks to all of you!!!

Chris

May 12 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Nedelcho Stanev | last post by:
Hello All, I have strange problem with libodbc++ ( 0.2.3 or 0.2.2 ). i'm using mysql-4.0.14 , MyODBC-3.51.06 and unixODBC-2.2.6 configured with following options 1.MySQL ../configure...
2
by: Paul Drummond | last post by:
Hi all, I am developing software for Linux Redhat9 and I have noticed some very strange behaviour when throwing exceptions within a shared library. All our exceptions are derived from...
3
by: Don McNamara | last post by:
Hi, I've hit quite a strange problem with XmlSerializer on my W2K3 server. When I serialize/deserialize using an exe on my local computer (XP), everything works fine. When I put the code out on...
3
by: nick | last post by:
The program runs well on Local harddisk. But it always popup the exception if I run it on Netware mapping disk. After debugger say there is no source code on the exception. Also the following error...
2
by: Chris | last post by:
Hi, a strange behaviour when working with exceptions : when I divide and integer by 0 will an exception be thrown. OK but, when I divide a double by 0 is no exception thrown ??? How come ? ...
1
by: Chris | last post by:
Hi, a strange behaviour when working with exceptions : when I divide and integer by 0 will an exception be thrown. OK but, when I divide a double by 0 is no exception thrown ??? How come ? ...
0
by: Mark | last post by:
Hi - I have a really strange problem - straight forward login code (on the event of a button press). Works perfectly locally - but when I upload to my host, I get the message and stack shown...
10
by: John Kraft | last post by:
Hello all, I'm experiencing some, imo, strange behavior with the StreamReader object I am using in the code below. Summary is that I am downloading a file from a website and saving it to disk...
3
by: Anup Daware | last post by:
Hi Group, I am facing a strange problem here: I am trying to read xml response from a servlet using XmlTextWriter. I am able to read the read half of the xml and suddenly an exception:...
13
by: Jen | last post by:
One user of my application is experiencing an exception "input string not in correct format". But it makes no sense where it is occurring. It is occurring when a string from a textbox ("172") is...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.