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. sizeof(float)/sizeof(float[0])

sizeof(float)/sizeof(float[0])

Scheduled Pinned Locked Moved C / C++ / MFC
questiondata-structures
15 Posts 6 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.
  • A Offline
    A Offline
    Alex Cutovoi
    wrote on last edited by
    #1

    Hi fellows I have a single question. I have a float array that have 6 elements all of them initialized with values. I need to find out the size of this array. Let's called my float array of theArray. I use this commom formula: sizeof(theArray)/sizeof(theArray[0]). My results are strange, because it returns 1. I use this code to see the values: char cTemp[50]; sprintf(cTemp, "%d %d %d", sizeof(dVertexes), sizeof(dVertexes[0]), sizeof(dVertexes)/sizeof(dVertexes[0])); MessageBox(NULL, cTemp, "", MB_OK); In the first and in the second values it returns "4", and in the third value it returns "1". Why it returns 1? Did this operation returns the correct size of array(in my case "6")? thanks for the support

    C J Z 3 Replies Last reply
    0
    • A Alex Cutovoi

      Hi fellows I have a single question. I have a float array that have 6 elements all of them initialized with values. I need to find out the size of this array. Let's called my float array of theArray. I use this commom formula: sizeof(theArray)/sizeof(theArray[0]). My results are strange, because it returns 1. I use this code to see the values: char cTemp[50]; sprintf(cTemp, "%d %d %d", sizeof(dVertexes), sizeof(dVertexes[0]), sizeof(dVertexes)/sizeof(dVertexes[0])); MessageBox(NULL, cTemp, "", MB_OK); In the first and in the second values it returns "4", and in the third value it returns "1". Why it returns 1? Did this operation returns the correct size of array(in my case "6")? thanks for the support

      C Offline
      C Offline
      Chris Losinger
      wrote on last edited by
      #2

      Alex Cutovoi wrote:

      Why it returns 1?

      4/4 = 1 4 = size of a pointer is dVertexes allocated dynamically ? if so, you can't use that sizeof trick to get its size. -c

      image processing | blogging

      A 2 Replies Last reply
      0
      • A Alex Cutovoi

        Hi fellows I have a single question. I have a float array that have 6 elements all of them initialized with values. I need to find out the size of this array. Let's called my float array of theArray. I use this commom formula: sizeof(theArray)/sizeof(theArray[0]). My results are strange, because it returns 1. I use this code to see the values: char cTemp[50]; sprintf(cTemp, "%d %d %d", sizeof(dVertexes), sizeof(dVertexes[0]), sizeof(dVertexes)/sizeof(dVertexes[0])); MessageBox(NULL, cTemp, "", MB_OK); In the first and in the second values it returns "4", and in the third value it returns "1". Why it returns 1? Did this operation returns the correct size of array(in my case "6")? thanks for the support

        J Offline
        J Offline
        James R Twine
        wrote on last edited by
        #3

        The math is working: 4/4=1.    The problem looks like dVertexes is not a true array.  It is an atomic type (double) or a pointer.    Note that using sizeof(...) in this way does not work correctly with pointers to arrays, even if they are pointers that have decayed from a true array.    Peace!

        -=- James


        If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
        Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
        DeleteFXPFiles & CheckFavorites (Please rate this post!)

        A 1 Reply Last reply
        0
        • C Chris Losinger

          Alex Cutovoi wrote:

          Why it returns 1?

          4/4 = 1 4 = size of a pointer is dVertexes allocated dynamically ? if so, you can't use that sizeof trick to get its size. -c

          image processing | blogging

          A Offline
          A Offline
          Alex Cutovoi
          wrote on last edited by
          #4

          my array is declared as following: float fVertexes[6]; I'm inserting this values; fVertexes[0] = 1.0; fVertexes[1] = 0.0; fVertexes[2] = -1.0; fVertexes[3] = 1.0; fVertexes[4] = -1.0; fVertexes[5] = -1.0; I'm passing this guy to a function, like this: primitive.Shape(GL_TRIANGLES, fVertexes, 1,1,1); Again, why it returns 1 if I don't create this guy dynamically?

          J G 2 Replies Last reply
          0
          • A Alex Cutovoi

            my array is declared as following: float fVertexes[6]; I'm inserting this values; fVertexes[0] = 1.0; fVertexes[1] = 0.0; fVertexes[2] = -1.0; fVertexes[3] = 1.0; fVertexes[4] = -1.0; fVertexes[5] = -1.0; I'm passing this guy to a function, like this: primitive.Shape(GL_TRIANGLES, fVertexes, 1,1,1); Again, why it returns 1 if I don't create this guy dynamically?

            J Offline
            J Offline
            James R Twine
            wrote on last edited by
            #5

            That is an example of the decay I mentioned in my post - when you pass the array, it becomes a pointer in the called function.    The function that has the pointer has no "internal" or automatic way of knowing how much the data points to.  That is why whenever you pass an array of data to a function, you usually have to tell the function how much valid data is in the array (or have a special element in the array to indicate the end of valid data).    Peace!

            -=- James


            If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
            Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
            DeleteFXPFiles & CheckFavorites (Please rate this post!)

            1 Reply Last reply
            0
            • J James R Twine

              The math is working: 4/4=1.    The problem looks like dVertexes is not a true array.  It is an atomic type (double) or a pointer.    Note that using sizeof(...) in this way does not work correctly with pointers to arrays, even if they are pointers that have decayed from a true array.    Peace!

              -=- James


              If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
              Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
              DeleteFXPFiles & CheckFavorites (Please rate this post!)

              A Offline
              A Offline
              Alex Cutovoi
              wrote on last edited by
              #6

              ok, so how can I discover the array size? I don't want to use vectors....

              J W 2 Replies Last reply
              0
              • A Alex Cutovoi

                ok, so how can I discover the array size? I don't want to use vectors....

                J Offline
                J Offline
                James R Twine
                wrote on last edited by
                #7

                You have to pass the size to the called function to let it know how much valid data exists.    Peace!

                -=- James


                If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
                Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
                DeleteFXPFiles & CheckFavorites (Please rate this post!)

                A 1 Reply Last reply
                0
                • A Alex Cutovoi

                  my array is declared as following: float fVertexes[6]; I'm inserting this values; fVertexes[0] = 1.0; fVertexes[1] = 0.0; fVertexes[2] = -1.0; fVertexes[3] = 1.0; fVertexes[4] = -1.0; fVertexes[5] = -1.0; I'm passing this guy to a function, like this: primitive.Shape(GL_TRIANGLES, fVertexes, 1,1,1); Again, why it returns 1 if I don't create this guy dynamically?

                  G Offline
                  G Offline
                  Galatei
                  wrote on last edited by
                  #8

                  Hi, Just modify Shape function by adding INT nCount after pointer to array of floats. Then in Shape function, calculate array size as nCount*sizeof(float). nCount should specify the size of the array. Regards

                  A 1 Reply Last reply
                  0
                  • A Alex Cutovoi

                    ok, so how can I discover the array size? I don't want to use vectors....

                    W Offline
                    W Offline
                    Waldermort
                    wrote on last edited by
                    #9

                    Depending on how often you are passing these arrays around and how frequently the size changes, you might want to declare a struct with the array and the size as members, then pass that to your functions.

                    typdef struct _double_array
                    {
                    double array[9];
                    int nCount;
                    } DOUBLE_ARRAY, *LPDOUBLE_ARRAY;

                    You can then pass the stuct 'DOUBLE_ARRAY' or a pointer to the struct 'LPDOUBLE_ARRAY'.

                    A 1 Reply Last reply
                    0
                    • G Galatei

                      Hi, Just modify Shape function by adding INT nCount after pointer to array of floats. Then in Shape function, calculate array size as nCount*sizeof(float). nCount should specify the size of the array. Regards

                      A Offline
                      A Offline
                      Alex Cutovoi
                      wrote on last edited by
                      #10

                      I made this: I've passed the size of the array(like you've said) and divided by float[0] and works fine

                      1 Reply Last reply
                      0
                      • J James R Twine

                        You have to pass the size to the called function to let it know how much valid data exists.    Peace!

                        -=- James


                        If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
                        Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
                        DeleteFXPFiles & CheckFavorites (Please rate this post!)

                        A Offline
                        A Offline
                        Alex Cutovoi
                        wrote on last edited by
                        #11

                        I've made this: primitive.Shape(GL_TRIANGLES, fVertexes, sizeof(fVertexes), 0,1,1); And in the function I'm divided the size of array by the size of first element in the array. Works fine, thanks for help

                        1 Reply Last reply
                        0
                        • W Waldermort

                          Depending on how often you are passing these arrays around and how frequently the size changes, you might want to declare a struct with the array and the size as members, then pass that to your functions.

                          typdef struct _double_array
                          {
                          double array[9];
                          int nCount;
                          } DOUBLE_ARRAY, *LPDOUBLE_ARRAY;

                          You can then pass the stuct 'DOUBLE_ARRAY' or a pointer to the struct 'LPDOUBLE_ARRAY'.

                          A Offline
                          A Offline
                          Alex Cutovoi
                          wrote on last edited by
                          #12

                          Thanks for the tip. It's a good idea

                          W 1 Reply Last reply
                          0
                          • C Chris Losinger

                            Alex Cutovoi wrote:

                            Why it returns 1?

                            4/4 = 1 4 = size of a pointer is dVertexes allocated dynamically ? if so, you can't use that sizeof trick to get its size. -c

                            image processing | blogging

                            A Offline
                            A Offline
                            Alex Cutovoi
                            wrote on last edited by
                            #13

                            I made some modifications and works fine. Thanks for the support

                            1 Reply Last reply
                            0
                            • A Alex Cutovoi

                              Thanks for the tip. It's a good idea

                              W Offline
                              W Offline
                              Waldermort
                              wrote on last edited by
                              #14

                              No problem. If you look into the windows api's, you will find that many of the functions are declared like this, you either have to set up a struct or you get one back. My current project calls on the need of dealing with several arrays all relating to the same info, so instead of passing all the arrays and sizes to each function, I simply pass a pointer to a struct.

                              1 Reply Last reply
                              0
                              • A Alex Cutovoi

                                Hi fellows I have a single question. I have a float array that have 6 elements all of them initialized with values. I need to find out the size of this array. Let's called my float array of theArray. I use this commom formula: sizeof(theArray)/sizeof(theArray[0]). My results are strange, because it returns 1. I use this code to see the values: char cTemp[50]; sprintf(cTemp, "%d %d %d", sizeof(dVertexes), sizeof(dVertexes[0]), sizeof(dVertexes)/sizeof(dVertexes[0])); MessageBox(NULL, cTemp, "", MB_OK); In the first and in the second values it returns "4", and in the third value it returns "1". Why it returns 1? Did this operation returns the correct size of array(in my case "6")? thanks for the support

                                Z Offline
                                Z Offline
                                Zac Howland
                                wrote on last edited by
                                #15

                                If the array is allocated on the heap, sizeof behaves differently than it does if it was on the stack.

                                int i = 0;
                                
                                float stackArray[5];
                                i = sizeof(stackArray) / sizeof(stackArray[0]);	// i is 5
                                // sizeof(stackArray) will return (5 * sizeof(float))
                                // sizeof(stackArray[0]) will return sizeof(float)
                                
                                float* heapArray = new float[5];
                                i = sizeof(heapArray) / sizeof(heapArray[0]);	// i is 1
                                // sizeof(heapArray) will return sizeof(float*)
                                // sizeof(heapArray[0]) will return sizeof(float)
                                
                                // Interesting Note (forget bounds errors here)
                                int myFunction(float* myArray)
                                {
                                	return sizeof(myArray) / sizeof(myArray[0]);
                                }
                                // above function will return heap results regardless of where
                                // the float array is actually allocated (that is, always 1)
                                

                                If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                                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