Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Finding a string variable in memory??

Finding a string variable in memory??

Scheduled Pinned Locked Moved C#
performancequestion
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    diePopster
    wrote on last edited by
    #1

    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?

    N D D 3 Replies Last reply
    0
    • D diePopster

      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?

      N Offline
      N Offline
      Not Active
      wrote on last edited by
      #2

      diePopster wrote:

      see where it is stored in the memory

      Why?


      only two letters away from being an asset

      1 Reply Last reply
      0
      • D diePopster

        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?

        D Offline
        D Offline
        DaveyM69
        wrote on last edited by
        #3

        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)

        1 Reply Last reply
        0
        • D diePopster

          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?

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups