Thread: if statements causing program to crash

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    7

    if statements causing program to crash

    My program crashes on runtime. It can compile, but crashes at .exe. But everything works, right up to until I run the following code:
    Code:
    cout<< "centroid y: "<<trialone.centroidy()<<endl;
    the method centroidy is defined as follows:
    Code:
    double EarthCalc::centroidy()
    {
        double firstmoment = 0.0;
        double mass1 = 0.0;
        double mass2 = 0.0;
        double count = 2159;
    
    
        for (int i = 0; i<colNum; i++ )
        {
            count--;
    
    
            for ( int j = 0; j<rowNum; j++ )
            {
                
                if ( array[i][j] > 0 )
                {
                    mass1 = mass1 + (LONDISTcalc(count/12.0 - 90)) *(LATDISTcalc(count/12.0 - 90))*(array[i][j])*2.5;
                }
                if ( array[i][j] < 0 )
                {
                    mass2 = mass2 + (LONDISTcalc(count/12.0 - 90) * LATDISTcalc(count/12.0 - 90) *array[i][j]);
                }
    
    
                firstmoment = firstmoment + (j/12.0 - 90)*LONDISTcalc(count/12.0 - 90)*LATDISTcalc(count/12.0 - 90);
            }
        }
        return (firstmoment/(mass1 + mass2));
    }
    Ive narrowed the problem down to the 2 if statements. For some reason, the if statements crash the program, even though I checked the code like 50 times and saw nothing wrong. I am accessing a huge array of like 18 MB, but still, I accessed that array before in other methods of the class as well. But it didnt crash. WTF?

    Anyways, I've also attached the 3 files with all the code as well. If anyone can help me, thanks.

    UPDATE: I fixed the code by deleting the if statements entirely. I used some of the other methods in the class to work around using ifs and only using the fors. But it would be still nice if someone can tell me why it crashed me
    Attached Files Attached Files
    Last edited by mantracker; 11-10-2012 at 08:23 PM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    if statements don't cause a program to crash. Mistakes by programmers do. Always.

    I haven't even bothered to look at the code. The fact you are "accessing a huge array" gives an indication of where your problem is.


    Look at the code that creates (or allocates) the arrays, and sets the values of rowNum and colNum. For an array access to work, the array must be successfully created with the right size (or sizes, since you are using two-dimensional indices) and the recorded dimensions need to be correct.

    If the array was created on the stack, it is quite possibly larger than the stack. That might be one cause. Similarly, if the array is created globally - it may be too large. Reduce the array dimensions (say to a 3x3 array) and see if the code runs. If so, you are exhausting capabilities of your host system. The only solution is to make the arrays smaller, and use some smarter (less lazy) technique for processing your data that does not require such large arrays.

    If you are using dynamic memory allocation (operator new in C++ or malloc() if you are using C techniques) to create the array, then you need to check that the allocation succeeded. If the allocation failed, then then you should not be accessing array elements.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    7
    Quote Originally Posted by grumpy View Post
    if statements don't cause a program to crash. Mistakes by programmers do. Always.

    I haven't even bothered to look at the code. The fact you are "accessing a huge array" gives an indication of where your problem is.


    Look at the code that creates (or allocates) the arrays, and sets the values of rowNum and colNum. For an array access to work, the array must be successfully created with the right size (or sizes, since you are using two-dimensional indices) and the recorded dimensions need to be correct.

    If the array was created on the stack, it is quite possibly larger than the stack. That might be one cause. Similarly, if the array is created globally - it may be too large. Reduce the array dimensions (say to a 3x3 array) and see if the code runs. If so, you are exhausting capabilities of your host system. The only solution is to make the arrays smaller, and use some smarter (less lazy) technique for processing your data that does not require such large arrays.

    If you are using dynamic memory allocation (operator new in C++ or malloc() if you are using C techniques) to create the array, then you need to check that the allocation succeeded. If the allocation failed, then then you should not be accessing array elements.
    If you look at some of my other methods in the class, you would notice that the array was accessed in the exactly same way: "If ( array[i][j] > 0 )", none of those caused me to crash, so that's what confused me. It is an array of size 2196x4000 something, but still you should be able to check it w/o pointers right?

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    You have your heart set on the belief that the problem is with the particular "if" statement, and don't want to accept advice that the problem occurs before that, eh?

    The fact that a statement ("if") or expression (array[i][j] > 0) causes a crash in one place, but (under similar conditions, such as the same values of i and j) doesn't cause a crash, is a sign that you're looking in the wrong spot.

    A common mistake for beginners, which you are making, is to believe that the code where a crash occurs is also the cause of the crash. In reality, any and call code code, and code interactions, executed before the crash occurs are potential contributors to it.

    But, okay, you can insist that the problem is with the if statement, and persist in looking there. It's your time to waste.

    When problems occur with arrays, the root cause is most often with dimensions (eg trying to access 10th element of a 3 element array) or with creation of the array (eg the system can't allocate all memory the array needs, so code which accesses array elements eventually - not immediately - fails).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Use a debugger!
    Code:
    $ g++ -g DTM.cpp DTM-driver.cpp
    $ gdb ./a.out 
    GNU gdb (Ubuntu/Linaro 7.3-0ubuntu2) 7.3-2011.08
    Copyright (C) 2011 Free Software Foundation, Inc.
    License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
    This is free software: you are free to change and redistribute it.
    There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
    and "show warranty" for details.
    This GDB was configured as "x86_64-linux-gnu".
    For bug reporting instructions, please see:
    <http://bugs.launchpad.net/gdb-linaro/>...
    Reading symbols from /tmp/a.out...done.
    (gdb) run
    Starting program: /tmp/a.out 
    Water percentage :-nan
    Water volume:0
    Highest elevation:0
    Lattitude highest: 2.96439e-323
    Longitutde highest: 3.11455e-317
    Deepest elevation: 0
    Lattitude lowest: 2.07378e-317
    Longitutde lowest: 0
    Average elev: 0
    Average above sea elev: -nan
    
    Program received signal SIGSEGV, Segmentation fault.
    0x000000000040195a in EarthCalc::centroidy (this=0x7fffffffe100) at DTM.cpp:188
    188				if ( array[i][j] > 0 )
    (gdb) print i
    $1 = 2176
    (gdb) print j
    $2 = 0
    Woah - 2176!?
    But you have
    static const int rowNum = 2160;

    A large number of your for loops are whacked.
    Code:
    $ grep 'for' DTM.cpp
      for( int i=0; i < columns; i++)
    	  for( int j=0; j < rows; j++ )
    	for (int i = 0; i<rowNum; i++ )
    		for(int j = 0; j<colNum; j++ )
    	for(int i = 0; i<rowNum; i++ )
    		for(int j = 0; j<colNum; j++ )
    	for(int i = 0; i<rowNum; i++ )
    		for(int j = 0; j<colNum; j++ )
    	for(int i = 0; i<rowNum; i++ )
    		for(int j = 0; j<colNum; j++ )
    	for( int i = 0; i<rowNum; i++ )
    		for( int j = 0; j<rowNum; j++ )
    	for ( int i = 0; i<rowNum; i++ )
    		for( int j = 0; j<rowNum; j++ )
    	for (int i = 0; i<colNum; i++ )
    		for ( int j = 0; j<rowNum; j++ )
    	for (int i = 0; i<colNum; i++ )
    		for ( int j = 0; j<rowNum; j++ )
    It's supposed to be row by col, not
    row by row
    or
    col by row
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array causing program to crash
    By rTeapot in forum C++ Programming
    Replies: 11
    Last Post: 07-03-2012, 10:40 AM
  2. Is my code causing my system to crash?
    By camel-man in forum C Programming
    Replies: 1
    Last Post: 04-14-2011, 05:54 PM
  3. fscanf causing a crash
    By dougwilliams in forum C Programming
    Replies: 6
    Last Post: 11-18-2007, 04:52 PM
  4. what is causing this seemingly simple app to crash?
    By Shadow12345 in forum C++ Programming
    Replies: 6
    Last Post: 12-06-2002, 08:36 PM
  5. allegro causing a crash
    By kooma in forum Game Programming
    Replies: 5
    Last Post: 04-06-2002, 02:01 PM