473,503 Members | 1,700 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C matrix does funny things

6 New Member
I once had a matrix working like this

matrix[20][2]

So, since its indexed with 0, I used them as matrix[i][0] or matrix[i][1].

Then the other day, I forgot the 0 index, and used matrix[i][2].

And it worked perfectly fine! And worse so, the program didn't work anymore when I revert that back to matrix[i][1]. Also matrix[i][0] now had ridiculous values inside.

More importantly, there are other places in the code in which the matrix works with matrix[i][0 or 1]. THE SAME MATRIX.

Its just this particular segment that has gone haywire.

I am using mingw latest version, and no the matrices are not passed by reference to anything or anywhere. And they are all initialized to all 0 values by a loop at the start of the code. The matrices are double type.

My program is working fine, after about half an hour of randomly placing 0s, 1s and 2s somehow its working very fine. But I am afraid it would go bonkers again on another machine.

Does anyone know whats happening? Is it my compiler or my coding?
Thanks!
Jun 26 '08 #1
1 1421
weaknessforcats
9,208 Recognized Expert Moderator Expert
All the indexes do is perform a pointer arithmetic calculation. If the indexes are inside your array you get good results. If they aren't, you get bad results since you have corrupted memory outside the array bounds.

Read this article: http://bytes.com/forum/thread772412.html.

Post again of your still have questions.
Jun 26 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

5
3785
by: Jason | last post by:
Hello. I am trying to learn how operator overloading works so I wrote a simple class to help me practice. I understand the basic opertoar overload like + - / *, but when I try to overload more...
15
13246
by: christopher diggins | last post by:
Here is some code I wrote for Matrix multiplication for arbitrary dimensionality known at compile-time. I am curious how practical it is. For instance, is it common to know the dimensionality of...
16
3232
by: Martin Jørgensen | last post by:
Hi, I've made a program from numerical recipes. Looks like I'm not allowed to distribute the source code from numerical recipes but it shouldn't even be necessary to do that. My problem is...
20
5184
by: Frank-O | last post by:
Hi , Recently I have been commited to the task of "translating" some complex statistical algorithms from Matlab to C++. The goal is to be three times as fast as matlab ( the latest) . I've...
2
9335
by: jjiyunlee | last post by:
Hi, I'm new to C (and programming in general), and I have to say that this site has helped me learn a great deal about C (thanks everybody!!). I've looked through several discussions specifically...
2
8544
by: DarrenWeber | last post by:
Below is a module (matrix.py) with a class to implement some basic matrix operations on a 2D list. Some things puzzle me about the best way to do this (please don't refer to scipy, numpy and...
5
9689
by: adam.kleinbaum | last post by:
Hi there, I'm a novice C programmer working with a series of large (30,000 x 30,000) sparse matrices on a Linux system using the GCC compiler. To represent and store these matrices, I'd like to...
5
9616
by: Anolethron | last post by:
Wrong one: void minptr (int *matrix, int rows, int columns,int *min){ int i=0,j=0; *min=*matrix; //!!!!!!!!!!!!!!!!! for (i=0; i < rows; i++) { for (j=0; j < columns; j++) { if( *min...
6
7434
by: lovecreatesbea... | last post by:
Hello experts, I code an function to rotate a matrix by 90 degrees clockwise. The matrix can be in any size provided its length equals to width. The one minor limitation is that this requires an...
0
7201
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
7083
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
7278
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,...
1
6988
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
7456
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
3166
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3153
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1510
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
379
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.