> -----Original Message----- > From: civics@oocities.com [mailto:civics@oocities.com] > Sent: Thursday, September 20, 2007 1:20 PM > To: vijay.zanvar@wipro.com > Subject: Survey Results > > Name = Amit > URL = amit.singh2241@gmail.com > Comments = Hi, > > I have a question, from a long time I am wondering about this > but never got a satisfying answer. I myself have lots of > explanations for it but need the accurate reason. > > pls see the program: > > int main() > { > int *ptr; > > ptr = 134520840; //this value i got when i > //used malloc(), so the > //address shd work. > Though the address should work, that is probably correct; but the question is whether that address is withing the `address space' of this process (i.e., the program)! Certainly, this will work when you malloc() some memory and then access it; but it would not, and shoul not, when you just access memory address randomly. > *ptr = 100; //gives segmentation fault Certainly. When you access `ptr' to store a value in it, not matter what you assign, that does not cause a segmentation fault, because `ptr' is well within the address space. But it is upon accessing the object pointed by `ptr' a segmentation fault is certain. Accessing a memory location, which is out of bound, is more of an OS phenomenon. This program may not fault if the OS does not raise OOB exceptions! You may also want to see http://www.oocities.org/vijoeyz/faq/linux/seg-page.html Best, Vijay Zanvar > } > > Question: > --------- > Here as you can see I have initialized my pointer with a > value which everytime my malloc() returns at runtime. but > when i dont use malloc() and statically assign the same value > to the pointer, i cannot access the pointer to store or read > the values ponited by it. > > I have found lots of answers to this, and have my own a few > too, but was never convinced and totally satisfied by any of > those. It would be great if you can help me with solving this.... > > thnx in adv, > Amit > > subject = Survey Results > REMOTE_HOST: 196.12.63.196 >