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. General question about arrays and I/o
Forum Updated to NodeBB v4.3 + New Features

General question about arrays and I/o

Scheduled Pinned Locked Moved Solved C++ Gurus
27 Posts 6 Posters 5.3k 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.
  • jsulmJ jsulm

    @Please_Help_me_D said in General question about arrays and I/o:

    data2 is now is equal to 3 and that is it. Is it correct?

    Yes it is, you can treat a pointer as an array (actually in C/C++ an array is simply a pointer to first element of the array). So, data2[0] == 3, data[1] == 4...
    Just do

    qDebug() << data2[1];
    

    and see.

    Regarding second question: this is debugger view. Your array is for sure 4 char in size. To verify do

    char_file[4] = 1;
    

    your app should crash.

    Please_Help_me_DP Offline
    Please_Help_me_DP Offline
    Please_Help_me_D
    wrote on last edited by
    #21

    @jsulm Yes that works:

        int data[5] = {1, 2, 3, 4, 5};
        int *data2 = &data[2]; // data2 is now [3, 4, 5].
        qDebug() << data2[2];
    

    But what's the magic behind that?:) I debug I can see that data2 has only a single number.

    But this doesn't crash and I can see some output in terminal (it is not 100 but some letters or signs as I think it is char) even if I lauch the program not in debug mode:

        int n = 2;
        char *char_file = new char[n]; // error: cannot initialize a variable of type 'char' with an rvalue of type 'char *'
        delete[] char_file; // Delete when not needed anymore
    
        char_file[5] = 100;
    
        std::cout << char_file[5];
    

    Are there in Qt the possibility to use command line when the program stopped in debug mode? For example if it's stopped and I want to do something in real time (while the program is topped)? Like in Matlab command line in debug mode

    JonBJ jsulmJ 2 Replies Last reply
    0
    • Please_Help_me_DP Please_Help_me_D

      @jsulm Yes that works:

          int data[5] = {1, 2, 3, 4, 5};
          int *data2 = &data[2]; // data2 is now [3, 4, 5].
          qDebug() << data2[2];
      

      But what's the magic behind that?:) I debug I can see that data2 has only a single number.

      But this doesn't crash and I can see some output in terminal (it is not 100 but some letters or signs as I think it is char) even if I lauch the program not in debug mode:

          int n = 2;
          char *char_file = new char[n]; // error: cannot initialize a variable of type 'char' with an rvalue of type 'char *'
          delete[] char_file; // Delete when not needed anymore
      
          char_file[5] = 100;
      
          std::cout << char_file[5];
      

      Are there in Qt the possibility to use command line when the program stopped in debug mode? For example if it's stopped and I want to do something in real time (while the program is topped)? Like in Matlab command line in debug mode

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #22

      @Please_Help_me_D said in General question about arrays and I/o:

      Are there in Qt the possibility to use command line when the program stopped in debug mode? For example if it's stopped and I want to do something in real time (while the program is topped)? Like in Matlab command line in debug mode

      No, this is a C++ compiled program (nothing to do with Qt), not Matlab/an interpreted language! You can print out values, and even at a pinch poke a value into a variable, but you can't start "telling" the debugger/program to go perform actions :)

      Please_Help_me_DP 1 Reply Last reply
      1
      • Please_Help_me_DP Please_Help_me_D

        @jsulm Yes that works:

            int data[5] = {1, 2, 3, 4, 5};
            int *data2 = &data[2]; // data2 is now [3, 4, 5].
            qDebug() << data2[2];
        

        But what's the magic behind that?:) I debug I can see that data2 has only a single number.

        But this doesn't crash and I can see some output in terminal (it is not 100 but some letters or signs as I think it is char) even if I lauch the program not in debug mode:

            int n = 2;
            char *char_file = new char[n]; // error: cannot initialize a variable of type 'char' with an rvalue of type 'char *'
            delete[] char_file; // Delete when not needed anymore
        
            char_file[5] = 100;
        
            std::cout << char_file[5];
        

        Are there in Qt the possibility to use command line when the program stopped in debug mode? For example if it's stopped and I want to do something in real time (while the program is topped)? Like in Matlab command line in debug mode

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by jsulm
        #23

        @Please_Help_me_D said in General question about arrays and I/o:

        But what's the magic behind that?:) I debug I can see that data2 has only a single number.

        Pointer magic :-) data2 is defined as pointer to int, that's why debugger only shows one value. But you as developer know that it's actually pointing to an array of int. Writing data2[1] is same as *(data2 + 1).
        *(data2 + 1) means: give me the value in memory at the position (data2 + 1) is pointing to.
        Keep in mind that in this case (data2 + 1) increments the pointer by 4 as sizeof(int) == 4.

        This should actually crash as you're accessing memory which was already freed:

        delete[] char_file; // Delete when not needed anymore
        char_file[5] = 100;
        std::cout << char_file[5];
        

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        2
        • JonBJ JonB

          @Please_Help_me_D said in General question about arrays and I/o:

          Are there in Qt the possibility to use command line when the program stopped in debug mode? For example if it's stopped and I want to do something in real time (while the program is topped)? Like in Matlab command line in debug mode

          No, this is a C++ compiled program (nothing to do with Qt), not Matlab/an interpreted language! You can print out values, and even at a pinch poke a value into a variable, but you can't start "telling" the debugger/program to go perform actions :)

          Please_Help_me_DP Offline
          Please_Help_me_DP Offline
          Please_Help_me_D
          wrote on last edited by
          #24

          @JonB thank you! Now I know that:)
          @jsulm ok that is interesting. I need to read about pointers
          I read about QVector and I can't solve the situation when I need to extract {4, 2, 3} elements from data (without loop) so that:
          data2[0] = data[4] = 5,
          data2[1] = data[2] = 3,
          data2[2] = data[3] = 4
          ,
          where data = {1, 2, 3, 4, 5};
          Seems to me that QVector::mid(int pos, int length = ...) can't do that. But of coarse:

              QVector<int> data = {1, 2, 3, 4, 5}; 
              QVector<int> data2 = data.mid(1,2); // data2 = {2, 3}
          

          this is also good to know for me.
          I'm trying to avoid loops here because it is like the main standart operation for me and I feel there should be a way to do that.

          JonBJ jsulmJ 2 Replies Last reply
          0
          • Please_Help_me_DP Please_Help_me_D

            @JonB thank you! Now I know that:)
            @jsulm ok that is interesting. I need to read about pointers
            I read about QVector and I can't solve the situation when I need to extract {4, 2, 3} elements from data (without loop) so that:
            data2[0] = data[4] = 5,
            data2[1] = data[2] = 3,
            data2[2] = data[3] = 4
            ,
            where data = {1, 2, 3, 4, 5};
            Seems to me that QVector::mid(int pos, int length = ...) can't do that. But of coarse:

                QVector<int> data = {1, 2, 3, 4, 5}; 
                QVector<int> data2 = data.mid(1,2); // data2 = {2, 3}
            

            this is also good to know for me.
            I'm trying to avoid loops here because it is like the main standart operation for me and I feel there should be a way to do that.

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #25

            @Please_Help_me_D
            I will say one thing about "loops": although you may try to avoid, it is likely that even if there is a library call that will do a loop. (Not always true, some adjacent elements may be implemented by a memmove or similar.) Even your Matlab or whatever may present an an operation as "non-loop" as far as you are concerned, but under the hood that is what it will have to do. C++ is "lower-level" and more "literal" about what is going on/has to go on than a higher level language like Matlab may suggest to you. Not saying you shouldn't ask, or try to avoid, but be aware it may be inevitable.

            In your example, btw, technically there is no loop. It's just what it looks like: 3 separate statements (which cannot be optimized for non-adjacent data). And someone here will tell you these will take about 3 nanoseconds to perform.

            Please_Help_me_DP 1 Reply Last reply
            1
            • JonBJ JonB

              @Please_Help_me_D
              I will say one thing about "loops": although you may try to avoid, it is likely that even if there is a library call that will do a loop. (Not always true, some adjacent elements may be implemented by a memmove or similar.) Even your Matlab or whatever may present an an operation as "non-loop" as far as you are concerned, but under the hood that is what it will have to do. C++ is "lower-level" and more "literal" about what is going on/has to go on than a higher level language like Matlab may suggest to you. Not saying you shouldn't ask, or try to avoid, but be aware it may be inevitable.

              In your example, btw, technically there is no loop. It's just what it looks like: 3 separate statements (which cannot be optimized for non-adjacent data). And someone here will tell you these will take about 3 nanoseconds to perform.

              Please_Help_me_DP Offline
              Please_Help_me_DP Offline
              Please_Help_me_D
              wrote on last edited by
              #26

              @JonB Yes I understand that many function that don't use explicitly loops they use them under the hood. But I believe that in computer science there are some tricks that I don't know and that provide high perfomance of operations and specialists who write Qt functions (or Matlab functions) they implement those tricks to achieve good optimization of code.
              I'm just starting learning Qt I think I just need experience in C++ :)

              1 Reply Last reply
              0
              • Please_Help_me_DP Please_Help_me_D

                @JonB thank you! Now I know that:)
                @jsulm ok that is interesting. I need to read about pointers
                I read about QVector and I can't solve the situation when I need to extract {4, 2, 3} elements from data (without loop) so that:
                data2[0] = data[4] = 5,
                data2[1] = data[2] = 3,
                data2[2] = data[3] = 4
                ,
                where data = {1, 2, 3, 4, 5};
                Seems to me that QVector::mid(int pos, int length = ...) can't do that. But of coarse:

                    QVector<int> data = {1, 2, 3, 4, 5}; 
                    QVector<int> data2 = data.mid(1,2); // data2 = {2, 3}
                

                this is also good to know for me.
                I'm trying to avoid loops here because it is like the main standart operation for me and I feel there should be a way to do that.

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #27

                @Please_Help_me_D said in General question about arrays and I/o:

                I need to extract {4, 2, 3}

                You will need to do it by yourself. There is nothing for that neither in Qt nor in C++ stdlib. Even if there would be it would do nothing else as iterating over {4, 2, 3} in a loop. So, I doubt you can optimise here much.

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                1

                • Login

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