Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Problem with C++ arrays.

Problem with C++ arrays.

Scheduled Pinned Locked Moved C / C++ / MFC
12 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Ratul Thakur

    im my textbook , i was told that you daclare an array as follows:

    type var[size];

    where size is constant and cannot be changed later. but if i run this :

    int a[2];
    a[3]=10;
    cout<
    it does not give any error and prints:
    10

    how is this possible.. because the size of the array was 2!! ??
    please help me understand.

    J Offline
    J Offline
    jeron1
    wrote on last edited by
    #2

    Maybe take a look at this[^] thread, as they talk about out-of-bounds array indexing, it's undefined behaviour.

    "the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle

    1 Reply Last reply
    0
    • R Ratul Thakur

      im my textbook , i was told that you daclare an array as follows:

      type var[size];

      where size is constant and cannot be changed later. but if i run this :

      int a[2];
      a[3]=10;
      cout<
      it does not give any error and prints:
      10

      how is this possible.. because the size of the array was 2!! ??
      please help me understand.

      K Offline
      K Offline
      k5054
      wrote on last edited by
      #3

      C/C++ does not enforce arrays bounds checking. Your code invokes "Undefined behavior", which means what it sounds like - the C/C++ standards don't say what will/can happen. The compiler may do anything, including make demons fly out your nose! (see Undefined behavior - Wikipedia, the free encyclopedia[^]). This is often the root of subtle, and often hard to find bugs.

      R 1 Reply Last reply
      0
      • K k5054

        C/C++ does not enforce arrays bounds checking. Your code invokes "Undefined behavior", which means what it sounds like - the C/C++ standards don't say what will/can happen. The compiler may do anything, including make demons fly out your nose! (see Undefined behavior - Wikipedia, the free encyclopedia[^]). This is often the root of subtle, and often hard to find bugs.

        R Offline
        R Offline
        Ratul Thakur
        wrote on last edited by
        #4

        okay thanks

        K 1 Reply Last reply
        0
        • R Ratul Thakur

          okay thanks

          K Offline
          K Offline
          k5054
          wrote on last edited by
          #5

          I can recommend the link that jeron1 gave you. The top voted answer top the question is a very good explanation of what's happening behind the scenes.

          1 Reply Last reply
          0
          • R Ratul Thakur

            im my textbook , i was told that you daclare an array as follows:

            type var[size];

            where size is constant and cannot be changed later. but if i run this :

            int a[2];
            a[3]=10;
            cout<
            it does not give any error and prints:
            10

            how is this possible.. because the size of the array was 2!! ??
            please help me understand.

            P Offline
            P Offline
            Patrice T
            wrote on last edited by
            #6

            It is possible because C and C++ do not check array boundaries, this behavior make them fast at cost of security. In such a small program, you see no consequences, but bigger program, you will see another variable suddenly changing of value when it is not supposed to.

            int a[2];
            int b;
            b= 0;
            a[3]=10;
            cout<
            In this example, b may end up with 10 depending on compiler details

            Patrice

            “Everything should be made as simple as possible, but no simpler.” Albert Einstein

            L 1 Reply Last reply
            0
            • P Patrice T

              It is possible because C and C++ do not check array boundaries, this behavior make them fast at cost of security. In such a small program, you see no consequences, but bigger program, you will see another variable suddenly changing of value when it is not supposed to.

              int a[2];
              int b;
              b= 0;
              a[3]=10;
              cout<
              In this example, b may end up with 10 depending on compiler details

              Patrice

              “Everything should be made as simple as possible, but no simpler.” Albert Einstein

              L Offline
              L Offline
              leon de boer
              wrote on last edited by
              #7

              Actually the behaviour has nothing to do with speed, its a legacy of dereferenced pointers in C p[i] is EXACTLY the same as writing *(p+i), most novice programmers will recognize the first form but not the second. The two codes are literally identical and will almost always produce the same code. The second however makes it clear that applying bound checking as a generic behaviour would break all dereferenced pointers. Some C compilers recognize the first form as a special case and will do bound checking and give a warning, but even they will not detect a bound error if the second form is used because p+i is just a pointer with an offset. That may well be, and often is intended behaviour. So array bounds has always been and will always remain the responsibility of the C programmer. A pointer can exist to a memory that is non existent, invalid or has been previously released anyhow, so the array bound situation is no more dangerous than any of those all of which are the C programmers responsibility.

              In vino veritas

              1 Reply Last reply
              0
              • R Ratul Thakur

                im my textbook , i was told that you daclare an array as follows:

                type var[size];

                where size is constant and cannot be changed later. but if i run this :

                int a[2];
                a[3]=10;
                cout<
                it does not give any error and prints:
                10

                how is this possible.. because the size of the array was 2!! ??
                please help me understand.

                H Offline
                H Offline
                Henry Faure
                wrote on last edited by
                #8

                I hate C++ :sigh: it 's so difficult

                J 1 Reply Last reply
                0
                • H Henry Faure

                  I hate C++ :sigh: it 's so difficult

                  J Offline
                  J Offline
                  jeron1
                  wrote on last edited by
                  #9

                  And an easy language would be?

                  "the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle

                  S 1 Reply Last reply
                  0
                  • J jeron1

                    And an easy language would be?

                    "the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle

                    S Offline
                    S Offline
                    Super Lloyd
                    wrote on last edited by
                    #10

                    C#! ;P

                    A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

                    J 1 Reply Last reply
                    0
                    • S Super Lloyd

                      C#! ;P

                      A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

                      J Offline
                      J Offline
                      jeron1
                      wrote on last edited by
                      #11

                      Of course! nothing bad happens when you go out of range on an array in C#! :laugh:

                      "the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle

                      S 1 Reply Last reply
                      0
                      • J jeron1

                        Of course! nothing bad happens when you go out of range on an array in C#! :laugh:

                        "the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle

                        S Offline
                        S Offline
                        Super Lloyd
                        wrote on last edited by
                        #12

                        Not sure if it's an agreement or not... Let's say: no memory corruption occurs!

                        A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups