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. Upper Bound and Lower Bound of an array [SOLVED]

Upper Bound and Lower Bound of an array [SOLVED]

Scheduled Pinned Locked Moved C / C++ / MFC
data-structurestutorial
15 Posts 8 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.
  • G goldenrose9

    how to get the upper bound and lower bound of an array BYTE *pByte = new BYTE[64];

    Some Day I Will Prove MySelf :: GOLD

    modified on Friday, February 4, 2011 8:37 AM

    C Offline
    C Offline
    Cool_Dev
    wrote on last edited by
    #2

    goldenrose9 wrote:

    BYTE *pByte = new BYTE[64];

    You are allocating the array, then it is your responsibility to keep the bounds as 0 and 64. :)

    L G 2 Replies Last reply
    0
    • C Cool_Dev

      goldenrose9 wrote:

      BYTE *pByte = new BYTE[64];

      You are allocating the array, then it is your responsibility to keep the bounds as 0 and 64. :)

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #3

      I think you mean 0 and 63.

      I must get a clever new signature for 2011.

      C 1 Reply Last reply
      0
      • L Lost User

        I think you mean 0 and 63.

        I must get a clever new signature for 2011.

        C Offline
        C Offline
        Cool_Dev
        wrote on last edited by
        #4

        yes.. of course it is!! thanks for pointing about that small but BIG mistake.. :thumbsup:

        L 1 Reply Last reply
        0
        • C Cool_Dev

          yes.. of course it is!! thanks for pointing about that small but BIG mistake.. :thumbsup:

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #5

          My pleasure; it's your turn to catch me out next time. ;)

          I must get a clever new signature for 2011.

          1 Reply Last reply
          0
          • C Cool_Dev

            goldenrose9 wrote:

            BYTE *pByte = new BYTE[64];

            You are allocating the array, then it is your responsibility to keep the bounds as 0 and 64. :)

            G Offline
            G Offline
            goldenrose9
            wrote on last edited by
            #6

            yes, 0 to 63, but i was in search of a function that returns the bound of an array, as in vb UBound() and LBound() function is used. Is there any similer function that helps to acheive bounds in c++

            Some Day I Will Prove MySelf :: GOLD

            C S 2 Replies Last reply
            0
            • G goldenrose9

              how to get the upper bound and lower bound of an array BYTE *pByte = new BYTE[64];

              Some Day I Will Prove MySelf :: GOLD

              modified on Friday, February 4, 2011 8:37 AM

              A Offline
              A Offline
              Andrew Brock
              wrote on last edited by
              #7

              Since it is C++, why not make your own little class that stores the length of the array.

              class Buffer {
              public:
              Buffer(unsigned nSize) : m_pBuffer(NULL), m_nLength(nSize) {
              if (nSize > 0) {
              m_pBuffer = new BYTE[nSize];
              if (m_pBuffer == NULL) {
              m_nLength = 0;
              //you ran out of memory, do something
              }
              }
              }
              ~Buffer() {
              if (m_pBuffer != NULL) {
              delete []m_pBuffer;
              }
              }
              unsigned GetLength() {
              return m_nLength;
              }
              BYTE &operator[](unsigned nIndex) {
              if (nIndex >= m_nLength) {
              //error somehow
              }
              return m_pBuffer[nIndex];
              }
              BYTE *operator*() {
              return m_pBuffer;
              }

              private:
              	BYTE \*m\_pBuffer;
              	unsigned nLength;
              

              };

              Then you can use it like

              Buffer bufData(64);
              strcpy(*bufData, "hello world"); //the operator*() can be used to get a pointer to the data
              //print the length
              printf("The valid range is 0-%u\n", bufData.GetLength() - 1); //remember indices start at 0 and go to n-1
              printf("The 2nd character is \'%c\'\n", bufData[1]); //Again, indices start at 0 making the 2nd element index 1
              //The buffer automatically cleans up its memory when it goes out of scope

              You still need to call delete pData; if the buffer was created with Buffer pData = new Buffer(64);

              N 1 Reply Last reply
              0
              • G goldenrose9

                yes, 0 to 63, but i was in search of a function that returns the bound of an array, as in vb UBound() and LBound() function is used. Is there any similer function that helps to acheive bounds in c++

                Some Day I Will Prove MySelf :: GOLD

                C Offline
                C Offline
                Cool_Dev
                wrote on last edited by
                #8

                Nope. :) Otherwise you have to go for STL container classes in C++, collection classes in MFC, or your own array class to achieve this.

                G 1 Reply Last reply
                0
                • G goldenrose9

                  how to get the upper bound and lower bound of an array BYTE *pByte = new BYTE[64];

                  Some Day I Will Prove MySelf :: GOLD

                  modified on Friday, February 4, 2011 8:37 AM

                  H Offline
                  H Offline
                  Hans Dietrich
                  wrote on last edited by
                  #9

                  VS provides the macro

                  #define _countof(array) (sizeof(array)/sizeof(array[0]))

                  [Note: this can't be used for arrays allocated via new.]

                  Best wishes, Hans


                  [Hans Dietrich Software]

                  1 Reply Last reply
                  0
                  • A Andrew Brock

                    Since it is C++, why not make your own little class that stores the length of the array.

                    class Buffer {
                    public:
                    Buffer(unsigned nSize) : m_pBuffer(NULL), m_nLength(nSize) {
                    if (nSize > 0) {
                    m_pBuffer = new BYTE[nSize];
                    if (m_pBuffer == NULL) {
                    m_nLength = 0;
                    //you ran out of memory, do something
                    }
                    }
                    }
                    ~Buffer() {
                    if (m_pBuffer != NULL) {
                    delete []m_pBuffer;
                    }
                    }
                    unsigned GetLength() {
                    return m_nLength;
                    }
                    BYTE &operator[](unsigned nIndex) {
                    if (nIndex >= m_nLength) {
                    //error somehow
                    }
                    return m_pBuffer[nIndex];
                    }
                    BYTE *operator*() {
                    return m_pBuffer;
                    }

                    private:
                    	BYTE \*m\_pBuffer;
                    	unsigned nLength;
                    

                    };

                    Then you can use it like

                    Buffer bufData(64);
                    strcpy(*bufData, "hello world"); //the operator*() can be used to get a pointer to the data
                    //print the length
                    printf("The valid range is 0-%u\n", bufData.GetLength() - 1); //remember indices start at 0 and go to n-1
                    printf("The 2nd character is \'%c\'\n", bufData[1]); //Again, indices start at 0 making the 2nd element index 1
                    //The buffer automatically cleans up its memory when it goes out of scope

                    You still need to call delete pData; if the buffer was created with Buffer pData = new Buffer(64);

                    N Offline
                    N Offline
                    Niklas L
                    wrote on last edited by
                    #10

                    Problem is, when you write things like this yourself, you always forget something :) . Like declaring GetLength() const for instance, and having a const accessor. std::vector<BYTE> is the way to go.

                    home

                    1 Reply Last reply
                    0
                    • G goldenrose9

                      yes, 0 to 63, but i was in search of a function that returns the bound of an array, as in vb UBound() and LBound() function is used. Is there any similer function that helps to acheive bounds in c++

                      Some Day I Will Prove MySelf :: GOLD

                      S Offline
                      S Offline
                      Stefan_Lang
                      wrote on last edited by
                      #11

                      pByte as you declared is just a pointer. You are putting the semantics of an array into it by assigning the memory for it. But the compiler has no way of knowing what, exactly this pointer is pointing to at any time. In other words, no, the compiler cannot know what the semantics of the memory is that any pointer is pointing to, nor can any function. That said, the above is not entirely true: when you compile your program in debug mode and check what new (and delete) actually do, you will find that, internally, the system's memory manager indeed does store information about the memory being allocated, including the information whether this memory is for an array of objects or just one object. Therefore, if you try to release an array using delete rather than delete [], you will get a runtime error! If you want to, you can replace the system's operator new and delete with your own implementations. These could then store additional information, such as the number of elements being allocated, and then you could implement a function UBound() that takes advantage of that hidden information.

                      N D 2 Replies Last reply
                      0
                      • C Cool_Dev

                        Nope. :) Otherwise you have to go for STL container classes in C++, collection classes in MFC, or your own array class to achieve this.

                        G Offline
                        G Offline
                        goldenrose9
                        wrote on last edited by
                        #12

                        thanx.:thumbsup:

                        Some Day I Will Prove MySelf :: GOLD

                        1 Reply Last reply
                        0
                        • S Stefan_Lang

                          pByte as you declared is just a pointer. You are putting the semantics of an array into it by assigning the memory for it. But the compiler has no way of knowing what, exactly this pointer is pointing to at any time. In other words, no, the compiler cannot know what the semantics of the memory is that any pointer is pointing to, nor can any function. That said, the above is not entirely true: when you compile your program in debug mode and check what new (and delete) actually do, you will find that, internally, the system's memory manager indeed does store information about the memory being allocated, including the information whether this memory is for an array of objects or just one object. Therefore, if you try to release an array using delete rather than delete [], you will get a runtime error! If you want to, you can replace the system's operator new and delete with your own implementations. These could then store additional information, such as the number of elements being allocated, and then you could implement a function UBound() that takes advantage of that hidden information.

                          N Offline
                          N Offline
                          Niklas L
                          wrote on last edited by
                          #13

                          hmm, never thought of that. Rather clever!

                          home

                          1 Reply Last reply
                          0
                          • S Stefan_Lang

                            pByte as you declared is just a pointer. You are putting the semantics of an array into it by assigning the memory for it. But the compiler has no way of knowing what, exactly this pointer is pointing to at any time. In other words, no, the compiler cannot know what the semantics of the memory is that any pointer is pointing to, nor can any function. That said, the above is not entirely true: when you compile your program in debug mode and check what new (and delete) actually do, you will find that, internally, the system's memory manager indeed does store information about the memory being allocated, including the information whether this memory is for an array of objects or just one object. Therefore, if you try to release an array using delete rather than delete [], you will get a runtime error! If you want to, you can replace the system's operator new and delete with your own implementations. These could then store additional information, such as the number of elements being allocated, and then you could implement a function UBound() that takes advantage of that hidden information.

                            D Offline
                            D Offline
                            David Crow
                            wrote on last edited by
                            #14

                            Stefan63 wrote:

                            If you want to, you can replace the system's operator new and delete with your own implementations. These could then store additional information, such as the number of elements being allocated...

                            Which is essentially what happens in debug mode.

                            "One man's wage rise is another man's price increase." - Harold Wilson

                            "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                            "Man who follows car will be exhausted." - Confucius

                            1 Reply Last reply
                            0
                            • G goldenrose9

                              how to get the upper bound and lower bound of an array BYTE *pByte = new BYTE[64];

                              Some Day I Will Prove MySelf :: GOLD

                              modified on Friday, February 4, 2011 8:37 AM

                              L Offline
                              L Offline
                              Lost User
                              wrote on last edited by
                              #15

                              goldenrose9 wrote:

                              how to get the upper bound and lower bound of an array BYTE *pByte = new BYTE[64];

                              In C/C++ there is nothing like lower bound of array is zero. Upper bound is 63 as you specified 64 (bcoz array starts with zero so 64-1).

                              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