Thread: example segmental error

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    11

    Post example segmental error

    code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int
    main()
    {
     int *my_array;
     int key;
    
     my_array = malloc(sizeof(int) * 10);
    
     for(key = 0; key < 20; key++)
     {
      my_array[key] = key;
      printf("key %i:\t%i\n",key,my_array[key]);
     }
    
     return 0;
    }

    terminal:
    Code:
    grytskiv@ZXDSL831II:~/memory$ gcc -ansi sigmental_memory.c -o sigmental_memory && ./sigmental_memory
    key 0:  0
    key 1:  1
    key 2:  2
    key 3:  3
    key 4:  4
    key 5:  5
    key 6:  6
    key 7:  7
    key 8:  8
    key 9:  9
    key 10: 10
    key 11: 11
    key 12: 12
    key 13: 13
    key 14: 14
    key 15: 15
    key 16: 16
    key 17: 17
    key 18: 18
    key 19: 19
    grytskiv@ZXDSL831II:~/memory$



    AND SECOND VARIANT CODE:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int
    main()
    {
     int my_array[10];
     int key;
    
     /* my_array = malloc(sizeof(int) * 10); */
    
     for(key = 0; key < 20; key++)
     {
      my_array[key] = key;
      printf("key %i:\t%i\n",key,my_array[key]);
     }
    
     return 0;
    }

    terminal:
    Code:
    grytskiv@ZXDSL831II:~/memory$ gcc -ansi sigmental_memory.c -o sigmental_memory && ./sigmental_memory
    key 0:  0
    key 1:  1
    key 2:  2
    key 3:  3
    key 4:  4
    key 5:  5
    key 6:  6
    key 7:  7
    key 8:  8
    key 9:  9
    key 10: 10
    key 11: 11
    key 12: 12
    key 13: 13
    key 14: 14
    key 15: 15
    key 16: 16
    key 17: 17
    key 18: 18
    key 19: 19
    segmental error
    grytskiv@ZXDSL831II:~/memory$


    Why in first variant code I don't recive "segmental error"??

    And why in second variant my program execute full code? My program assign data for not existing element array (out range defined elements for my_array)

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Pure chance.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Yeah, what Quzah said. The memory locations things are put in when you declare them are essentially arbitrary, and often there will be big swathes of unused memory between them. And your code will only crash if you attempt to access through your array memory that is allocated to something else. This is why segmentation faults are the most evil, insidious and nasty things ever; you may not get an error message until long after the actual error's happened and end up blaming a perfectly innocent bit of code for the crash.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Winsock problem
    By Wolf` in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2010, 04:55 PM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM

Tags for this Thread