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. Array Lengths?

Array Lengths?

Scheduled Pinned Locked Moved C / C++ / MFC
data-structureshelptutorialquestion
9 Posts 4 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.
  • M Offline
    M Offline
    Michael101
    wrote on last edited by
    #1

    Hey Everyone, I've got a short array that starts with 1 element (obviously) and it grows if it needs more elements. Untimately the array needs to grow as much as possible to suit the program.... I don't know however, how to return the size of the array. e.g. ShortArray[0] = 1; ShortArray[1] = 5; Length of the array is 2... Mean there are two elements. How do you return the length? Thanks for your help in advance.... I appreciate all your input :-) Cheers, Michael

    N D J 3 Replies Last reply
    0
    • M Michael101

      Hey Everyone, I've got a short array that starts with 1 element (obviously) and it grows if it needs more elements. Untimately the array needs to grow as much as possible to suit the program.... I don't know however, how to return the size of the array. e.g. ShortArray[0] = 1; ShortArray[1] = 5; Length of the array is 2... Mean there are two elements. How do you return the length? Thanks for your help in advance.... I appreciate all your input :-) Cheers, Michael

      N Offline
      N Offline
      Naveen
      wrote on last edited by
      #2

      Michael101 wrote:

      I've got a short array that starts with 1 element

      How did you declare the array?

      Michael101 wrote:

      Untimately the array needs to grow as much as possible to suit the program

      This means you dynamically allocate the memory?

      nave [OpenedFileFinder]

      M 1 Reply Last reply
      0
      • N Naveen

        Michael101 wrote:

        I've got a short array that starts with 1 element

        How did you declare the array?

        Michael101 wrote:

        Untimately the array needs to grow as much as possible to suit the program

        This means you dynamically allocate the memory?

        nave [OpenedFileFinder]

        M Offline
        M Offline
        Michael101
        wrote on last edited by
        #3

        register short* PrimeNumber = 0; That's how I declared the array.... I would change it if I need to though. Yeah I want allocate it more memory during runtime i.e it grows. Cheers, Michael

        N 1 Reply Last reply
        0
        • M Michael101

          Hey Everyone, I've got a short array that starts with 1 element (obviously) and it grows if it needs more elements. Untimately the array needs to grow as much as possible to suit the program.... I don't know however, how to return the size of the array. e.g. ShortArray[0] = 1; ShortArray[1] = 5; Length of the array is 2... Mean there are two elements. How do you return the length? Thanks for your help in advance.... I appreciate all your input :-) Cheers, Michael

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

          Use a std::vector and be done with it.


          "A good athlete is the result of a good and worthy opponent." - David Crow

          "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

          M 1 Reply Last reply
          0
          • M Michael101

            register short* PrimeNumber = 0; That's how I declared the array.... I would change it if I need to though. Yeah I want allocate it more memory during runtime i.e it grows. Cheers, Michael

            N Offline
            N Offline
            Naveen
            wrote on last edited by
            #5

            Michael101 wrote:

            I would change it if I need to though.

            The memory can be allocated like PrimeNumber = new short[2];// an array having length of 2 is created So 2 is length of array. Similarly you can put the number of element you want in the array in the place of 2.

            nave [OpenedFileFinder]

            M 1 Reply Last reply
            0
            • N Naveen

              Michael101 wrote:

              I would change it if I need to though.

              The memory can be allocated like PrimeNumber = new short[2];// an array having length of 2 is created So 2 is length of array. Similarly you can put the number of element you want in the array in the place of 2.

              nave [OpenedFileFinder]

              M Offline
              M Offline
              Michael101
              wrote on last edited by
              #6

              Do you know any methods or functions that return how many elements that are in the array? Like before Array[0] = 1; Array[1] = 2; There are "Two" elements in this array. Do you know of anything like that? Cheers again, Michael

              N 1 Reply Last reply
              0
              • M Michael101

                Do you know any methods or functions that return how many elements that are in the array? Like before Array[0] = 1; Array[1] = 2; There are "Two" elements in this array. Do you know of anything like that? Cheers again, Michael

                N Offline
                N Offline
                Naveen
                wrote on last edited by
                #7

                Sorry I dont get your question correctly. Suppose you are allocating a memeory with size 5. Then the length of the array is 5. And whats the confusion in that? you can get the size of memory that you dynamically allocated using new or malloc using the function _msize(). Is that what you want?

                nave [OpenedFileFinder]

                1 Reply Last reply
                0
                • D David Crow

                  Use a std::vector and be done with it.


                  "A good athlete is the result of a good and worthy opponent." - David Crow

                  "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                  M Offline
                  M Offline
                  Michael101
                  wrote on last edited by
                  #8

                  yep I agree.... Thank you for your help! Cheers

                  1 Reply Last reply
                  0
                  • M Michael101

                    Hey Everyone, I've got a short array that starts with 1 element (obviously) and it grows if it needs more elements. Untimately the array needs to grow as much as possible to suit the program.... I don't know however, how to return the size of the array. e.g. ShortArray[0] = 1; ShortArray[1] = 5; Length of the array is 2... Mean there are two elements. How do you return the length? Thanks for your help in advance.... I appreciate all your input :-) Cheers, Michael

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

                    Michael101 wrote:

                    ShortArray[0] = 1; ShortArray[1] = 5; Length of the array is 2... Mean there are two elements. How do you return the length?

                    const unsigned int length = sizeof(ShortArray)/sizeof(ShortArray[0]);

                    That is, you divide the amount (in bytes) of your array by the size (in bytes) of one element.


                    Though I speak with the tongues of men and of angels, and have not money, I am become as a sounding brass, or a tinkling cymbal.
                    George Orwell, "Keep the Aspidistra Flying", Opening words

                    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