Thread: Access violation

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    385

    Access violation

    The following code terminates abruptly when the printf is uncommented, but works fine with printf commented out. Isn't it supposed to terminate as soon as the control reaches line no.7 (irrespective of printf commented or otherwise)??

    Code:
    #include<stdio.h>
    
    int main()
    {
    	int *p, i;
    	p = (int*)0x1234;
    	i = *p;
    	//printf("%d",i);
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    No, it is supposed to print "hello world!".

    In other words, once there is undefined behaviour, it is unreasonable to expect the program to terminate at the point where undefined behaviour is introduced. The program can continue running "perfectly fine".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    So does that mean that we can never directly access any memory outside of our address space.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by juice
    So does that mean that we can never directly access any memory outside of our address space.
    Why do you ask? Anyway, I think the answer is: it depends (e.g., an operating system might allow it, or there might not be an operating system).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    Quote Originally Posted by laserlight View Post
    an operating system might allow it, or there might not be an operating system.
    I suppose you're trying to tell me that its not possible. But what if the page we are trying to access has read only attributes, can we not read it if we need to?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Hmm... are you trying to do some kind of interprocess communication? There are ways to legitimately share memory, for example.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    Quote Originally Posted by laserlight View Post
    There are ways to legitimately share memory, for example.
    May be I should wait until I get into the intricasies of operating systems but for now should I assume that we can't access (read) anything outside of our address space?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes, if you don't need to do otherwise, assuming that is fine.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    It's more likely that in the absence of the side effect (printing it), the compiler optimises the program down to nothing.

    Certainly gcc -O2 eliminates what would otherwise be a disaster.
    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. Access Violation
    By Kiera89 in forum C Programming
    Replies: 2
    Last Post: 04-26-2011, 05:52 AM
  2. Access Violation
    By Josh21 in forum C Programming
    Replies: 1
    Last Post: 04-25-2011, 02:57 PM
  3. Access violation in std::map
    By Dark_Phoenix in forum C++ Programming
    Replies: 7
    Last Post: 08-16-2009, 12:02 PM
  4. Access Violation under VC++ 6.0
    By xephyr in forum C Programming
    Replies: 4
    Last Post: 07-30-2004, 12:06 AM
  5. Access Violation!!
    By Yoshi in forum C++ Programming
    Replies: 4
    Last Post: 09-11-2002, 01:22 PM