So I'm learning about pointers and stuff and I want to write a program that show what is stored in a certain memory position.

So far I've done this:

Code:
#include <stdio.h>

main()
{
   int i, *ptr;
   for(i=0; i<50; i++)
   {
      ptr= i;
      printf("%d\n",*ptr);
   }
}


The cmd just crashes for some reason.

How do I write this program?


EDIT: I forgot to mention, but the compiler gives me this warning: "[Warning] ...c:8: warning: assignment makes pointer from integer without a cast"
The program as you have written it is correct, in that it attempts to read the integers in memory from address 0 through 50. What you fail to account for is virtual memory.

In particular, 0 is typically an invalid address (the NULL pointer). Most systems deliberately leave the first page of virtual memory unmapped to handle this. If you really wanted to read all of your process' virtual memory, there are (ugly) ways to do so.

On Windows, the IsBadReadPtr function can inspect your address space and tells you whether the specified block of address space is mapped readable. For other platforms, libsigsegv (or directly handling SIGSEGV) provides an indirect way to probe memory mappings. It may also be possible to directly inspect a process' VM mappings (likely through /proc), but that sounds messy.

Trying to do these things is generally a bad idea though.
Tari wrote:
The program as you have written it is correct, in that it attempts to read the integers in memory from address 0 through 50. What you fail to account for is virtual memory.

In particular, 0 is typically an invalid address (the NULL pointer). Most systems deliberately leave the first page of virtual memory unmapped to handle this. If you really wanted to read all of your process' virtual memory, there are (ugly) ways to do so.

On Windows, the IsBadReadPtr function can inspect your address space and tells you whether the specified block of address space is mapped readable. For other platforms, libsigsegv (or directly handling SIGSEGV) provides an indirect way to probe memory mappings. It may also be possible to directly inspect a process' VM mappings (likely through /proc), but that sounds messy.

Trying to do these things is generally a bad idea though.


I tried with i starting at 1 instead of 0 and the cmd still crashes.

EDIT: Also what does pointer cast mean?
AliceIsDead wrote:
I tried with i starting at 1 instead of 0 and the cmd still crashes.
As Tari said, the entire first page is usually unmapped (that is, the first 4KB of memory or so). In fact, because of virtual memory, you have 32 to 48 bits of address, the vast, vast majority of which does not map to actual memory. Tari recommended using something like direct sigsegv handling or the libsigsegv library to catch and continue when you access an invalid memory address. Of course, don't forget that a program that ends up handling close to 2^32 or 2^48 segmentation faults will take a very, very long time to complete.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement