Printing the contents of memory

This is a discussion on Printing the contents of memory within the C Programming forums, part of the General Programming Boards category; Hello all, I've been working on learning C for some time, but one of things I just can't get my ...

  1. #1
    Living to Learn
    Join Date
    Feb 2006
    Location
    Aberdeen, UK
    Posts
    33

    Printing the contents of memory

    Hello all,

    I've been working on learning C for some time, but one of things I just can't get my head around is the business of printing the contents of memory to the screen. I know it's probably something as simple as making a loop that will cycle through addresses from a start point to an end point, and then print the data using printf, but I just can't figure out how to express that in code.

    I've look around, but can't find any examples to work from, all I found was a page on pointers and extracting the address of variables from "C By Example" (chapter 20).

    As an example, say I wanted to print out a list of chars stored in memory from a given address, how would that be accomplished?

    Thanks a lot, and sorry if this sounds like a stupid question. I searched the forums but couldn't find any similar questions asked before.

    Cheers.

    Hussein.

  2. #2
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479
    I do C++, but the same theroy applys in C. If you are talking about using memory addreesses, you need to use pointers. If you are unsure on how to use them, read a tutorial online to get the jist of it. A pointer is basicly a variable that contains a memory address.

    Code:
    #include <iostream>
    
    using std::cout;
    using std::endl;
    using std::cin;
    
    int main ( void )
    {
       int num = 10;
       int *ptr = &num;  // get address of num
    
       cout << *ptr << endl;  // will print 10
    
       cin.get();
    
       return 0;
    }
    I applogise in advance if you already know this, but sombody else may have a more definate answer, using pointers is the prime key though

  3. #3
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Still, you can't just read the memory in many OS-s, because the OS protects the memory that isn't meant to be read by your program.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    You could write your own memory device driver which would run under the system and have unrestricted access to process memory. Whether this seems like a blatantly stupid, unresourceful waste of time and effort is your choice to make.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    I wrote my own program to examine memory under Windows. Windows generally doesn't care what memory you access, as long as it's relatively close to the memory you're supposed to access.

    Out of curiousity, why do you want to do this? A debugger could do it for you, unless you want this feature to be present in your finished program.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Living to Learn
    Join Date
    Feb 2006
    Location
    Aberdeen, UK
    Posts
    33
    Hi. firstly, I'm really sorry for not responding sooner. Most of you must think I'm simply one of those people that come along, beg for answers and then just disappear. In actual fact, I hadn't realised that I had my preferences set to not send e-mail notification, and hence, and because I'm partially sighted, I lost the thread in amongst the forums. Just found this now after about an hour of searching.

    Anyway, thanks for the tips. I read up about pointers, and whereas I'm still having trouble getting my head around the fact that a pointer is basically just a signpost saying 'xxx is this way' and also getting to grips with the fact that sometimes you put an asterisk before the pointer (char *ptr) and sometimes you don't (ptr = filename) I do get the basic gist.

    As for wy I wanted to know, there was a pretty big section on it in the books I was studying from, and also it seemed to be important if you wanted to read back portions of memory, say if a file had been saved there.

    Sorry if this all sounds pretty pointless. I'm still learning C, and haven't yet got to the concepts surrounding saving and opening files. At this level, I'm still just creating data, storing it in memory and then accessing it from there, as opposed to actual saved files.

    Thanks again for the help. i appreciate your patience with me.

    Hussein.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    In the future, you can search for posts you've made; click on your username, and then click on "Find all posts by this user" or "Find all threads started by this user".
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

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, 05:04 AM
  2. Relate memory allocation in struct->variable
    By Niara in forum C Programming
    Replies: 4
    Last Post: 03-23-2007, 04:06 PM
  3. Printing out the memory address
    By Scalpel78 in forum C++ Programming
    Replies: 16
    Last Post: 01-09-2007, 11:01 AM
  4. Copying memory, pointers and the like.
    By psychopath in forum C++ Programming
    Replies: 34
    Last Post: 12-12-2006, 01:37 PM
  5. Shared Memory - shmget questions
    By hendler in forum C Programming
    Replies: 1
    Last Post: 11-29-2005, 02:15 AM

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