Thread: Reading particular memory location ?

  1. #1
    Registered User
    Join Date
    Feb 2006
    Location
    Great India
    Posts
    24

    Reading particular memory location ?

    Hi members,
    Now a days i was working on a program and i made a big move in it. Now the last stage is that i came up with a memory address and i wanna read the content at that particular address.. I have got the memory location and i simply made a "long int" variable and tried to read that memory location but error in program. Here is my source code.
    Code:
    #include<stdio.h>
    int main()
    {
         long int *a;
         a=58112;
         printf("%ld",*(58112));
         getchar();
         return 0;
    }
    I think that we can't assign a constant to a pointer. Am I right ? If yes then please tell me how can i read this particular memory location.Alll suggestions are welcomed.

    With love
    REAL NAPSTER

  2. #2
    Registered User
    Join Date
    Jul 2005
    Posts
    14
    Code:
    *((long int *)58112)
    perhaps?

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    You can't just assign any random memory address to a variable. First off, you couldn't possibly know if that memory address was even reserved for your program. What you have there would give you a segmentation fault for accessing memory outside of the programs scope.

    Why would you want to read a memory address if you don't know what's there, anyway?
    Sent from my iPadŽ

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
     printf("%ld",*a);
    or
    Code:
         printf("%ld",*(long *)58112);
    should "work".
    Kurt

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Work as far as what? Compile?

    Compilation errors on only the beginning of the OP's problems with this code.
    Sent from my iPadŽ

  6. #6
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by ZuK
    [should "work".
    as for your question yes compiles. I don't question the origin of the magic number.
    Kurt

  7. #7
    Registered User
    Join Date
    Feb 2006
    Location
    Great India
    Posts
    24
    Your code eliminated the errors and i was able to make exe. But when i executed that some error came up and that was :
    Code:
    The instruction at "0x00401309" referenced memory at "0x037c0000".The
    memory could not be "read".
    
    click on OK to terminate the program.
    click on CANCEL to debug the program.
    By the way thanks guys but still the problem is here.

    With thanks
    REALNAPSTER

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > The instruction at "0x00401309" referenced memory at "0x037c0000".The
    > memory could not be "read".
    Try listening to SlyMaelstrom
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Let's make pictures. They're fun and easy to understand.
    Last edited by SlyMaelstrom; 05-03-2006 at 06:59 AM.
    Sent from my iPadŽ

  10. #10
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    >> I AM NOT A COMPILER. PLEASE DO NOT TREAT ME LIKE ONE.

    I don't scream obscenities at you when you fail to compile or fiddle with your libraries do I?
    Did you draw that picture yourself?
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  11. #11
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by ahluka
    >> I AM NOT A COMPILER. PLEASE DO NOT TREAT ME LIKE ONE.

    I don't scream obscenities at you when you fail to compile or fiddle with your libraries do I?
    Did you draw that picture yourself?
    Yes you do and yes I did.
    Sent from my iPadŽ

  12. #12
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Sorry if I broke anything.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  13. #13
    Registered User
    Join Date
    Feb 2006
    Location
    Great India
    Posts
    24
    hi guys,
    I used this code to read the memory addresses and i can read only a specific range.
    Code:
    #include<stdio.h>
    int main()
    {
        printf("%ld",*(long int *)2281471);
        getchar();
        return 0;
    }
    The address range is
    minimum = 2281472
    maximum = 2297852
    Now i wanna know if i can read this region why i can't read the region that i want ? Actually i wanna read "address : 1613758136". Please tell me what the hell is happening beyond this specific region ?

    what i can say know i just why why why why........... ?

    A plethora of questions and no answer. Why but why i can't read that address ? There should be some way.

    Thanks skymaelstrom for telling me the internal memory management.


    With happiness
    real napster
    Last edited by realnapster; 05-03-2006 at 11:12 AM.

  14. #14
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    What part of
    Quote Originally Posted by SlyMaelstrom
    What you have there would give you a segmentation fault for accessing memory outside of the programs scope.
    don't you understand?

    Any of your major operating systems have this very basic security that prevents an executable from accessing memory outside of its scope. This is so applications can't edit other applications (without of course the other application accepting the data). More specifically, the OS puts it in place so you can't edit system memory and system applications, meaning to say, in Windows for example, you can't mess with explorer.exe, svchost.exe, etc...

    So, to put it plain and simple... you can't access memory outside of your programs scope. Which means you can just access some arbitrary memory address. The only way you could would be considered hacking, which is not discussed on these forums. So please... stop whining.
    Sent from my iPadŽ

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You can't access memory you don't own - period.
    If you try it, the OS kills your program - period.

    Picking random memory addresses doesn't work, something (namely the OS) has to tell you which bits of memory you can access and which you can't.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading Process Memory
    By polydegmon in forum C# Programming
    Replies: 0
    Last Post: 05-26-2009, 07:18 AM
  2. tools for finding memory leaks
    By stanlvw in forum C++ Programming
    Replies: 4
    Last Post: 04-03-2009, 11:41 AM
  3. allocate apecified memory location for a c variable
    By BharathKumar in forum Linux Programming
    Replies: 5
    Last Post: 06-01-2007, 03:47 PM
  4. Reading Process Memory
    By Llam4 in forum C# Programming
    Replies: 10
    Last Post: 04-20-2007, 01:24 PM
  5. Is it necessary to write a specific memory manager ?
    By Morglum in forum Game Programming
    Replies: 18
    Last Post: 07-01-2002, 01:41 PM