Finding a string variable in memory??
-
Hi there, If i assign a value to a string, is it possible to go and see where it is stored in the memory and what the value is? e.g.
string _testString = "Value I want to read from memory";
Any way to do this?
-
Hi there, If i assign a value to a string, is it possible to go and see where it is stored in the memory and what the value is? e.g.
string _testString = "Value I want to read from memory";
Any way to do this?
diePopster wrote:
see where it is stored in the memory
Why?
only two letters away from being an asset
-
Hi there, If i assign a value to a string, is it possible to go and see where it is stored in the memory and what the value is? e.g.
string _testString = "Value I want to read from memory";
Any way to do this?
Yes, but it is not recommended. If you have a requirement to do this you should really be using C++. This is a quickie example of manipulating a string by getting it's pointer (char*)
/* Will need to check 'Allow unsafe code' in
Project|Properties|Build */string text = "Test";
unsafe
{
fixed (char* textPointer = text)
{
char* pointer = textPointer;
*pointer = Char.ToLower(*pointer);
*++pointer = Char.ToUpper(*pointer);
*++pointer = Char.ToUpper(*pointer);
*++pointer = Char.ToUpper(*pointer);
}
}
// text will now be "tEST"Dave
Generic BackgroundWorker - My latest article!
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Why are you using VB6? Do you hate yourself? (Christian Graus) -
Hi there, If i assign a value to a string, is it possible to go and see where it is stored in the memory and what the value is? e.g.
string _testString = "Value I want to read from memory";
Any way to do this?
On top of everything else that's been said, using managed code, C#, VB.NET, or any other language that targets the .NET Framework, an item can be moved in memory by the Garbage Collector at any time. So, even though you got the pointer to item, you really cannot use the pointer because the item might not be there anymore. The pointer you have will NOT get updated to reflect the new location in memory. However, there is a way aroudn that. It's called "pinning". The GC can be told to pin an object in memory so it can't be moved. Read up on it here[^]. Be careful. When you pin an object, you are also responsible for removing the pin when you're done with it.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008
But no longer in 2009...