I have a function for splitting a string. This function returns a basic array which is allocated inside the function The code for this function is
string mySplitString(string s, string sep)[]{
int offset=0, len=0, i=0, pos=0
//evaluate the length beforehands...
while (findPlainText(s[pos:], sep, offset, len, true)){
pos += offset+len
i++
}
//....for a late allocation
string ret[i+1]
i=0; pos=0
while (findPlainText(s, sep, offset, len, true)){
ret[i++] = s[0:offset-1]
s = s[offset+len:]
}
ret[i++]=s[0:] //add the last one
return ret
}
Now the trouble... Could someone explain to me why this code
string aU[]
string aL[]
aU=mySplitString("A/B/C","/")
aL=mySplitString("a/b/c","/")
print aU[0] aU[1] aU[2] "\n"
print aL[0] aL[1] aL[2] "\n"
fails and this other (just a bare change of statements order)
string aU[]
string aL[]
aU=mySplitString("A/B/C","/")
print aU[0] aU[1] aU[2] "\n"
aL=mySplitString("a/b/c","/")
print aL[0] aL[1] aL[2] "\n"
works? The first code would print =>abc =>abc The last code would print =>ABC =>abc pvselex - Thu Dec 17 03:54:25 EST 2015 |
Re: Basic array allocation in function It a bit technical... so here's the simple version. Your array is allocated in memory on the 'stack' in a frame reserved for the function. When the function ends, the frame is un-reserved and will be overwritten by any new variables. You can't return an array from a function and expect it not to get mangled. Read up on how stacks and heaps work for details. |
Re: Basic array allocation in function Ok. I knew it after all. My programming skills got really rusty after a long time of inactivity ;-) Then my question is, In DOORS DXL, are you only supposed to pass a basic array as a parameter to a function? Shouldn't be some way in DXL to dynamic allocate in the caller memory space from within the called function?
Needless to say that I solved my problems with a DXL Array and a sllight modification of the function which now reads int mySplitString(Array ar, string s, string sep)
|
Re: Basic array allocation in function pvselex - Thu Dec 17 05:47:36 EST 2015 Ok. I knew it after all. My programming skills got really rusty after a long time of inactivity ;-) Then my question is, In DOORS DXL, are you only supposed to pass a basic array as a parameter to a function? Shouldn't be some way in DXL to dynamic allocate in the caller memory space from within the called function?
Needless to say that I solved my problems with a DXL Array and a sllight modification of the function which now reads int mySplitString(Array ar, string s, string sep)
By the way: The answer to your original question was: NEVER return a simple array from a function!
string mySplitString(string s, string sep)[]{
...
}
DXL takes the syntax, but the array is deallocated after the function ends, since it is only valid inside the scope of the function (stack). And from returning a deallocated array all kinds of weird things can happen. The catch here is, that when you do not use the stack a lot, the array contents are still intact in memory and the code seems to work, but it breaks down when the stack memory gets reused and overwritten. Regards, Mathias |
Re: Basic array allocation in function Mathias Mamsch - Thu Dec 17 06:38:47 EST 2015 By the way: The answer to your original question was: NEVER return a simple array from a function!
string mySplitString(string s, string sep)[]{
...
}
DXL takes the syntax, but the array is deallocated after the function ends, since it is only valid inside the scope of the function (stack). And from returning a deallocated array all kinds of weird things can happen. The catch here is, that when you do not use the stack a lot, the array contents are still intact in memory and the code seems to work, but it breaks down when the stack memory gets reused and overwritten. Regards, Mathias Ok, got that Thanks to both I guess DXL can only allocate in local memory |