C++ array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rajrsk
    New Member
    • Nov 2006
    • 2

    #1

    C++ array

    Dear sir,

    I have problem C++ array(with respect windows operationg System)

    When i declare int a[5]. It must get value a[0],a[1],..a[4]. But it is getting upto a[10}.

    why is it so?


    Thanks lot
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    Originally posted by rajrsk
    Dear sir,

    I have problem C++ array(with respect windows operationg System)

    When i declare int a[5]. It must get value a[0],a[1],..a[4]. But it is getting upto a[10}.

    why is it so?


    Thanks lot
    memory is allocated to elements array a[0] to a[4]. However, C and C++ (unlike Java) have no array bounds checking so a program may address elements outside the array, e.g.
    x=a[5];
    a[6]=10;
    a[10] =z;
    etc. This will read or write memory outside the array bounds which could give incorrect data or corrupt other variables, etc. What happens depends on the compiler/operating system, etc. The program may
    (1) just carry on with corrupt data and appear to work OK (it then crashes months later when run with different data)
    (2) give errors later on when the corrupt data is accessed which are difficult to track down.
    (3) may give a memory segmentation error.

    It is generally advised to use <vector> in C++.

    Comment

    • rajrsk
      New Member
      • Nov 2006
      • 2

      #3
      Originally posted by horace1
      memory is allocated to elements array a[0] to a[4]. However, C and C++ (unlike Java) have no array bounds checking so a program may address elements outside the array, e.g.
      x=a[5];
      a[6]=10;
      a[10] =z;
      etc. This will read or write memory outside the array bounds which could give incorrect data or corrupt other variables, etc. What happens depends on the compiler/operating system, etc. The program may
      (1) just carry on with corrupt data and appear to work OK (it then crashes months later when run with different data)
      (2) give errors later on when the corrupt data is accessed which are difficult to track down.
      (3) may give a memory segmentation error.

      It is generally advised to use <vector> in C++.

      Thank You sir.

      Comment

      Working...