Reputation: 7925
I'm playing with pointers in order to understand them, so i'd like to know why I can't, for example, print the value of the address 0 (zero) and others.
#include <stdio.h>
int main()
{
int *i = 0;
int *e = (int*)0x100;
while (i <= e)
{
printf("%d\n", *i);
i++;
}
return 0;
}
This example crashes.
Upvotes: 1
Views: 931
Reputation:
From C standard, paragraph 6.5.3.2/4
"If an invalid value has been assigned to the pointer, the behaviour of the unary * operator is undefined."
An example of an invalid value is the null pointer. So what you do might work and might not work. I know that on HP-UX 11.31 with gcc 4.3.1 it will work and not crash. in you case it crashes. As you see the standard does not imposes any particular behaviour in this situation.
Upvotes: 1
Reputation: 20997
You're probably trying access memory which your program isn't allowed to access and "crash" is just system preventing "virus" (you :) ) from destroying it.
Here's a little linux code compiled with gcc:
#include <stdio.h>
int b;
int main() {
char *ptr = (char*)&b;
ptr -= 2368;
int i;
for( i = 0; i < 3984; i++){
printf( "%d: %c\n", i, ptr[i]);
}
printf( "\n");
return 0;
}
If you try to access -2369
bytes you'll get segmentation fault (access violation). If you try to access more than 3984th byte (3983th including "zero byte") you'll get the same error (this is probably page size usable for application use).
You can also access binary code directly:
char *ptr = (char*)&main;
ptr -= 999;
int i;
for( i = 0; i < 3763; i++){
printf( "%c", ptr[i]);
}
On my system is address of b: 0x600970
And address of main: 0x4004e4
So you can see you have access to different scopes of memory, but you're limited just to those.
Upvotes: 0
Reputation: 145829
0
is a null pointer constant
(C99, 6.3.2.3p3 ): "An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant."
Dereferencing a null pointer is undefined behavior.
(C99, 6.5.3.2.p4) "If an invalid value has been assigned to the pointer, the behavior of the unary * operator is undefined.87)"
and
87): "Among the invalid values for dereferencing a pointer by the unary * operator are a null pointer, an address inappropriately aligned for the type of object pointed to, and the address of an object after the end of its lifetime."
Upvotes: 1
Reputation: 7608
Not all the addresses in the address space are mapped to an actual memory cell on a physical memory module. When you try to read the value at address NULL
(i.e. the cell at address 0) that is not mapped to anything, your system detects it and kills your process with a segmentation falt
signal or something similar.
Upvotes: 0
Reputation: 6052
Access Violation...
You don't have access to all of your memory directly, there are protected areas.
To put it simply, an access violation occurs any time an area of memory is accessed that the program doesn't have access to.
http://blogs.technet.com/b/askperf/archive/2008/06/03/access-violation-how-dare-you.aspx
Upvotes: 2
Reputation: 399793
You can only dereference valid pointers. These are pointers returned from e.g. malloc()
, or pointers generated by taking the address of something. Dereferencing an invalid pointer is undefined behevior.
In your case, your operating system likely doesn't allow you to read memory that isn't mapped to your process, which is why it kills the process when you try.
Upvotes: 2