Like Tree7Likes
  • 1 Post By laserlight
  • 1 Post By laserlight
  • 4 Post By Salem
  • 1 Post By vart

crash program pointer

This is a discussion on crash program pointer within the C Programming forums, part of the General Programming Boards category; Hello everybody I try these the following code It compiles without any error But in runtime the program will crash ...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Location
    Pune, Maharashtra, India
    Posts
    33

    crash program pointer

    Hello everybody
    I try these the following code
    It compiles without any error
    But in runtime the program will crash
    notes win7 64 CodeBlocks
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    int main()
    {
           int a=3;
            int *j;
            j=&a;
           printf("value of a :%d  \n",a);
           printf("Adress of a :%u \n",j);
           j=j+1;
           printf("new address of j: %u \n",j);
    
           printf("Value of a: %d \n",a);
           printf("value of j: %u \n",j);
           printf("vlaue at address:: %d \n",*j);//it must print garbage value
            *j=a;
           printf("value of j: %u \n",j);
           printf("vlaue at address: %d \n",*j);
           return 0;
    
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,589
    When you do this:
    Code:
    j=j+1;
    j effectively becomes a one past the end pointer. Since a one past the end pointer is not permitted to be dereferenced, when you do *j, there is undefined behaviour. One symptom of undefined behaviour is a program crash.

    Moral of the story: just because it compiles does not mean it is correct, so don't do this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jul 2011
    Location
    Pune, Maharashtra, India
    Posts
    33
    Thank you
    But I don't understand meaning of " one past the end pointer "??
    can you explaine more

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,589
    Quote Originally Posted by mohsen
    But I don't understand meaning of " one past the end pointer "??
    Easier to understand it in the context of an array:
    Code:
    int numbers[5] = {0, 1, 2, 3, 4};
    int *one_past_the_end = numbers + 5;
    int *p;
    for (p = numbers; p != one_past_the_end; ++p)
    {
        printf("%d\n", *p);
    }
    So, one_past_the_end is a pointer that is at index 5. The last element of the array (the end) is at index 4. Indeed, index 5 is not actually the index of an element, but you can point to it. As shown, one reason to point to it is for iterating over the elements of the array using a pointer to the first element that is increment to point to the second, third, ..., last element, and finally to one past the end, upon which it compares equal to one_past_the_end, hence terminating the loop.

    In your example, you don't have an array, but the lone object is treated as if it were an array of a single element when you incremented the pointer.
    mohsen likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jul 2011
    Location
    Pune, Maharashtra, India
    Posts
    33

    one more issue

    In the following code I add 2 ,to the pointer
    then program did not crash.why not?
    line 9
    Code:
    #include<stdio.h>
    int main()
    {
           int i=1;
            int *j;
            j=&i;
    
    printf("%d\n",j);
    j=j+2;//if it add with one program will be crash WHY?
    *j=i;
    
    printf("%d",*j);
    return 0;
    
    }

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    > then program did not crash.why not?
    Pure dumb luck.
    Just because you did something stupid, it doesn't automatically infer that the compiler (or the OS, or the machine) will intervene to save you.

    You can never equate a lack of a crash as being "bug-free" or "working".
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Jul 2011
    Location
    Pune, Maharashtra, India
    Posts
    33
    But the program will crash only for number one j=j+1;
    For others number it works j=j+12; or j=j+any number
    There is nothing wrong about the compiler. it is about runtime crash

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,589
    Quote Originally Posted by mohsen
    But the program will crash only for number one j=j+1;
    For others number it works j=j+12; or j=j+any number
    No, it does not work for any number added to j other than 0. Your confidence in claiming a crash versus no crash is completely unwarranted. The behaviour is undefined; the program might crash, or it might not crash, or it might only crash on Friday the 13th, or it might not crash but instead attempt to delete your family photos.

    Quote Originally Posted by mohsen
    There is nothing wrong about the compiler. it is about runtime crash
    Indeed, the effects that you observe here do not show that there is anything wrong with your compiler. Rather, it shows that there is something seriously wrong with your program.
    mohsen likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    > There is nothing wrong about the compiler. it is about runtime crash

    Consider this thought experiment....
    Close your eyes and run across the road(*).
    If you survive the first time, what is your conclusion?



    You have to understand on an abstract level that the code is BROKEN.

    It really doesn't matter what your results are, since they only apply to this program, compiled with your compiler, running on YOUR machine.
    Nobody else is going to be able to 100% replicate your results (so ultimately, it's useless to know this in a program you want to share).

    I've seen code like this before - where for example someone made a minor edit to a large (but broken) program, and it all of a sudden started crashing in an unrelated area of the code.
    This my friend is what you have. You've trashed someone else's memory, and at some point, they're going to notice!.

    The only lesson from this is that you have to be able to spot bugs by reading the code and understanding what is going on.
    You cannot rely on "well it didn't crash, so it must be good".

    (*) disclaimer
    If you're actually stupid enough to try this, then I recommend http://www.darwinawards.com/
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    Registered User
    Join Date
    Jul 2011
    Location
    Pune, Maharashtra, India
    Posts
    33
    Thank you
    In pointer we do not allow to do this because maybe by next execution the program access to the memory which used by other programs
    Thanks a lot

  11. #11
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by mohsen View Post
    Thank you
    In pointer we do not allow to do this because maybe by next execution the program access to the memory which used by other programs
    Thanks a lot
    not other programs, but other code in your program...

    your program consists from a lot more code than you see on the screen, for example printf function you are calling will be as well part of your program. If your broken code trashes variables used by this function it could crash

    The point is - when you go out of bounds of allocated memory - you do not know whose foot you are stepping on.
    mohsen likes this.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why does my program crash
    By Diablo667 in forum C Programming
    Replies: 4
    Last Post: 01-25-2012, 11:31 PM
  2. 2D array and pointer( crash exe)
    By mohsen in forum C Programming
    Replies: 8
    Last Post: 08-01-2011, 03:03 PM
  3. Graceful Crash on Bad Pointer
    By leeor_net in forum C++ Programming
    Replies: 55
    Last Post: 03-19-2009, 01:30 PM
  4. my program crash?
    By vrkiller in forum C++ Programming
    Replies: 10
    Last Post: 03-04-2009, 02:03 PM
  5. Why does the program crash?
    By SkyRaign in forum C++ Programming
    Replies: 8
    Last Post: 09-25-2005, 10:06 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21