Detect if two string references are identical

How does one tell if two string references refer to the same block of physical memory?

I need this ability for a system which computes memory usage of a running application using Introspection and Runtime.

They don’t
Strings are not objects in the sense they are a “pointer” to a memory space

What you indicate IS true for instantiated objects, but not for strings, double, integer, boolean etc variables

why not figure out a way to query the operating system instead… The memory footprint of a running application is not static, it changes as the program runs, and includes chunks of memory that Introspection may not even know about (invalidated space that has yet to be released or reused)

Behind the scenes, strings do act like objects so code like this:

dim s1 as string = "hi there"
dim s2 as string = s1

will create two references to the same string. As such, if you code carefully, you can be reasonably sure that if StrComp( s1, s2, 0 ) = 0, it’s the same string reference.

No Kem that does NOT create two reference to the same string…
It creates two strings that happen to have the same value.
but they are two instances.

If they were the SAME reference than

dim s1 as string = "hi there"
dim s2 as string = s1
s2="Good Bye"

would result in S1 also becoming “Good Bye”… which we know does not happen

REFERENCE and VALUE are not the same

[quote=334484:@Dave S]No Kem that does NOT create two reference to the same string…
It creates two strings that happen to have the same value.
but they are two instances.

If they were the SAME reference than

dim s1 as string = "hi there"
dim s2 as string = s1
s2="Good Bye"

would result in S1 also becoming “Good Bye”… which we know does not happen

REFERENCE and VALUE are not the same[/quote]
That depends on the implementation of the assignment. You cannot draw this conclusion just by looking at the code. There are programming languages implementing this “copy only on assignment”-thing.

Eli… what ever are you talking about…
Xojo strings are immutable…
String assignments create new copies.
So in the above example, S1 and S2 do not exist in the same address space,
and in my example, when S2 is set to “GOOD BYE” it no longer resides in the same address space the previous incarnation of S2 was.

How “other languages” may or may not handle a string, in not relevant to this topic.

When you look at the runtime objects when doing:

Dim obj1 As Class1 = New Class1() Dim obj2 As Class1 = obj1
… you will see that there is only one instance in the list in the debugger. I assume – at least in theory – that Xojo string work the same as class instances (pointer on the stack, data in the heap).

It probably depends of a lot of things. Strings might interned (at least short ones) or they might even be on the stack (local variables and if they are short), we do not know.

Not related to the question at hand.

We do not know that for sure. I would say: only if necessary a copy is created.

Of course they are in the same address space (if you mean by that the memory available to the app – or else you would have to explain what you mean by “address space”).

[quote=334477:@Thomas Sanham]How does one tell if two string references refer to the same block of physical memory?

I need this ability for a system which computes memory usage of a running application using Introspection and Runtime.[/quote]

You simply can’t.

But… What’s wrong with http://documentation.xojo.com/index.php/Runtime.MemoryUsed ?

… and that is the problem.

Dave’s example shows that the two string variables are different. Strings are NOT objects but scalars.

Strings are not scalars, they appear to be scalars in Xojo, but they are not.

[quote=334493:@Markus Winter]@Eli Ott I assume …
… and that is the problem.[/quote]
Dave assumes too… he does not know either, because we cannot know since we do not have access to memory regarding strings.

@Dave S Xojo strings are immutable…
Not related to the question at hand.

VERY MUCH RELATED TO THE QUESTION AT HAND, and it indicates how Xojo handles strings

@Dave S String assignments create new copies.
We do not know that for sure. I would say: only if necessary a copy is created.

WE DO (OR MOST OF US DO), See #1 above…

@Dave S S1 and S2 do not exist in the same address space
Of course they are in the same address space (if you mean by that the memory available to the app – or else you would have to explain what you mean by “address space”).

Same address space is the same sequential bytes of memory… so NO they are NOT in the same address space

And Eli… I am ASSUMING nothing… This is in FACT how it works…
Internally, yes Strings ARE objects, but you cannot INSTANTIATE them like an object. FolderItems are Object too, but also are also not “handled” the same.

A normal object consists of a POINTER, and when TWO variables REFERENCE the same object, then their POINTERS are equal… which is why changing a property on one, causes the other one to also change.
Since STRINGS do not operate that way, their “pointers” are not the same.

In “C” you could very easily do what you are saying, but then this is not “C”

Dave, I have no objection if it is like that, of course. But you do not know the internals – or have you worked on the LLVM compiler for Xojo, Inc.?

Seriously Eli? not worth responding anymore…
I have stated how it works, and unless Norman or Greg or someone from Xojo says “Dave, you are full of $%^#, and string variables are treated just like other classes in how they are references, and all the examples you posted above are totally false”, Then I have nothing more to add to this conversation. (Oh, and Eli? I doubt that is what they will say)

No. Dave has example code to back up his conclusion.

But in your case you have to explain the following:

Let’s look at the example:

Dim s1 as String = " hello"
Dim s2 as String = s1

Let us assume there would be just one memory location. Then you have a BIG problem now:

s2 = “World”

Why is s1 not “World”?

Why would the assignment of “World” to s2 not just change the value of s2 but create a new “object”???

Why would a string “object” behave completely different from any other object?

Your assumption would require a string variable to be able to both act as a real object and as a skalar, but how does it decide when it is which?

The assignment of s1 to s2 creates a new string for s2 and fills it with a copy of the VALUE of s1.

Anything else makes no sense.

Some programming languages have “interned strings”, which mean each different string is only existing once in the memory of the application. If there is a line producing a new string, the application first checks if that string already exists in memory, and the points to it instead of creating a new one.

Such languages implement “copy-on-write”, which means only at the very last moment the string is copied, namely when an assignment would lead to “co-change” a second string (pointer) pointing to the same string. It is an implementation thing. It can even be that the “old” Xojo compiler does it the way Dave says and the “new” using LLVM does copy-on-write.

All I was saying is that from looking at Xojo source code you cannot draw any conclusions about that. It is a black box. Norman or Greg would indeed have to clarify that, because only they know.

I only skimmed the thread, but have been told over the years that this is how it works (and my own tests confirm it).

s = "some string" // One string, one reference
s1 = s // One string, two references
s = "some other string" // s and s1 refer to different strings
s1 = s // Original string is gone, now one string, two references

You can confirm this yourself with by watching the memory footprint of this code:

dim s as string = "Assume this is a 1 MB string"
dim arr( 1000000 ) as string
for index as integer = 0 to arr.Ubound
  arr( index ) = s
next

To be clear, assigning one string to another in Xojo does not copy the string. Remember, strings are immutable so all you can do is assign another string. Because they are immutable, it wouldn’t make sense to make a copy for a simple assignment.

Dave, you are 100% wrong. Eli and Kem are correct. I have heard that from Xojo engineers.