Searching memory for an adress?

This is a discussion on Searching memory for an adress? within the C++ Programming forums, part of the General Programming Boards category; Ok! I made a very small program to search through my memory. But, it doesn't cut out when it finds ...

  1. #1
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379

    Searching memory for an adress?

    Ok! I made a very small program to search through my memory. But, it doesn't cut out when it finds its values. Now, what I thought this would do is end at variable adress c or d. Instead, it never ends.

    Code:
    #include <iostream>
    using namespace std;
    
    int main() {
     int *a;
     int b;
     int c = 9999;
     int d = 0;
    
     for(a = &b; a != &c || a != &d; a++) {
      cout << a;
     }
    }
    Being set to the adress of b shoulden't it cut out directly after a++? Or is memory not set up that way? Well, thank you in advance.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    The only way that "a != &c || a != &d" can be false is if a == &c and a == &d simultaneously. But c and d have different addresses, so that never happens.

    Edit: I think you meant "a != &c && a != &d".
    Last edited by robatino; 03-26-2007 at 05:38 PM.

  3. #3
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    That's a good point -,-. Haven't used C++ in awhile, I did it backwards. What I meant to do was:

    Code:
    #include <iostream>
    using namespace std;
    
    int main() {
     int *a;
     int b;
     int c = 9999;
     int d = 0;
    
     for(a = &d; *a != c && *a != d; a++) {
      cout << a;
     }
    
     cout << '/n' << a;
    }
    That cut's out if the memory is not either 9999 or 0 right? -Well, it probably doesn't, but it does crash unlike the other one. Now why does this crash but the other doesn't?

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    Because in this one you are dereferencing a, but since a is unitialized you are dereferencing a pointer to memory that you don't own. This causes a crash.

    If it worked, you would be able to read whatever memory you wanted to, which would be not very secure.

  5. #5
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    Wait, so I can point to whatever place in memory I want, I just can't read it if it's not in my programs scope?

    So then, how do I make sure the memory is in the scope of my program. Or, how do I safely read my memory so it doesn't result in an error?

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,037
    There is no way to check if a pointer point to memory used by your program. You have to keep track of pointers yourself.

    According to standard C++, pointer only has a valid value if it point to a variable, an value in an array, a value 1 beyond the end of the array, or a it is a null pointer. In practice, the restrictions on pointers are a little more lax, but for true cross-platform program code you'll want to abide by that. But even with detailed knowledge of the platform, you shouldn't have pointers that don't point to anything specific.
    Last edited by King Mir; 03-26-2007 at 07:40 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  7. #7
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by King Mir View Post
    There is no way to check if a pointer point to memory used by your program. You have to keep track of pointers yourself.

    According to standard C++, pointer only has a valid value if it point to a variable, an value in an array, a value 1 beyond the end of the array, or a it is a null pointer. In practice, the restrictions on pointers are a little more lax, but for true cross-platform program code you'll want to abide by that. But even with detailed knowledge of the platform, you shouldn't have pointers that don't point to anything specific.
    And to emphasize this the code
    Code:
    int d;
    int* a = &d;
    a++;
    is illegal because you have no legal way to know where the a points. Actually - you can do this, but you cannot use value of a after this...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Quote Originally Posted by vart View Post
    And to emphasize this the code
    Code:
    int d;
    int* a = &d;
    a++;
    is illegal because you have no legal way to know where the a points. Actually - you can do this, but you cannot use value of a after this...
    According to the Standard, the last line generates undefined behavior, so conceivably the program could crash or do anything else, even if a isn't dereferenced.

  9. #9
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Quote Originally Posted by Blackroot View Post
    Wait, so I can point to whatever place in memory I want, I just can't read it if it's not in my programs scope?

    So then, how do I make sure the memory is in the scope of my program. Or, how do I safely read my memory so it doesn't result in an error?
    Well, I am going to assume you're talking about a modern, conventional operating system like Windows, MacOS, Linux, etc.

    First off -- a pointer only exists in the scope of your program. If your program and mine execute on the same machine, and you read address 0xBEEFBEEF and I read address 0xBEEFBEEF, assuming neither program crashes, we'll be reading totally different locations in RAM.

    A pointer is a virtual address, each process lives in its own virtual address space. All addresses in that space can ONLY reference your own program's data. When you read a memory address, your OS's memory manager looks up in a table where that block of memory physically resides (either in RAM or on the hard drive pagefile). If the address you feed it is not in the virtual memory lookup table for the program, the program segfaults.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  10. #10
    The larch
    Join Date
    May 2006
    Posts
    3,573
    In short, this can't be done. If you want a pointer to int a or int b, you don't search for them but take the address with the & operator and use it.

    By the way, is there any guarantee that a++ is going in the right direction? May-be it could be a-- just as well?

    (In the above program you probably could compare the addresses of b, c, and d and just print whatever is "closer" to b.)

  11. #11
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    as I remember comparing pointers is supported for continues regeons like arrays, structs etc...

    comparing pointer pointing to the different regeons (like pointer to d and pointer to b) is undefined behaviour
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  12. #12
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You can cast the pointers and just compare the value of the address.

    Anyway, I don't see where it would be useful to do something based on the location of a variable in memory, but here's what I meant.

    Code:
    #include <iostream>
    #include <cstdlib>
    
    int main()
    {
        char a = 'a', b = 'b', c = 'c';
        unsigned add_a = reinterpret_cast<unsigned>(&a);
        unsigned add_b = reinterpret_cast<unsigned>(&b);
        unsigned add_c = reinterpret_cast<unsigned>(&c);
        
        std::cout << add_a << '\n' 
                  << add_b << '\n' 
                  << add_c << '\n';
        std::cout << "Closest to a is: " 
            << ( abs(add_b - add_a) < abs(add_c - add_a) ? b : c )
            << '\n';
        std::cin.get();
    }

  13. #13
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    Actualy, I found a way to read an address and woulden't you know it;
    Code:
    ReadProcessMemory
    Just have to set the programs permission rights, which suprisingly is even easier than reading the memory O_o. I thought it would be a bit more complicated but eh, maybe windows doesn't overcomplicate everything.

    Thanks for the help, guys ;P.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mutex and Shared Memory Segment Questions.
    By MadDog in forum Linux Programming
    Replies: 14
    Last Post: 06-20-2010, 04:04 AM
  2. Replies: 7
    Last Post: 02-06-2009, 11:27 AM
  3. Copying memory, pointers and the like.
    By psychopath in forum C++ Programming
    Replies: 34
    Last Post: 12-12-2006, 12:37 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  5. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 12:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21