Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. How to Get Length Of Int* Array
Forum Updated to NodeBB v4.3 + New Features

How to Get Length Of Int* Array

Scheduled Pinned Locked Moved Solved C++ Gurus
26 Posts 8 Posters 5.8k Views 2 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.
  • K Offline
    K Offline
    Ketan__Patel__0011
    wrote on 11 Feb 2021, 14:09 last edited by
    #1

    Hello Friends And Qt Experts
    Currently I am Facing int* array length finding Problem

    I want to find the Length of Dynamic array

    i am able to find the length of int[] but i can't find the length of int*

    i want to know how many elements are in int* Array

    For Example ::

    int *Temp = new int[50];
    Temp[0] = 10;
    Temp[1] = 20;
    Temp[2] = 30;
    Temp[3] = 40;
    Temp[4] = 50;
    Temp[5] = 60;
    Temp[6] = 70;
    Temp[7] = 80;
    Temp[8] = 90;
    Temp[9] = 100;
    

    I want to find The Length of Temp
    Here you can see i have add 10 Elements in My Temp Array

    Is There any way to Find the length of any dynamic Array at runtime ?

    J 1 Reply Last reply 11 Feb 2021, 14:14
    0
    • K Ketan__Patel__0011
      11 Feb 2021, 14:50

      @J-Hilk

      I was Try this But Application is Still Crashing

      	std::vector<std::vector<std::vector<int>>> Temp;
      
      	Temp.at(0).at(0).push_back(10);
      	Temp.at(0).at(0).push_back(20);
      	Temp.at(0).at(0).push_back(30);
      	Temp.at(0).at(0).push_back(40);
      
      	cout << Temp.at(0).at(0).at(0);
      	cout << Temp.at(0).at(0).at(1);
      	cout << Temp.at(0).at(0).at(2);
      	cout << Temp.at(0).at(0).at(3);
      
      
      J Offline
      J Offline
      J.Hilk
      Moderators
      wrote on 11 Feb 2021, 14:52 last edited by J.Hilk 2 Nov 2021, 14:52
      #12

      @Ketan__Patel__0011

          std::vector<std::vector<std::vector<int>>> Temp;
      
          int targetSize = 4;
          Temp.resize(targetSize,std::vector<std::vector<int> >(targetSize,std::vector<int>(targetSize)));
      
          Temp.at(0).at(0).at(0) = 10;
          Temp.at(0).at(0).at(1) = 20;
          Temp.at(0).at(0).at(2) = 30;
          Temp.at(0).at(0).at(3) = 40;
      

      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      3
      • A Offline
        A Offline
        Asperamanca
        wrote on 11 Feb 2021, 14:11 last edited by
        #2

        No, there is no way. int* is only a pointer, it doesn't even know it's an array.

        Why not use a proper array?

        K 1 Reply Last reply 11 Feb 2021, 14:14
        5
        • C Offline
          C Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 11 Feb 2021, 14:14 last edited by
          #3

          @Ketan__Patel__0011 said in How to Get Length Of Int* Array:

          Is There any way to Find the length of any dynamic Array at runtime ?

          Use a proper container.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          K 1 Reply Last reply 11 Feb 2021, 14:15
          2
          • A Asperamanca
            11 Feb 2021, 14:11

            No, there is no way. int* is only a pointer, it doesn't even know it's an array.

            Why not use a proper array?

            K Offline
            K Offline
            Ketan__Patel__0011
            wrote on 11 Feb 2021, 14:14 last edited by
            #4

            @Asperamanca

            There are The Lot's of reason for it like Runtime Size Or Memory Allocation Based On My Data

            1 Reply Last reply
            0
            • K Ketan__Patel__0011
              11 Feb 2021, 14:09

              Hello Friends And Qt Experts
              Currently I am Facing int* array length finding Problem

              I want to find the Length of Dynamic array

              i am able to find the length of int[] but i can't find the length of int*

              i want to know how many elements are in int* Array

              For Example ::

              int *Temp = new int[50];
              Temp[0] = 10;
              Temp[1] = 20;
              Temp[2] = 30;
              Temp[3] = 40;
              Temp[4] = 50;
              Temp[5] = 60;
              Temp[6] = 70;
              Temp[7] = 80;
              Temp[8] = 90;
              Temp[9] = 100;
              

              I want to find The Length of Temp
              Here you can see i have add 10 Elements in My Temp Array

              Is There any way to Find the length of any dynamic Array at runtime ?

              J Offline
              J Offline
              J.Hilk
              Moderators
              wrote on 11 Feb 2021, 14:14 last edited by
              #5

              @Ketan__Patel__0011

              like @Asperamanca said, you can't,
              why not use std::vector ? thats the perfect substitution


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              K 1 Reply Last reply 11 Feb 2021, 14:17
              0
              • C Christian Ehrlicher
                11 Feb 2021, 14:14

                @Ketan__Patel__0011 said in How to Get Length Of Int* Array:

                Is There any way to Find the length of any dynamic Array at runtime ?

                Use a proper container.

                K Offline
                K Offline
                Ketan__Patel__0011
                wrote on 11 Feb 2021, 14:15 last edited by
                #6

                @Christian-Ehrlicher

                Thank for the reply

                can you please elaborate ?

                1 Reply Last reply
                0
                • J J.Hilk
                  11 Feb 2021, 14:14

                  @Ketan__Patel__0011

                  like @Asperamanca said, you can't,
                  why not use std::vector ? thats the perfect substitution

                  K Offline
                  K Offline
                  Ketan__Patel__0011
                  wrote on 11 Feb 2021, 14:17 last edited by Ketan__Patel__0011 2 Nov 2021, 14:21
                  #7

                  @J-Hilk

                  thanks for your reply

                  std::vector is good idea but some issues like Memory location access or Memory Allocation

                  And Very difficult for Multidimensional

                  J 1 Reply Last reply 11 Feb 2021, 14:31
                  0
                  • K Ketan__Patel__0011
                    11 Feb 2021, 14:17

                    @J-Hilk

                    thanks for your reply

                    std::vector is good idea but some issues like Memory location access or Memory Allocation

                    And Very difficult for Multidimensional

                    J Offline
                    J Offline
                    J.Hilk
                    Moderators
                    wrote on 11 Feb 2021, 14:31 last edited by
                    #8

                    @Ketan__Patel__0011 said in How to Get Length Of Int* Array:

                    std::vector is good idea but some issues like Memory location access or Memory Allocation

                    what do you mean? vector is literally a heap allocated array

                    And Very difficult for Multidimensional

                    multidimensionalism is an illusion anyway but if you want the [][] or at().at() syntax available to you

                    std::vector<std::vector<int>>
                    

                    here, admittedly, you may get into differences between std::vector and good old c-array, memory wise


                    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                    Q: What's that?
                    A: It's blue light.
                    Q: What does it do?
                    A: It turns blue.

                    K 1 Reply Last reply 11 Feb 2021, 14:42
                    1
                    • J J.Hilk
                      11 Feb 2021, 14:31

                      @Ketan__Patel__0011 said in How to Get Length Of Int* Array:

                      std::vector is good idea but some issues like Memory location access or Memory Allocation

                      what do you mean? vector is literally a heap allocated array

                      And Very difficult for Multidimensional

                      multidimensionalism is an illusion anyway but if you want the [][] or at().at() syntax available to you

                      std::vector<std::vector<int>>
                      

                      here, admittedly, you may get into differences between std::vector and good old c-array, memory wise

                      K Offline
                      K Offline
                      Ketan__Patel__0011
                      wrote on 11 Feb 2021, 14:42 last edited by Ketan__Patel__0011 2 Nov 2021, 14:42
                      #9

                      @J-Hilk

                      Application crashed When i used this Syntax for Vector

                              std::vector<std::vector<std::vector<int>>> Temp;
                      
                      	Temp.at(0).at(0).at(0) = 10; /// My Application Crashed at this point
                      	Temp.at(0).at(0).at(1) = 20;
                      	Temp.at(0).at(0).at(2) = 30;
                      	Temp.at(0).at(0).at(3) = 40;
                      
                      	cout << Temp[0][0][0];
                      	cout << Temp[0][0][1];
                      	cout << Temp[0][0][2];
                      	cout << Temp[0][0][3];
                      

                      What's Wrong in this ?

                      J 1 Reply Last reply 11 Feb 2021, 14:48
                      0
                      • K Ketan__Patel__0011
                        11 Feb 2021, 14:42

                        @J-Hilk

                        Application crashed When i used this Syntax for Vector

                                std::vector<std::vector<std::vector<int>>> Temp;
                        
                        	Temp.at(0).at(0).at(0) = 10; /// My Application Crashed at this point
                        	Temp.at(0).at(0).at(1) = 20;
                        	Temp.at(0).at(0).at(2) = 30;
                        	Temp.at(0).at(0).at(3) = 40;
                        
                        	cout << Temp[0][0][0];
                        	cout << Temp[0][0][1];
                        	cout << Temp[0][0][2];
                        	cout << Temp[0][0][3];
                        

                        What's Wrong in this ?

                        J Offline
                        J Offline
                        J.Hilk
                        Moderators
                        wrote on 11 Feb 2021, 14:48 last edited by
                        #10

                        @Ketan__Patel__0011
                        you need to either reserve a size for your vectors or use the vector functions to append data

                        push_back, resize, reserve etc

                        https://en.cppreference.com/w/cpp/container/vector

                        or use initializer lists


                        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                        Q: What's that?
                        A: It's blue light.
                        Q: What does it do?
                        A: It turns blue.

                        K 1 Reply Last reply 11 Feb 2021, 14:50
                        1
                        • J J.Hilk
                          11 Feb 2021, 14:48

                          @Ketan__Patel__0011
                          you need to either reserve a size for your vectors or use the vector functions to append data

                          push_back, resize, reserve etc

                          https://en.cppreference.com/w/cpp/container/vector

                          or use initializer lists

                          K Offline
                          K Offline
                          Ketan__Patel__0011
                          wrote on 11 Feb 2021, 14:50 last edited by
                          #11

                          @J-Hilk

                          I was Try this But Application is Still Crashing

                          	std::vector<std::vector<std::vector<int>>> Temp;
                          
                          	Temp.at(0).at(0).push_back(10);
                          	Temp.at(0).at(0).push_back(20);
                          	Temp.at(0).at(0).push_back(30);
                          	Temp.at(0).at(0).push_back(40);
                          
                          	cout << Temp.at(0).at(0).at(0);
                          	cout << Temp.at(0).at(0).at(1);
                          	cout << Temp.at(0).at(0).at(2);
                          	cout << Temp.at(0).at(0).at(3);
                          
                          
                          J K 2 Replies Last reply 11 Feb 2021, 14:52
                          0
                          • K Ketan__Patel__0011
                            11 Feb 2021, 14:50

                            @J-Hilk

                            I was Try this But Application is Still Crashing

                            	std::vector<std::vector<std::vector<int>>> Temp;
                            
                            	Temp.at(0).at(0).push_back(10);
                            	Temp.at(0).at(0).push_back(20);
                            	Temp.at(0).at(0).push_back(30);
                            	Temp.at(0).at(0).push_back(40);
                            
                            	cout << Temp.at(0).at(0).at(0);
                            	cout << Temp.at(0).at(0).at(1);
                            	cout << Temp.at(0).at(0).at(2);
                            	cout << Temp.at(0).at(0).at(3);
                            
                            
                            J Offline
                            J Offline
                            J.Hilk
                            Moderators
                            wrote on 11 Feb 2021, 14:52 last edited by J.Hilk 2 Nov 2021, 14:52
                            #12

                            @Ketan__Patel__0011

                                std::vector<std::vector<std::vector<int>>> Temp;
                            
                                int targetSize = 4;
                                Temp.resize(targetSize,std::vector<std::vector<int> >(targetSize,std::vector<int>(targetSize)));
                            
                                Temp.at(0).at(0).at(0) = 10;
                                Temp.at(0).at(0).at(1) = 20;
                                Temp.at(0).at(0).at(2) = 30;
                                Temp.at(0).at(0).at(3) = 40;
                            

                            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                            Q: What's that?
                            A: It's blue light.
                            Q: What does it do?
                            A: It turns blue.

                            1 Reply Last reply
                            3
                            • K Ketan__Patel__0011
                              11 Feb 2021, 14:50

                              @J-Hilk

                              I was Try this But Application is Still Crashing

                              	std::vector<std::vector<std::vector<int>>> Temp;
                              
                              	Temp.at(0).at(0).push_back(10);
                              	Temp.at(0).at(0).push_back(20);
                              	Temp.at(0).at(0).push_back(30);
                              	Temp.at(0).at(0).push_back(40);
                              
                              	cout << Temp.at(0).at(0).at(0);
                              	cout << Temp.at(0).at(0).at(1);
                              	cout << Temp.at(0).at(0).at(2);
                              	cout << Temp.at(0).at(0).at(3);
                              
                              
                              K Offline
                              K Offline
                              KroMignon
                              wrote on 11 Feb 2021, 14:53 last edited by KroMignon 2 Nov 2021, 14:57
                              #13

                              @Ketan__Patel__0011 said in How to Get Length Of Int* Array:

                              was Try this But Application is Still Crashing

                              	std::vector<std::vector<std::vector<int>>> Temp;
                              
                              	Temp.at(0).at(0).push_back(10);
                              

                              Yes of course, the 2 first vector are not defined!

                              You could read this article to learn how to work with multidimensionalvectors => https://mklimenko.github.io/english/2019/08/17/multidimensional-vector-allocation/

                              It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                              1 Reply Last reply
                              2
                              • A Offline
                                A Offline
                                Asperamanca
                                wrote on 11 Feb 2021, 15:04 last edited by
                                #14

                                Maybe something like this helps:
                                https://www.learncpp.com/cpp-tutorial/an-introduction-to-stdvector/

                                It also refers to std::array, which might be an alternative if you really only need fixed-sized arrays.

                                K 1 Reply Last reply 11 Feb 2021, 15:27
                                1
                                • K Offline
                                  K Offline
                                  Ketan__Patel__0011
                                  wrote on 11 Feb 2021, 15:09 last edited by Ketan__Patel__0011 2 Nov 2021, 15:16
                                  #15

                                  Thanks to All for your reply

                                  i can't find the length of the Vector

                                  K 1 Reply Last reply 11 Feb 2021, 15:20
                                  0
                                  • K Ketan__Patel__0011
                                    11 Feb 2021, 15:09

                                    Thanks to All for your reply

                                    i can't find the length of the Vector

                                    K Offline
                                    K Offline
                                    KroMignon
                                    wrote on 11 Feb 2021, 15:20 last edited by
                                    #16

                                    @Ketan__Patel__0011 have you really searched ? ==> https://www.cplusplus.com/reference/vector/vector/size/

                                    It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                                    K 1 Reply Last reply 11 Feb 2021, 15:30
                                    0
                                    • A Asperamanca
                                      11 Feb 2021, 15:04

                                      Maybe something like this helps:
                                      https://www.learncpp.com/cpp-tutorial/an-introduction-to-stdvector/

                                      It also refers to std::array, which might be an alternative if you really only need fixed-sized arrays.

                                      K Offline
                                      K Offline
                                      Ketan__Patel__0011
                                      wrote on 11 Feb 2021, 15:27 last edited by
                                      #17

                                      @Asperamanca
                                      thanks for your reply

                                      But Fixed Sized array not useful in my case

                                      1 Reply Last reply
                                      0
                                      • K KroMignon
                                        11 Feb 2021, 15:20

                                        @Ketan__Patel__0011 have you really searched ? ==> https://www.cplusplus.com/reference/vector/vector/size/

                                        K Offline
                                        K Offline
                                        Ketan__Patel__0011
                                        wrote on 11 Feb 2021, 15:30 last edited by
                                        #18

                                        @KroMignon

                                        Yes i see this example

                                        They Are Declare Singla Dimension Vector and Add First 10 Element and Again Add 10 Element After the First 10th Element Index and in last step they are remove the element and display the size of vector not a length

                                        K 1 Reply Last reply 11 Feb 2021, 15:36
                                        0
                                        • K Ketan__Patel__0011
                                          11 Feb 2021, 15:30

                                          @KroMignon

                                          Yes i see this example

                                          They Are Declare Singla Dimension Vector and Add First 10 Element and Again Add 10 Element After the First 10th Element Index and in last step they are remove the element and display the size of vector not a length

                                          K Offline
                                          K Offline
                                          KroMignon
                                          wrote on 11 Feb 2021, 15:36 last edited by
                                          #19

                                          @Ketan__Patel__0011 said in How to Get Length Of Int* Array:

                                          display the size of vector not a length

                                          I don't understand what you want?!?
                                          By simply reading std::vector() documentation, you could find yourself:

                                          • vector::size(): Returns the number of elements in the vector.
                                          • vector::capacity(): Returns the size of the storage space currently allocated for the vector, expressed in terms of elements.

                                          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                                          K 1 Reply Last reply 11 Feb 2021, 15:57
                                          0
                                          • K KroMignon
                                            11 Feb 2021, 15:36

                                            @Ketan__Patel__0011 said in How to Get Length Of Int* Array:

                                            display the size of vector not a length

                                            I don't understand what you want?!?
                                            By simply reading std::vector() documentation, you could find yourself:

                                            • vector::size(): Returns the number of elements in the vector.
                                            • vector::capacity(): Returns the size of the storage space currently allocated for the vector, expressed in terms of elements.
                                            K Offline
                                            K Offline
                                            Ketan__Patel__0011
                                            wrote on 11 Feb 2021, 15:57 last edited by
                                            #20

                                            @KroMignon

                                            i want to find how many elements are in the My Vector

                                            i was used vector::size() function but it is return to me total size of vector

                                            for example

                                            my Vector can store 10 elements but i am add only 6 elements

                                            so my Vector length is 6 And Vector size is 10

                                            K C 2 Replies Last reply 11 Feb 2021, 16:21
                                            0

                                            10/26

                                            11 Feb 2021, 14:48

                                            topic:navigator.unread, 16
                                            • Login

                                            • Login or register to search.
                                            10 out of 26
                                            • First post
                                              10/26
                                              Last post
                                            0
                                            • Categories
                                            • Recent
                                            • Tags
                                            • Popular
                                            • Users
                                            • Groups
                                            • Search
                                            • Get Qt Extensions
                                            • Unsolved