[C# Request] Fix a pointer over runtime menu

User Tag List

Results 1 to 8 of 8
  1. #1
    Frosttall's Avatar Active Member
    Reputation
    64
    Join Date
    Feb 2011
    Posts
    261
    Thanks G/R
    16/3
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    [C# Request] Fix a pointer over runtime

    Hey guys,

    As you know are pointers in C++ static and won't be moved in the physically memory because it's an unmanaged language. There wouldn't be a problem to read where the pointer points to, store it somewhere else and 5 minutes later would the pointer still point to the same memory location and we wouldn't run in any problems.

    I'm trying to achieve now the same with C#, but as it's a managed language does the CLR move the values in the memory as it wishes and the pointed location would change while runtime.
    To keep a value physically at the same location while the execution of some code you could use the fixed-Keyword and do your work inside the braces, but the CLR would be able to move the value again as soon as it leaves the code which is "fixed".

    My problem is that I want to work with that pointer at various locations and the using of the fixed-Keyword is not possible.

    Do you know a way to keep a value the whole runtime physically at the same location?


    Thank you!

    [C# Request] Fix a pointer over runtime
  2. #2
    Sychotix's Avatar Moderator Authenticator enabled
    Reputation
    1443
    Join Date
    Apr 2006
    Posts
    4,001
    Thanks G/R
    295/587
    Trade Feedback
    1 (100%)
    Mentioned
    10 Post(s)
    Tagged
    0 Thread(s)
    I have no idea what you are talking about...

    From what I can tell, you are asking how you can make a pointer point to the same area in memory throughout the entire execution. I don't think this is different in any language. Its just

    char *c = malloc(sizeof(char));

    Now whatever you store into the memory location allocated for c should be there forever, as long as no other piece of your code (or outside malicious code) changes that. Afaik, the address at which the pointer is stored should not change either. It wouldn't make sense for program to continually copy/move the data to different locations.

    EDIT: Forgot to add the exception that if you lose a reference to this location in memory, some languages include a garbage collector which will free the memory for you. I.E. Java/Python
    Last edited by Sychotix; 07-05-2012 at 11:52 AM.

  3. #3
    TOM_RUS's Avatar Legendary
    Reputation
    914
    Join Date
    May 2008
    Posts
    699
    Thanks G/R
    0/52
    Trade Feedback
    0 (0%)
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You can try GCHandle Structure with GCHandleType.Pinned. What kind of information your pointer points to?

    P.S. An instance with nonprimitive (non-blittable) members cannot be pinned this way.
    Last edited by TOM_RUS; 07-05-2012 at 11:56 AM.

  4. #4
    Frosttall's Avatar Active Member
    Reputation
    64
    Join Date
    Feb 2011
    Posts
    261
    Thanks G/R
    16/3
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by Sychotix View Post
    I have no idea what you are talking about...

    From what I can tell, you are asking how you can make a pointer point to the same area in memory throughout the entire execution. I don't think this is different in any language. Its just

    char *c = malloc(sizeof(char));

    Now whatever you store into the memory location allocated for c should be there forever, as long as no other piece of your code (or outside malicious code) changes that. Afaik, the address at which the pointer is stored should not change either. It wouldn't make sense for program to continually copy/move the data to different locations.

    EDIT: Forgot to add the exception that if you lose a reference to this location in memory, some languages include a garbage collector which will free the memory for you. I.E. Java/Python
    You know that I'm talking about C#, do you?





    Originally Posted by TOM_RUS View Post
    You can try GCHandle Structure with GCHandleType.Pinned. What kind of information your pointer points to?

    P.S. An instance with nonprimitive (non-blittable) members cannot be pinned this way.
    That's exactly what I'm looking for

    I plan to use this for objects, for strings and for ints. I'll try it now but it seems like that it will work.

  5. #5
    Sychotix's Avatar Moderator Authenticator enabled
    Reputation
    1443
    Join Date
    Apr 2006
    Posts
    4,001
    Thanks G/R
    295/587
    Trade Feedback
    1 (100%)
    Mentioned
    10 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by Frosttall View Post
    You know that I'm talking about C#, do you?
    Yes... but I was just stating that it should not matter what language you are using.

    I plan to use this for objects, for strings and for ints. I'll try it now but it seems like that it will work.
    If you create a pointer to an integer, the location of that integer should never change unless you tell it to.

    For example (using C code because its easiest),
    Code:
    int *x;
    x = malloc(sizeof(int));
    *x = 42;
    int i;
    
    //Assume the pointer x was allocated at memory location 0x0, the malloc call allocated memory at 0x4
    //This should print the values 0 4 42
    for(i=0;i<however_long_you_want);i++) printf("Address of x: %d Address of value stored in x: %d Value stored in x: %d \n", &x, x, *x);
    
    x = malloc(sizeof(int));
    //In C#, the garbage collector as I understand it will (on its next cycle) see that you no longer have a reference to the address at which 42 was stored, so it will free that memory.
    
    *x = 400;
    //Assume the garbage collector has not run yet however, so the memory at 0x4 still exists. The new address where the integer is stored is at 0x8
    //This should print 0 8 400
    for(i=0;i<however_long_you_want);i++) printf("Address of x: %d Address of value stored in x: %d Value stored in x: %d \n", &x, x, *x);
    Unless I'm missing some fundamental difference that C# has over any other language I've programmed in (C,C++,C#,Java,Python,Delphi), there is no reason any of the values should be changing.

    Now for example, if you are looking at memory within WoW instead of your own program, you may not have the base pointer. WoW (and all programs really) need to know where everything is, even if it is buried within tons of structures/classes.
    Last edited by Sychotix; 07-05-2012 at 03:03 PM.

  6. #6
    Frosttall's Avatar Active Member
    Reputation
    64
    Join Date
    Feb 2011
    Posts
    261
    Thanks G/R
    16/3
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you, indeed would that work in an unmanaged language, but C# isn't

    The Garbage Collector of C# trys to keep the heapsize as small as possible. That's why once allocated values won't be stored the whole at the same location, but moved if necessary.

  7. #7
    DarkLinux's Avatar Former Staff
    CoreCoins Purchaser Authenticator enabled
    Reputation
    1627
    Join Date
    May 2010
    Posts
    1,846
    Thanks G/R
    193/539
    Trade Feedback
    16 (100%)
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Set that area to non writable. See if that works... I have no idea .. I dont use C#...

  8. #8
    Jadd's Avatar 🐸 Premium Seller
    Reputation
    1515
    Join Date
    May 2008
    Posts
    2,433
    Thanks G/R
    81/336
    Trade Feedback
    1 (100%)
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    This is what you want: Pinned Object - CodeProject

Similar Threads

  1. [Request] Fixed AAPDRDS Donation System $$
    By Nallman in forum WoW EMU Questions & Requests
    Replies: 0
    Last Post: 08-17-2009, 07:52 AM
  2. [Request] Fixing size on a race swap guide ?
    By Phoen!x in forum WoW ME Questions and Requests
    Replies: 9
    Last Post: 01-01-2009, 09:37 AM
  3. [Request]/Fix Blood Spurts
    By Gezzdt in forum WoW ME Questions and Requests
    Replies: 0
    Last Post: 12-25-2008, 01:26 PM
  4. [REQUEST] Fix for snowballs!
    By RyeRye in forum WoW EMU Questions & Requests
    Replies: 1
    Last Post: 12-13-2008, 07:01 AM
  5. [Request] Fixing my custom model
    By [Soul Eater] in forum WoW ME Questions and Requests
    Replies: 0
    Last Post: 06-07-2008, 10:24 AM
All times are GMT -5. The time now is 04:08 PM. Powered by vBulletin® Version 4.2.3
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Google Authenticator verification provided by Two-Factor Authentication (Free) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search