Thread: Avoid Array crossing boundry

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    2

    Avoid Array crossing boundry

    Hi
    Is there any technique to avoid array crossing its allocated boundry and corrupting other data.

    example:
    Code:
    main()
    {
    
     int A[2];
     A[0]=1;
     A[1]=1;
     A[2]=1;
     A[3]=1;
    }

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Yes. Don't do it. C doesn't do anything nice like always raise an exception when that happens: you have make it right. Best you can hope for is a SIGSIEV.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    2

    Thanks

    Ok... is there any way to get SIGSEGV when it crosses the boundry?

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by jionnet View Post
    Ok... is there any way to get SIGSEGV when it crosses the boundry?
    Nope.

    C does not include "baby sitting" such as bounds or range checking and it is up to you as a programmer to know the sizes of your arrays (etc.) and stay within them.

    The answer to the original question is: The only way to prevent array bounds errors is to not write code that gets outside the array boundaries. (as Whiteflags told you).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 03-02-2011, 07:35 PM
  2. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM