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. when pushback function of std::vector make program crash? How to handle it?
Forum Updated to NodeBB v4.3 + New Features

when pushback function of std::vector make program crash? How to handle it?

Scheduled Pinned Locked Moved Solved C++ Gurus
9 Posts 4 Posters 1.1k 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.
  • Q Offline
    Q Offline
    Qt embedded developer
    wrote on 17 Dec 2021, 14:34 last edited by Qt embedded developer
    #1

    Hi have faced one issue when my application get crash due to below line:

    strVehicleNo = "abc";

    VecVehicleCodeDigit.push_back(strVehicleNo.substr(strVehicleNo.length() - 4));

    i want to find generic solution for this problem.

    J 1 Reply Last reply 17 Dec 2021, 14:50
    0
    • J Offline
      J Offline
      JoeCFD
      wrote on 17 Dec 2021, 14:50 last edited by JoeCFD
      #3
      if ( strVehicleNo.length()  > 4 ) {
          VecVehicleCodeDigit.push_back(strVehicleNo.substr(strVehicleNo.length() - 4));
      }
      else {
          std::cout << " string " << strVehicleNo << " is ignored" <<std::endl;
      }
      
      Q 1 Reply Last reply 18 Dec 2021, 06:31
      3
      • Q Qt embedded developer
        17 Dec 2021, 14:34

        Hi have faced one issue when my application get crash due to below line:

        strVehicleNo = "abc";

        VecVehicleCodeDigit.push_back(strVehicleNo.substr(strVehicleNo.length() - 4));

        i want to find generic solution for this problem.

        J Offline
        J Offline
        JonB
        wrote on 17 Dec 2021, 14:50 last edited by JonB
        #2

        @Qt-embedded-developer

        • Check your VecVehicleCodeDigit.
        • strVehicleNo.substr(strVehicleNo.length() - 4): given that you can see your string is 3 characters long, what do you expect from this? Strange question....
        Q 1 Reply Last reply 18 Dec 2021, 06:34
        1
        • J Offline
          J Offline
          JoeCFD
          wrote on 17 Dec 2021, 14:50 last edited by JoeCFD
          #3
          if ( strVehicleNo.length()  > 4 ) {
              VecVehicleCodeDigit.push_back(strVehicleNo.substr(strVehicleNo.length() - 4));
          }
          else {
              std::cout << " string " << strVehicleNo << " is ignored" <<std::endl;
          }
          
          Q 1 Reply Last reply 18 Dec 2021, 06:31
          3
          • J JoeCFD
            17 Dec 2021, 14:50
            if ( strVehicleNo.length()  > 4 ) {
                VecVehicleCodeDigit.push_back(strVehicleNo.substr(strVehicleNo.length() - 4));
            }
            else {
                std::cout << " string " << strVehicleNo << " is ignored" <<std::endl;
            }
            
            Q Offline
            Q Offline
            Qt embedded developer
            wrote on 18 Dec 2021, 06:31 last edited by
            #4

            @JoeCFD i want to know pushback function why make program crash. Is it unauthorized memory access problem?

            1 Reply Last reply
            0
            • J JonB
              17 Dec 2021, 14:50

              @Qt-embedded-developer

              • Check your VecVehicleCodeDigit.
              • strVehicleNo.substr(strVehicleNo.length() - 4): given that you can see your string is 3 characters long, what do you expect from this? Strange question....
              Q Offline
              Q Offline
              Qt embedded developer
              wrote on 18 Dec 2021, 06:34 last edited by
              #5

              @JonB hi this data come from server and it need to be like "GJ-05-4578". But when server data wrong come like " M3". Then this crash happen.

              J 1 Reply Last reply 18 Dec 2021, 08:27
              0
              • Q Qt embedded developer
                18 Dec 2021, 06:34

                @JonB hi this data come from server and it need to be like "GJ-05-4578". But when server data wrong come like " M3". Then this crash happen.

                J Offline
                J Offline
                JonB
                wrote on 18 Dec 2021, 08:27 last edited by JonB
                #6

                @Qt-embedded-developer
                Did you read what I wrote and actually think about it?

                strVehicleNo.substr(strVehicleNo.length() - 4): given that you can see your string is 3 characters long, what do you expect from this?

                Obviously M3 is even shorter. So you cannot subtract 4 from its length, can you?

                But when server data wrong come like " M3". Then this crash happen.

                Either sort out why "wrong data" can come from server, or code to allow for it so your client does not crash.

                I really do not understand what your actual question is or what you expect people to say about it.

                Q 1 Reply Last reply 18 Dec 2021, 14:32
                2
                • J JonB
                  18 Dec 2021, 08:27

                  @Qt-embedded-developer
                  Did you read what I wrote and actually think about it?

                  strVehicleNo.substr(strVehicleNo.length() - 4): given that you can see your string is 3 characters long, what do you expect from this?

                  Obviously M3 is even shorter. So you cannot subtract 4 from its length, can you?

                  But when server data wrong come like " M3". Then this crash happen.

                  Either sort out why "wrong data" can come from server, or code to allow for it so your client does not crash.

                  I really do not understand what your actual question is or what you expect people to say about it.

                  Q Offline
                  Q Offline
                  Qt embedded developer
                  wrote on 18 Dec 2021, 14:32 last edited by
                  #7

                  @JonB i want to know pushback function why make program crash. Is it unauthorized memory access problem?

                  S 1 Reply Last reply 18 Dec 2021, 15:00
                  0
                  • Q Qt embedded developer
                    18 Dec 2021, 14:32

                    @JonB i want to know pushback function why make program crash. Is it unauthorized memory access problem?

                    S Offline
                    S Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on 18 Dec 2021, 15:00 last edited by
                    #8

                    Hi,

                    @Qt-embedded-developer did you actually to do the math that @JonB suggested ?

                    It's not the push_back call that is the issue, it's your substr call.

                    Use a debugger.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    1
                    • Q Offline
                      Q Offline
                      Qt embedded developer
                      wrote on 19 Dec 2021, 13:54 last edited by
                      #9

                      Thank you all dear for answers...

                      1 Reply Last reply
                      0

                      2/9

                      17 Dec 2021, 14:50

                      7 unread
                      • Login

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