![]() |
Telelogic DOORS (steve huntington) | ![]() |
new topic :
profile :
search :
help :
dashboard :
calendar :
home
|
||
Latest News:
|
|
Topic Title: Pass by reference int as key for Skip? Topic Summary: Odd behaviour Created On: 13-Aug-2007 18:18 Status: Post and Reply |
Linear : Threading : Single : Branch |
![]() |
![]()
|
![]() |
|
I have a function that makes a Skip list of all the baselines in a module. It also has a reference parameter for the number of baselines (see code). It assigns the count (reference int) as the key for the Skip, and later I go through the baselines and do stuff with them.
The problem is that it doesn't seem to actually put the value of the int for the key, but rather the address??? And since the address doesn't change, it only puts the first baseline into the Skip. I ran it with startDXLTracing_ enabled around it, and inside the get_baselines function, it gave "count = sp+23" whenever count was used.....but later, in the calling context outside the function, when I loop through the Skip list, it says the same thing---not a literal integer like I would have expected (I am presuming that "sp" means "stack pointer" or something similar). Is this expected behaviour? It seems kind of weird to me but it might be another one of those basic CS principles that I'm missing again ![]() Chris Edited: 13-Aug-2007 at 18:19 by Chris Jones |
|
![]() |
|
![]() |
|
Also...I forgot to mention that replacing the use of "count" in that function with a "temp_count" (which is declared inside of it) fixes the issue. So it's mostly curiosity now.
|
|
![]() |
|
![]() |
|
I believe that kind of behavior is correct.
------------------------- David Pechacek AAI Services Textron dpechacek@sc-aaicorp.com David.Pechacek@gmail.com |
|
![]() |
|
![]() |
|
I don't understand it, but I've seen problems like this before. It has to do with a function using a call parameter (count) as if it were a local parameter (you increment it in a loop). I modified your code declaring 'int count1 = 0' and using count1 in the loop, then setting count=count1 at the bottom. Attached. That seemed to clear up the problem.
I hope someone can explain it, but all my functions that use output call parameters must declare a local 'stageing' parameter, and assign the output parameter to the stage value before ending the function. That is, ALL output parameters are calculated like this: void DoIt(int &Count) { int Stage = calculate the results Count = Stage return } - Louie |
|
![]() |
|
![]() |
|
Louie:
Your "count1" idea is like the workaround I found. I never noticed problems with reference variables before (when just using them as ordinary variables), but now when I try to use it for an index to the Skip..... David: Can you explain that with any more details? Ramble: The DXL manual says of Skips, "The keys used with the skip list can be of any type. However, comparison of keys is based on the address of the key, not its contents. This is fine for elements that are always represented by a unique pointer, for example, objects, modules, or skip lists, but care is needed with strings. This is because a string may not have a unique address, depending on whether it is literal or a computed string stored in a variable. " I thought that I had it figured out after reading that more closely, but now I am only more confused. How then does it handle using a regular int (versus a reference to one)? The address of a local int does not change any more than that of a reference int... ![]() I tried typecasting it in the put statement (like put(baselines, (int count), b)) but I couldn't get it to run. It said, "incorrect use of identifier (count)." I also tried count int --- both of these are listed in the DXL manual as casts. Still confused. <shrug> Chris |
|
![]() |
|
![]() |
|
No explanation, sorry, but at least a simple workaround: If you replace the "put" statement with
bool hi = put(baselines, count+0, b) then all works as expected, as, I guess, the result of the calculation "count+0" is a proper integer which works as a skip key. Regards, Peter |
|
![]() |
|
![]() |
|
I think you wanted "int &count" not "int& count". I expect the number is a memory address
------------------------- Regards, Richard Good |
|
![]() |
|
![]() |
|
Peter:
Sure enough. Makes sense that that would guarantee a good int (though I still don't get why it wasn't before). Richard: Thanks for the idea --- I had already tried it though. Is it supposed to make a difference where the ampersand is? Thanks everybody for the help! |
|
![]() |
|
![]() |
|
I emphasise that this is a call-parameter-by-reference issue, not a Skip list issue. I get this sort of odd result whenever I use call-by-reference parameters in the body of the function.
I've always used "int &count", and have no idea "int& count" is really different or what it means if it is. Ideas? I had some of this stuff in College (right after they stopped using punch cards...), but went straight into Fortran at work and have never used it before DXL. - Louie |
|
![]() |
|
![]() |
|
Folks,
I believe the DXL Parser does not distinguish a difference between int& count and int &count. The count parameter is passed by reference. This means the address of the parameter (count) is passed on the stack. Therefore, the put function is actually putting the address of the count parameter in the skip list and not the value of count. This is why you are seeing a BIG number when pulling the key back out. When I tried to explictly cast the data type to an int: put(baselines,(int (count)),b) I received the error message: incorrect use of identifier (count). Unfortunately, the cast should have done the trick because a true cast should tell the interpreter to "Use the value at this address as the specified data type". |
|
![]() |
|
![]() |
|
There is a known defect with reference parameters in that they cannot be directly used inside the function. The workaround, as Louie suggested, is to use a local variable.
I always do this now, e.g. void func(int &i) { int local = 0 // intitialise i = 0 // do local stuff.... // set the parameter i = local } If you need to test the value of a reference parameter, then you need to assign it to a local variable, which does de-reference the pointer coorrectly, unlike the cast, e.g. int local = i if (local == 0) do stuff... ------------------------- Tony Goodman http://www.smartdxl.com Edited: 17-Aug-2007 at 10:16 by Tony Goodman |
|
![]() |
|
![]() |
|
Tony,
I'm not sure what you are saying. Is there an official defect logged from Telelogic regarding this issue? I believe the problem is actually in the put function itself...because the following code uses the parameter passed by reference correctly. I would like to understand exactly what you are saying so I can make sure my code doesn't have some bugs. |
|
![]() |
Telelogic DOORS
» DXL Exchange
»
Pass by reference int as key for Skip?
|
![]() |
FuseTalk Standard Edition v3.2 - © 1999-2009 FuseTalk Inc. All rights reserved.