Topic
  • 5 replies
  • Latest Post - ‏2014-03-07T22:36:52Z by Mathias Mamsch
GerhardS
GerhardS
55 Posts

Pinned topic Skip of string arrays?

‏2014-02-27T08:54:20Z |

Hello,

I'm just struggling with a skip list which should have string arrays are content, i.e. something like this:

string strValues[5] = {"a", "b", "c", "d", "e"};
Skip skpList = create();

put(skpList, "array1", strValues);

string strResult[];

for strResult in skpList do
{
    print strResult[0] "\n"
}

When doing this in the DXL Editor, it's working fine, showing "a" as result.

However, having this in the production code (well, a bit more complex, as the skip list is filled and returned by another function), this leads to a DOORS crash. When checking the size of the "strResult" array, it shows something like "670412624", and the values are not valid.

string strTemp = "";
string strValues[];
int     intCount = 0;
                
for strValues in pSkpTypes do
{
        strTemp = (string key(pSkpTypes));
        
        debugPrint("Enumeration for: " strTemp " size: " sizeof(strValues) "");
                        
        for (intCount = 0; intCount < sizeof(strValues); intCount++)
        {
                debugPrint("--- value: " strValues[intCount] "");   
        }
}

I suspect the strResult[] declaration as problem, and the assignment by the "for" loop.

Do you have an idea what's wrong? As I require the string arrays (having different sizes), and the collection of them, can you think of an alternative?

Best regards,

Gerhard

  • David_G_Bond
    David_G_Bond
    55 Posts

    Re: Skip of string arrays?

    ‏2014-02-27T15:14:34Z  

    The first thing I notice is that the Skip list is defined with a string key, but you use create() to create it. If the key is a string you need to use createString()

     

    - David Bond

  • llandale
    llandale
    3005 Posts

    Re: Skip of string arrays?

    ‏2014-02-28T18:29:21Z  

    Yes, use "createString" when the "key" is "string"; it is never a problem if you always do that.  But IIRC in this particular case since all the "key" values are inserted as string constants; it accidentally works in your example.

    As for your problem .. well I cannot articulate it correctly, but you cannot use an "array" (lower case "a") as a container, hoping to change the address via the skip loop.  You can with an "Array" (upper case "A", which is a completely different construct).  In this case your skip loop assigns the 1st value of the "array" in the Skip to variable "strValues", which is declared at size zero.  The Skip loop does not assign the address of the embedded array to strValues.

    In a related problem, if you declare an "array" inside a function, the space is reserved on the stack which is re-used when the function ends; which means if you attempt to "preserve" the "array", such as insert into a Skip, random results occur in the next function which resets that area of the stack that your "array" still points to.

    The reality of DXL is that "array"s that are to be used by more than one sibling function, must be declared in the main context at a suitable size.  That is in practice often unrealistic since the size of the array is typically determined at run time (such as the number of Enumerations in a Type).  I once proved the concept of declaring some huge array in the main context and then "aliasing" the dynamic array to the next available location, keeping track of how much of the global was still available.  It was incomprehensible, but it worked.

    Forget it, you can routinely replace the "array" with a "Skip", unless you figure to use the "array" in some kind of listView on some Dialog; in which case the space for it must persist; meaning it is in the main context.  Programming that is very awkward.

    -Louie

  • GerhardS
    GerhardS
    55 Posts

    Re: Skip of string arrays?

    ‏2014-03-02T12:11:26Z  
    • llandale
    • ‏2014-02-28T18:29:21Z

    Yes, use "createString" when the "key" is "string"; it is never a problem if you always do that.  But IIRC in this particular case since all the "key" values are inserted as string constants; it accidentally works in your example.

    As for your problem .. well I cannot articulate it correctly, but you cannot use an "array" (lower case "a") as a container, hoping to change the address via the skip loop.  You can with an "Array" (upper case "A", which is a completely different construct).  In this case your skip loop assigns the 1st value of the "array" in the Skip to variable "strValues", which is declared at size zero.  The Skip loop does not assign the address of the embedded array to strValues.

    In a related problem, if you declare an "array" inside a function, the space is reserved on the stack which is re-used when the function ends; which means if you attempt to "preserve" the "array", such as insert into a Skip, random results occur in the next function which resets that area of the stack that your "array" still points to.

    The reality of DXL is that "array"s that are to be used by more than one sibling function, must be declared in the main context at a suitable size.  That is in practice often unrealistic since the size of the array is typically determined at run time (such as the number of Enumerations in a Type).  I once proved the concept of declaring some huge array in the main context and then "aliasing" the dynamic array to the next available location, keeping track of how much of the global was still available.  It was incomprehensible, but it worked.

    Forget it, you can routinely replace the "array" with a "Skip", unless you figure to use the "array" in some kind of listView on some Dialog; in which case the space for it must persist; meaning it is in the main context.  Programming that is very awkward.

    -Louie

    Thanks for your explanation, Louie. It seems to me that I have to use a different approach, perhaps using skip of DxlObjects. I'm working with attribute enumeration types, where you need the arrays, so it would have been pretty straight forward to have a skip list of arrays. 

    - Hardy

  • pommCannelle
    pommCannelle
    80 Posts

    Re: Skip of string arrays?

    ‏2014-03-06T14:25:42Z  
    • GerhardS
    • ‏2014-03-02T12:11:26Z

    Thanks for your explanation, Louie. It seems to me that I have to use a different approach, perhaps using skip of DxlObjects. I'm working with attribute enumeration types, where you need the arrays, so it would have been pretty straight forward to have a skip list of arrays. 

    - Hardy

    ( or you can use the original container for your data ...
    For example, a skip is not needed to store module types, the module have the info and access it is as simple as access to a skip list
    -> for attributeType in myModule do  ...
    And you don't need a skip to store values of a type
    -> for i in 0:(attributeType.size-1) do print attributeType.strings[i] "\n"
    Well ... nice skip feature, like find, is not available in this case ... but perhaps to developp it is not so hard ;) )
     

  • Mathias Mamsch
    Mathias Mamsch
    2003 Posts

    Re: Skip of string arrays?

    ‏2014-03-07T22:36:52Z  

    See here for a discussion on the probable problem and some more solutions:

    https://www.ibm.com/developerworks/community/forums/html/topic?id=77777777-0000-0000-0000-000014267491&ps=25

    By putting arrays to skips you probably have the same problem as when trying to returning an array, especially when you create the array inside a function. That is the probable reason why your example code works, but your production code is not. The example code has the array declared on global level.

    Regards, Mathias