Code before a line that Crashes doesn't execute

This is a discussion on Code before a line that Crashes doesn't execute within the C++ Programming forums, part of the General Programming Boards category; I use cout<<"something"; to find a location as to where the program is crashing. However, they don't print even though ...

  1. #1
    Registered User Diamonds's Avatar
    Join Date
    Oct 2002
    Posts
    68

    Code before a line that Crashes doesn't execute

    I use cout<<"something"; to find a location as to where the program is crashing. However, they don't print even though the debugger says the program crashes at a line after the cout<<"something";

    Code:
    #include <iostream.h>
    #include <string.h>
    #include <stdlib.h>
    #include <time.h>
    
    void crossProduct (int **matrixA, int am, int an, int **matrixB, int bm, int bn, int **matrixC) {
    	int x= 0;
    	cout<<"something2";
    	int temp;
    	
    	for (int i = 0; i<am; i++)
    		for (int j = 0;j<an; i++) {
    			for (int k =0;k<bm;k++)
    				for (int l=0;l<bn;l++) {
    					cout<<"x:"<<x<<" ";
    					x++;
    					temp = matrixB[k][l] * matrixA[i][j] + temp;
    				}
    			matrixC[i][j] = temp;
    			temp = 0;
    		}
    
    }
    
    int main() {
    	int **p1, **p2, **p3;
    	int i;
    	int sizeX =3;
    	int sizeY =3;
    	p1 = new int*[sizeX];
    	p2 = new int*[sizeX];
    	p3 = new int*[sizeX];
    	for (i = 0; i < sizeX; ++i) {
    		p1[i] = new int[sizeY];
    		p2[i] = new int[sizeY];
    		p3[i] = new int[sizeY];
    	}
    //////////////////////////////////////////////////////
    	initMatrix(p1,sizeX, sizeY);
    	initMatrix(p2,sizeX,sizeY);
    	printMatrix(p1,sizeX,sizeY);
    	printMatrix(p2,sizeX,sizeY);
    	cout<<"something1";
    	crossProduct(p1,sizeX,sizeY,p2,sizeX,sizeY,p3);
    	printMatrix(p3,sizeX,sizeY);
    
    ...something down here...
    Where initMatrix fills it with values, and printMatrix does exactly that.
    The debugger is telling me that the program crashes at:
    Code:
    					temp = matrixB[k][l] * matrixA[i][j] + temp;
    However, something1 and something2 don't print. If I comment out
    Code:
    //	crossProduct(p1,sizeX,sizeY,p2,sizeX,sizeY,p3);
    like so, the program runs and doesn't crash.

    This is very strange...

  2. #2
    Registered User Diamonds's Avatar
    Join Date
    Oct 2002
    Posts
    68
    found it

    in the 4 for loops the j loop was incrementing i...

    but why would the program not print anyways?

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    because the stream is buffered and not necessarily flushed on a crash!

    add either endl or flush to the end of your couts.

    cout<<"debug message"<<endl;
    cout<<"debug message"<<flush;

    1st will flush and add a /n. second will just flush.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Imposing Line Numbers automatically in the C code
    By cavestine in forum C Programming
    Replies: 14
    Last Post: 10-15-2007, 12:41 AM
  2. help again with scrolling without wrapping
    By Dukefrukem in forum C Programming
    Replies: 8
    Last Post: 09-21-2007, 12:48 PM
  3. Need help on code to execute the program
    By hibmem10 in forum C++ Programming
    Replies: 6
    Last Post: 12-24-2005, 12:42 PM
  4. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 01:19 AM
  5. Greenhand want help!
    By leereg in forum C Programming
    Replies: 6
    Last Post: 01-29-2002, 05:04 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21