Welcome to Telelogic Product Support
  Home Downloads Knowledgebase Case Tracking Licensing Help Telelogic Passport
Telelogic DOORS (steve huntington)
Decrease font size
Increase font size
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
Search Topic Search Topic
Topic Tools Topic Tools
Quick Reply Quick Reply
Subscribe to this topic Subscribe to this topic
E-mail this topic to someone. E-mail this topic
Bookmark this topic Bookmark this topic
View similar topics View similar topics
View topic in raw text format. Print this topic.
 13-Aug-2007 18:18
User is offline View Users Profile Print this message


Chris Jones

Posts: 177
Joined: 1-Jul-2005

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
Report this to a Moderator Report this to a Moderator
 13-Aug-2007 18:37
User is offline View Users Profile Print this message


Chris Jones

Posts: 177
Joined: 1-Jul-2005

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.
Report this to a Moderator Report this to a Moderator
 13-Aug-2007 20:52
User is offline View Users Profile Print this message


David Pechacek

Posts: 674
Joined: 5-Dec-2006

I believe that kind of behavior is correct.

-------------------------
David Pechacek
AAI Services Textron
dpechacek@sc-aaicorp.com
David.Pechacek@gmail.com
Report this to a Moderator Report this to a Moderator
 13-Aug-2007 23:16
User is offline View Users Profile Print this message


Louie Landale

Posts: 2070
Joined: 12-Sep-2002

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
Report this to a Moderator Report this to a Moderator
 14-Aug-2007 00:38
User is offline View Users Profile Print this message


Chris Jones

Posts: 177
Joined: 1-Jul-2005

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... So, perhaps, the reference is implemented somehow as the address of the other int, and usually gets interpreted as a regular int...but it's handled a little differently when used as the key of a Skip?

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
Report this to a Moderator Report this to a Moderator
 14-Aug-2007 09:39
User is offline View Users Profile Print this message


Peter Albert

Posts: 232
Joined: 30-Dec-2005

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
Report this to a Moderator Report this to a Moderator
 14-Aug-2007 16:55
User is offline View Users Profile Print this message


Richard Good

Posts: 152
Joined: 22-Mar-2005

I think you wanted "int &count" not "int& count". I expect the number is a memory address

-------------------------
Regards,

Richard Good
Report this to a Moderator Report this to a Moderator
 14-Aug-2007 17:01
User is offline View Users Profile Print this message


Chris Jones

Posts: 177
Joined: 1-Jul-2005

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!
Report this to a Moderator Report this to a Moderator
 14-Aug-2007 18:58
User is offline View Users Profile Print this message


Louie Landale

Posts: 2070
Joined: 12-Sep-2002

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
Report this to a Moderator Report this to a Moderator
 16-Aug-2007 16:04
User is offline View Users Profile Print this message


Douglas Zawacki

Posts: 58
Joined: 17-Oct-2006

 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".


Report this to a Moderator Report this to a Moderator
 17-Aug-2007 10:15
User is offline View Users Profile Print this message


Tony Goodman

Posts: 1098
Joined: 12-Sep-2002

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
Report this to a Moderator Report this to a Moderator
 17-Aug-2007 16:21
User is offline View Users Profile Print this message


Douglas Zawacki

Posts: 58
Joined: 17-Oct-2006

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.
Report this to a Moderator Report this to a Moderator
Statistics
20925 users are registered to the Telelogic DOORS forum.
There are currently 2 users logged in.
The most users ever online was 15 on 15-Jan-2009 at 16:36.
There are currently 0 guests browsing this forum, which makes a total of 2 users using this forum.
You have posted 0 messages to this forum. 0 overall.

FuseTalk Standard Edition v3.2 - © 1999-2009 FuseTalk Inc. All rights reserved.