question

Patrick Zweekhorst avatar image
1 Like"
Patrick Zweekhorst asked Phil BoBo answered

Problem with tonum in module

Hi,

We are updating a module from FlexSim version 7 to FlexSim 17.0.1. We have a usercommand in FlexSim which calls a dll function. This dll function returns a pointer to a node as double (return tonum( nodeX ) ). However, the value I get back from the usercommand is a node. This results in some problems because other pieces of code assume it is a double and not a pointer. How can we ensure that the returned value is a double? Is there a bug in the tonum function or should we do something else?

Thanks in advance,

Patrick

PS: It was not possible to select software version FlexSim 17.0.1 in the dropdown

FlexSim 17.0.0
modulesusercommanddll functions
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

1 Answer

Phil BoBo avatar image
0 Likes"
Phil BoBo answered

Storing pointers to objects as a number stores their memory address on the node. This was fine in 32-bit because it was storing a 32-bit memory address on a double (53-bits of precision for the significand).

When we switched to 64-bit, this became a problem because now pointers have 64-bits of precision, but you are storing them on a double, which only has 53 bits of precision for the significand.

Therefore, if the memory address is larger than 53-bits, it will get truncated when you call tonum() on it, and it won't give back the correct address when you call tonode() on that truncated value.

To fix this, we implemented the Variant class, which can hold a pointer.

Update your code so that you don't store pointers in doubles.

5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.