Thread: Why there is no runtime error for this code ?

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    59

    Exclamation Why there is no runtime error for this code ?

    Hello Friends !!

    I am unable to know why there is no runtime error or segmentation error (in Linux) when i run the following code :

    Code:
    #include <stdio.h>
    
    int x = 'A';
    
    int main()
    {   
        int *wild;
        int next;
        wild = &x;
        next = *(++wild);
        printf("\n next = %d",next);
        printf("\n\n");
        return 0;
    }
    
    output = 0. (on Linux)
    even though i make the pointer wild to point to an unknown location
    through wild++, i don't get any runtime error or segmentation error
    (when we try to access an unknown location on Linux). Why ?? Can someone make me clear ?

    Regards
    Last edited by techie_san778; 01-09-2016 at 10:33 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Good "luck" or bad "luck", depending on your point of view. The behaviour is undefined, so it running without crashing is a possibility.
    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
    Jun 2015
    Posts
    1,640
    Memory is not assigned to a process in int-sized chunks but in "pages" of, say, 4K bytes each, so there will be quite a bit of memory in your apportioned segment beyond x. Why not make a loop printing a count to see where the segmentation fault occurs?

  4. #4
    Registered User
    Join Date
    Aug 2013
    Posts
    59
    yes algorism, i also think in the same way as you. i observed this peculiarity only in case of global variables. I better try it on windows platform and as you said, i will also check with loop. I will reply with the results.

  5. #5
    Registered User
    Join Date
    Aug 2013
    Posts
    59
    i checked on windows. I ran a loop 10 times. On Linux, the output was 0 always. On windows, 0 was most of the time and garbage values the rest of time.

  6. #6
    Registered User
    Join Date
    Aug 2013
    Posts
    59
    But why is this happening with only external variables ? In case of local variables, i get segmentation fault. Is there any explanation ?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by techie_san778
    But why is this happening with only external variables ? In case of local variables, i get segmentation fault. Is there any explanation ?
    Non-constant variables with static storage duration may be allocated space in a different place from local variables, e.g., data segment rather than stack.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. runtime error
    By zebani in forum C Programming
    Replies: 6
    Last Post: 12-29-2011, 10:39 AM
  2. runtime error in code
    By Lenjaku in forum C Programming
    Replies: 3
    Last Post: 11-08-2010, 03:48 PM
  3. Error in runtime
    By sick in forum C Programming
    Replies: 7
    Last Post: 08-15-2009, 02:51 AM
  4. compile time error or runtime error?
    By George2 in forum C# Programming
    Replies: 3
    Last Post: 05-07-2008, 07:08 AM
  5. Runtime Error
    By Highland Laddie in forum C++ Programming
    Replies: 9
    Last Post: 09-16-2005, 03:29 PM