Home AMX User Forum NetLinx Studio
Options

length_array() issue

Gday all,

I've noticed some "interesting" behavior with length_array(), which had me baffled for several days. It doesn't seem to like constant strings. If it is called several times on the same constant string, the master locks up (including bringing down comms to the master). I've been too familiar with the PRD switch & power plug while trying to figure out this issue.

An example:
PROGRAM_NAME='LengthArray test'
DEFINE_DEVICE
dvRelay = 5001:8:0
dvTp = 128:1:0

DEFINE_CONSTANT
//char cDelimiter[] = ';'  //Can lock up the master

DEFINE_VARIABLE
char cText[50]
char cDelimiter[] = ';'  //Works fine.
//constant char cDelimiter[] = ';'  //Can lock up the master

DEFINE_START
DEFINE_EVENT
button_event[dvTp,1]{
 push:{
  if(length_array(cText) > 0){
   wait 10 'test'{
    //Works fine.
//    cText = left_string(cText, length_array(cText) - 1)
    
    //Can lock up the master if cDelimiter is a constant string.
    cText = left_string(cText, length_array(cText) - length_array(cDelimiter))
    
    send_command dvTp, "'!T',1,cText"
   }
  }
 }
}

button_event[dvTp,2]{  //Restore
 push:{
  cText = 'The quick brown fox jumps over the lazy dog.'
  send_command dvTp, "'!T',1,cText"
 }
}
DEFINE_PROGRAM

When using a constant string & the truncate button [dvTp,1] is more than once, the master locks up. When using a non-constant string the master runs without issue.

So I guess my question is:
Is it bad practice to use length_array() on a constant string, or is it a bug with the compiler/master firmware?
I've sinced changed the offending line to something that works, but I thought others may be interested in this issue.

I'm using:
NI-3100 v3.13.339 / v1.12.140
NLS build 2.5.0.163 (NetLinx compiler 2.3.0.0)

Yours,
Roger

Comments

  • Options
    Joe HebertJoe Hebert Posts: 2,159
    I?ve never been exactly clear as to what the difference is but if you force the constant string literal into a constant char array, Netlinx will not die such a brutal death with LENGTH_ARRAY. So if you change:

    This:
    DEFINE_CONSTANT
    char cDelimiter[] = ';' //Can lock up the master ? and will

    To this:
    DEFINE_CONSTANT
    char cDelimiter[] = {';'} //Will not lock up the master

    Then life will be good.

    Thanks for the insight. I didn?t know Netlinx could be brought to its knees in this scenario, doesn?t seem right to me.
  • Options
    Joe HebertJoe Hebert Posts: 2,159
    And to answer your question, I don?t do a LENGTH_ARRAY on a CONSTANT since it will never change at runtime. Instead I do this:

    DEFINE_CONSTANT

    //Queue constants
    CHAR cQDelim[3] = '~!~' //delimiter for queues
    INTEGER nQDelimLen = 3

    And then I just use nQDelimLen instead of LENGTH_ARRAY(cQDelim) I don?t think your way is bad practice, I?m just lazy and don?t like to type more than I have to. :)
    annuello wrote:
    So I guess my question is:
    Is it bad practice to use length_array() on a constant string, or is it a bug with the compiler/master firmware?
  • Options
    Array of constants.
    Joe Hebert wrote:

    DEFINE_CONSTANT
    char cDelimiter[] = {';'} //Will not lock up the master

    Then life will be good.

    I have alway put braces around character arrays that were defined as constants. This is the way it was taught in the AMX programming classes.

    I guess we now know why.
    DEFINE_CONSTANT
    
    CHAR cTEXT_ARRAY[4][] =
    {
      {'Text1'},
      {'Text2'},
      {'Text3'},
      {'Text4'}
    }
    
  • Options
    Single character strings AGAIN

    This appears to be yet another instance of the good old "not coping with single character strings" problem. Treat single character strings with enormous respect.
  • Options
    Joe HebertJoe Hebert Posts: 2,159
    This appears to be yet another instance of the good old "not coping with single character strings" problem.
    Right on target Mark. If there is more than one character you can dump the braces and the master will not lock up. This falls into the category of things that make you want to go hmmm...
  • Options
    annuelloannuello Posts: 294
    Old thread, I know.... I was banging my head again while trying to do some html decoding. Here's one to try at home...
    send_string 0, "'Normal String length is: ',itoa(length_array('Normal String'))"
    send_string 0, "'Greater-than length is: ', itoa(length_array('>'))"
    send_string 0, "'Ampersand length is: ',    itoa(length_array('&'))"
    
    **sigh** Two hours to track down what I was doing wrong. Note to self: It doesn't matter how the string inside length_array() is declared - a constant of any flavour will bite you.
    For the record I'm using NLS v3.0.0.315 on v3.50.430 firmware.

    Roger McLean
    Swinburne University
Sign In or Register to comment.