The following warnings occurred:
Warning [2] count(): Parameter must be an array or an object that implements Countable - Line: 906 - File: showthread.php PHP 7.2.24-0ubuntu0.18.04.15 (Linux)
File Line Function
/showthread.php 906 errorHandler->error




Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Array elements crashing program [SOLVED]
#1
I'm trying to write a simple path finding algorithm, and I've run into a problem with cbot arrays that I can't seem to work around:

I'm trying to create a 2d array representing the map at a 1m scale, so the array is nominally 808 by 808 elements.

If I try to initialize all the elements to some default value, Colobot GE slows to a crawl and I eventually have to kill the process.

Since Colobot appears to support sparse arrays, it seemed like a good idea to initialize only those array elements that the simple pathfinder actually needed to access (typically just a few hundred). The problem with THAT is that I can't find any way at all to test if an array element has been initialized.

Consider the following simple example program:

Code:
extern void object::artest()
{
    errmode(0); //This suppresses the "Variable not Initialized" error message, but doesn't stop the script from halting
    
    message( "Hello World" );
    int foo[]; // sizeof(foo) == 0
    
    foo[10] = 42; // sizeof(foo) == 11
    
    if( foo[0] == 0 ) {
        message( "This never happens" );
    }
    else {
        message( "This also never happens" );
    }
    
    message( "You won't see this message" );
}

So "sizeof(foo)" is no help.

"if( foo[0] == null )" fails to compile

EDIT:

While messing about with something unrelated, I stumbled upon a  solution using CBot's largely undocumented try-catch syntax. The following program seems to run as expected:

Code:
extern void object::artest()
{
  errmode(0); //This suppresses the "Variable not Initialized" error message, but doesn't stop the script from halting

  message( "Hello World" );
  int foo[]; // sizeof(foo) == 0

  foo[10] = 42; // sizeof(foo) == 11

  for( int i = 0; i < sizeof(foo); i++ ) {
    int v = SafeRead( foo, i );
    message( "foo[" + i + "] = " + v );
  }

  message( "Program finished" );
}

int object::SafeRead( int[] ar, int n ) {
    int defaultvalue = -99;
    int rv;
    
    try {
        rv = ar[n];
    }
    catch( CBotErrNotInit ) {
        message( "SafeRead() Caught Not-Initialized Error, returning default", DisplayError );
        return defaultvalue;
    }
    catch( CBotErrOutArray ) {
        message( "SafeRead() Caught Out-of-Bounds Error, returning default", DisplayError );
        return defaultvalue;
    }
    
    return rv;
}


My SafeRead() function isn't as generic as I'd like, but it should be trivial to adapt for use in most situations where something like this is needed.


Messages In This Thread
Array elements crashing program [SOLVED] - by DomesticAnt - 03-20-2018, 02:40 AM
RE: Array elements crashing program - by Smok - 03-20-2018, 08:29 PM
RE: Array elements crashing program - by Smok - 03-20-2018, 09:19 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)