Thread: How do I point to specified memory?

  1. #1
    Registered User errigour's Avatar
    Join Date
    Mar 2009
    Posts
    102

    How do I point to specified memory?

    I am trying to be very careful with pointing to memory and I was hoping someone can
    save me the trouble of trying to point to memory without knowing about what I am
    doing. My question is how do I? I might already know so you can just reply with you
    got it if I am right. If I am wrong could you show me an example of pointing to a
    specified memory location. I want to make a program that gives me a summery of my
    my hard drive and I need a pointer to point to 0x0 and I want to make the pointer
    increment what it's pointing to until the last memory location on my hardrive. Also
    if you answer this could you show me a constant character method of pointing to
    the information? If there is one.

    The code below is what I hope will do it. I would love to here what people have to
    say about this post.
    Code:
    char *char_ptr;
    char_ptr=0x0;
    
    I believe that this is absolutely completely unsafe to practice.
    const char int_ptr;
    &int_ptr=0x0;
    Last edited by errigour; 07-29-2011 at 08:03 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I don't think "unsafe to practice" is the phrase you want for the second half; "nonsensical" is more like it.

    As to what your want your program to do, well, it won't. Address space mapping means your program only sees a "chunk" of your actual physical memory, and there's no way for a user-space program to get a hold of the larger picture (so to speak), except via talking to the OS (if at all).

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by errigour View Post
    I am trying to be very careful with pointing to memory and I was hoping someone can
    save me the trouble of trying to point to memory without knowing about what I am
    doing. My question is how do I? I might already know so you can just reply with you
    got it if I am right. If I am wrong could you show me an example of pointing to a
    specified memory location.
    You're definitely not right. I think you need to do a lot more studying and practice before you actually try to do what you're after here.

    I want to make a program that gives me a summery of my
    my hard drive and I need a pointer to point to 0x0 and I want to make the pointer
    increment what it's pointing to until the last memory location on my hardrive.
    Pointers point to places in memory, your hard drive is not memory. C allows you to work with the file system on your hard drive, but the language itself doesn't allow raw access to your hard drive. You can not just start at address 0 on your hard drive and shlep through byte by byte, without some serious, hard core, low level code. Either super low-level driver stuff and/or some crazy assembly code would probably be necessary. I don't really know much about what it would involve, but plain old C won't really handle it. I couldn't even tell you where to start looking for this.

    Also if you answer this could you show me a constant character method of pointing to
    the information? If there is one.
    Not totally sure what you mean here. Perhaps you mean a pointer to const data, i.e. you can "read" the values but can't "write" them? It's not complicated, lots of library functions like strlen do it:
    Code:
    char a;
    char b;
    const char *p = &a;  // here, I can make p point to any address, but I can never modify the contents it's pointing to
    *p = 'a';  // the compiler wont allow this, because you declared p to point to a const char, so it can't be changed
    p = b;  // I can change what p points to, but again, I can't store anything there
    The code below is what I hope will do it. I would love to here what people have to
    say about this post.
    Code:
    char *char_ptr;
    char_ptr=0x0;
    
    I believe that this is absolutely completely unsafe to practice.
    const char int_ptr;
    &int_ptr=0x0;
    Yep, that's totally unsafe, and some of it plain old won't compile. you set char_ptr to point to the address 0x0, which is basically NULL (I won't get into exactly what NULL is or isn't, the jury is still out on that one).

    The code you believe is unsafe won't even work. First, you declare a const char called int_ptr (bad name, but whatever). That means it contains a single byte, one char, and it can't change. If it's a global var, it's initialized to 0 and stuck there. If it's a stack variable, it's initialized to garbage and wont change. That's pretty useless either way. Also, the last line is impossible. The & (address of) operator does not produce a modifiable value. It would be like trying to "relocate" int_ptr, which doesn't make sense, especially since you're trying to relocate it to NULL.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by errigour View Post
    I want to make a program that gives me a summery of my
    my hard drive and I need a pointer to point to 0x0 and I want to make the pointer
    increment what it's pointing to until the last memory location on my hardrive.
    Well there's your problem. You cannot do such a thing. A disk is not memory.
    Pointers do not point to stuff on disk (other than in the OS specific context of the pagefile or memory mapping), and those aren't much good for what you are thinking of.

    What kind of "summary" are you thinking of?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Wow......greater than 2 years messing with this stuff and it still looks like you just started. I am truly impressed. I am assuming you are still on a *nix system? Either way it doesn't really matter. OS designers have improved their knowledge and practices greatly and basically put and end to a lot of this "script kiddie" stuff. (Not saying that is what you are intending, just the same type of question. Just like - 'How can I make a key logger' "

    As anduril pointed out, you are going to need to do a lot more studying before you are going to be able to program these sort of "utilities".

    How will you know when you have studied enough? These type of topics all have the same recursive answer: "You will know how to do it when you know how to do it."
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  6. #6
    Registered User
    Join Date
    Aug 2010
    Location
    Rochester, NY
    Posts
    196
    Right, so every answer here seems to be on the negative slope of things...which is fine, I guess.

    On a more positive note, to try and help you out a bit, as many have stated, you
    A) Do not have direct access to the HDD, pointers point to memory locations, and even then, it's probably address mapped so you're probably not even getting a raw location there, either.
    B) The HDD access is performed via libs, and the memory, by the OS.
    C) You're very restricted from your OS assigned user space

    So to circumvent *some* of that, why not change the approach a bit. Instead of approaching this from a direction where you can't win, in user-land, instead, approach it from a direction where you can.

    A kernel module or driver is a good place, for that kind of access, IMO. More likely the former, though you could write a SATA driver that has the ability to do so intrinsic to its design.

    By creating a kernel module, you can directly access the kernel, and un-........ any mapping that's occurred. From there, you should be able to call upon the SATA driver directly to fetch bytes for you. You can then write a layer in user-land upon this kernel module, which has access to its API (which you'll have to define in your kernel driver - see the KMDF, if in W32). You can do all the processing of the bytes up there, in user-land, where it's safe.

    I hope this helps you, some. Best of luck with your program!

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by errigour View Post
    I want to make a program that gives me a summery of my
    my hard drive and I need a pointer to point to 0x0 and I want to make the pointer
    increment what it's pointing to until the last memory location on my hardrive.
    Best advice --which the other are already alluding to-- is "forget it".

    Hard disks are not memory. They are not accessed by pointers. They are not accessed in bytes.

    A hard disk is divided into sectors, spindles, and platters, each platter has a number of spindles, each spindle has a number of sectors and each sector has a number of bytes... the minimum you can read from a hard disk is one sector (usually either 512 or 2048 bytes depending on the drive).

    This information... which file is in which sectors can be obtained from the OS... take a look at most defragmenter tools and the disk map they provide... but outside the context of defragmenting a drive (which uses OS specific function calls) this information is basically useless to you.

    If you want an actual summary of your drive that you can use, you need to walk the directories (folders) compiling a list of files, sizes, dates, etc. which is still OS specific but far more informative and a gazillion times safer than what you're talking about.

  8. #8
    Registered User errigour's Avatar
    Join Date
    Mar 2009
    Posts
    102

    I think I have found a way.

    After thinking about it and testing stuff on my computer I have found that pointers
    can be used to access raw memory. The only problem is it isn't really specified. It's
    more along the lines of using what gcc specifies and subtracting exactly that much
    from the pointer to print the information at 0x0. Which means you can modify any
    specified detail of your hard drive. I was just was wondering if you could define a
    memory location that you want your pointer to point to instead of only defining the
    value of what your pointer is pointing to.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by errigour View Post
    After thinking about it and testing stuff on my computer I have found that pointer
    can be used to access raw memory. The only problem is it isn't really specified. It's
    more along the lines of using what gcc specifies and subtracting exactly that much
    from the pointer to print the information at 0x0. Which means you can modify any
    specified detail of your hard drive. I was just was wondering if you could define a
    memory location that you want your pointer to point to instead of only defining the
    value of what your pointer is pointing to.

    What part of "your hard disk is not memory" do you not understand?

    Really...

    At least 3 of us have explained that a hard disk is an I/O device, it is not part of your computer's memory.

    In modern computers memory is not contiguous... an application's memory is mapped into blocks of the larger system memory. You don't --can't-- actually know what physical address a pointer 0x00 is pointing to since you are only working with an offset into your application's allocated space.
    Last edited by CommonTater; 07-30-2011 at 10:06 AM.

  10. #10
    Registered User errigour's Avatar
    Join Date
    Mar 2009
    Posts
    102
    Also maybe someone knows how to use assembly in c to print specified hardware information.
    It's not impossible in c, I just don't know how to do it and am crossing my fingers on this one
    hoping that someone will post a method of doing such an easy task.

  11. #11
    Registered User errigour's Avatar
    Join Date
    Mar 2009
    Posts
    102
    Common Tater if you know what I mean when I talk about it then you shouldn't be offended.

  12. #12
    Registered User errigour's Avatar
    Join Date
    Mar 2009
    Posts
    102
    Why not tell me what to call the device that stores your data instead.

  13. #13
    Registered User errigour's Avatar
    Join Date
    Mar 2009
    Posts
    102
    And thank you for your comment.

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok... way too vague... What exactly (in detail) are you trying to do?

    It's very likely the OS already provides system calls for most anything you can legitimately need to know...

    For example : If you want to know the size, sector count, allocation units etc of a hard disk on a Windows machine there are windows API calls you can use... For example: GetDiskFreeSpace()

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by errigour View Post
    Why not tell me what to call the device that stores your data instead.
    It's a hard disk... it is connected to the i/o buss of your computer. It is used for perminent storage of data.

    It is NOT memory... memory is a physical electrinic chip connected to the main buss of the computer which is used for temporary storage of programs and data that is being manipulated by programs.


    You cannot examine a hard disk by looking at memory.

    Here's a very simple block diagram of what's in your computer...
    http://nl.wikipedia.org/wiki/Bestand...chitecture.png

    Notice how RAM (memory) and The hard disks (IDE and SATA) are not only two different things but are differently connected to the computer?
    Last edited by CommonTater; 07-30-2011 at 10:38 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-27-2011, 04:34 AM
  2. memory representation of floating point
    By karthigayan in forum C Programming
    Replies: 1
    Last Post: 06-10-2009, 06:21 AM
  3. Point to Point Protocol and Bluetooth DUN
    By PSLoh in forum Networking/Device Communication
    Replies: 2
    Last Post: 03-03-2008, 09:44 AM
  4. fixed point / floating point
    By confuted in forum Game Programming
    Replies: 4
    Last Post: 08-13-2002, 01:25 PM
  5. Floating point faster than fixed-point
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 11-08-2001, 11:34 PM