Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Detecting QStack overflow
Forum Updated to NodeBB v4.3 + New Features

Detecting QStack overflow

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 5 Posters 1.1k 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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #4

    It depends on what you store and what could happen that fills your queue faster than it can process.

    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
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by Christian Ehrlicher
      #5

      @Alvein said in Detecting QStack overflow:

      Such as?

      You want to tell us that you've more than 8GB (*) of contiguous memory available and also use this?

      If you really want more, use std::vector or wait for Qt6

      (*): 2^31 * 4 bytes = 8589934592 ~ 8.5GB, assumption that you only want to store an integer in the vector.

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

      1 Reply Last reply
      0
      • AlveinA Offline
        AlveinA Offline
        Alvein
        wrote on last edited by
        #6

        Not sure if you people get my idea.

        It's not that I need more memory.

        It's that I want to detect when there's no more available memory for my QStack, to stop the recursive calls at that point.

        If QStack::push() returned a bool result, meaning allocation success or something alike, this would be easy.

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #7

          @Alvein said in Detecting QStack overflow:

          It's that I want to detect when there's no more available memory for my QStack, to stop the recursive calls at that point.

          I would guess it's too late then but: https://en.cppreference.com/w/cpp/memory/new/bad_alloc

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

          AlveinA 2 Replies Last reply
          2
          • Christian EhrlicherC Christian Ehrlicher

            @Alvein said in Detecting QStack overflow:

            It's that I want to detect when there's no more available memory for my QStack, to stop the recursive calls at that point.

            I would guess it's too late then but: https://en.cppreference.com/w/cpp/memory/new/bad_alloc

            AlveinA Offline
            AlveinA Offline
            Alvein
            wrote on last edited by
            #8

            @Christian-Ehrlicher

            OK. Will try that. Thanks. :)

            1 Reply Last reply
            0
            • Christian EhrlicherC Christian Ehrlicher

              @Alvein said in Detecting QStack overflow:

              It's that I want to detect when there's no more available memory for my QStack, to stop the recursive calls at that point.

              I would guess it's too late then but: https://en.cppreference.com/w/cpp/memory/new/bad_alloc

              AlveinA Offline
              AlveinA Offline
              Alvein
              wrote on last edited by
              #9

              @Christian-Ehrlicher Hello. Tried std::bad_alloc but no exception is being thrown.

              Can't find a clear way of enabling those (if something needs to be enabled, that is - because it shouldn't, by default, IMHO).

              1 Reply Last reply
              0
              • mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by mrjj
                #10

                Hi
                It does throw something
                as

                class BIG
                {
                    std::array<int, 100000> test;
                
                };
                

                alt text
                does go to the catch part.

                Hmm
                Win 10, visual studio 32bit does throw bad_alloc

                alt text

                but app terminates right after it leaves the scope so i think its too late at this point to continue.

                1 Reply Last reply
                2
                • AlveinA Offline
                  AlveinA Offline
                  Alvein
                  wrote on last edited by
                  #11

                  Thanks.

                  Finally, I implemented something like this...

                  template <class itemType>
                  bool safePush(QStack<itemType> &stkStack,itemType stkItem) {
                      bool bSP=true;
                      try {
                          stkStack.push(stkItem);
                      }
                      catch (std::bad_alloc) {
                          bSP=false;
                      }
                      return bSP;
                  }
                  

                  ...and it works now.

                  Not sure what was causing the exception ignoring, tho.

                  JonBJ 1 Reply Last reply
                  1
                  • AlveinA Alvein

                    Thanks.

                    Finally, I implemented something like this...

                    template <class itemType>
                    bool safePush(QStack<itemType> &stkStack,itemType stkItem) {
                        bool bSP=true;
                        try {
                            stkStack.push(stkItem);
                        }
                        catch (std::bad_alloc) {
                            bSP=false;
                        }
                        return bSP;
                    }
                    

                    ...and it works now.

                    Not sure what was causing the exception ignoring, tho.

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

                    @Alvein
                    Just to say: if you are so close to running out of memory that pushing one item to a stack causes std::bad_alloc, you may succeed in trapping the error and continuing as you have, but is it not likely that as you continue something else fails disastrously as there is no memory left to allocate?

                    AlveinA 1 Reply Last reply
                    0
                    • JonBJ JonB

                      @Alvein
                      Just to say: if you are so close to running out of memory that pushing one item to a stack causes std::bad_alloc, you may succeed in trapping the error and continuing as you have, but is it not likely that as you continue something else fails disastrously as there is no memory left to allocate?

                      AlveinA Offline
                      AlveinA Offline
                      Alvein
                      wrote on last edited by
                      #13

                      @JonB Not exactly sure if the "no memory" exception means no memory for a single continuous block. But I reckon I'm just being paranoid. I'm handling the stack myself since originally the call stack was not enough, so I just added the check because it looks good (and for learning something in the middle).

                      In the end, even if I catch the exception, that's not a dead end, since as it's coded, the process can be restarted from where it was, with an empty stack again.

                      TBH, by design, not even a restart is required because the process backtracks when the memory is full, freeing private stack space. I'll have to see it running live. But the probability of seeing it reaching the exception is terribly low.

                      JonBJ 1 Reply Last reply
                      0
                      • AlveinA Alvein

                        @JonB Not exactly sure if the "no memory" exception means no memory for a single continuous block. But I reckon I'm just being paranoid. I'm handling the stack myself since originally the call stack was not enough, so I just added the check because it looks good (and for learning something in the middle).

                        In the end, even if I catch the exception, that's not a dead end, since as it's coded, the process can be restarted from where it was, with an empty stack again.

                        TBH, by design, not even a restart is required because the process backtracks when the memory is full, freeing private stack space. I'll have to see it running live. But the probability of seeing it reaching the exception is terribly low.

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

                        @Alvein

                        because the process backtracks when the memory is full, freeing private stack space

                        Again, just an observation :) I think you might be being too optimistic! You seem to assume two things:

                        • Freed blocks will get re-used, at this point, for another allocation. May depend on whether it's an odd or even day of the week ;)

                        • The code executed for backtracking does not itself do any allocation, which might fail where you are. There are a lot of things you might call without realizing they do some allocation!

                        Best of luck.

                        AlveinA 1 Reply Last reply
                        1
                        • JonBJ JonB

                          @Alvein

                          because the process backtracks when the memory is full, freeing private stack space

                          Again, just an observation :) I think you might be being too optimistic! You seem to assume two things:

                          • Freed blocks will get re-used, at this point, for another allocation. May depend on whether it's an odd or even day of the week ;)

                          • The code executed for backtracking does not itself do any allocation, which might fail where you are. There are a lot of things you might call without realizing they do some allocation!

                          Best of luck.

                          AlveinA Offline
                          AlveinA Offline
                          Alvein
                          wrote on last edited by
                          #15

                          Thank you very much for your advice.

                          • Freed blocks will get re-used, at this point, for another allocation. May depend on whether it's an odd or even day of the week ;)

                          Yes, I'm aware of that. My intention was simply avoiding my program to crash and tell the users what's happening, letting them to stop the process if they wish.

                          • The code executed for backtracking does not itself do any allocation, which might fail where you are. There are a lot of things you might call without realizing they do some allocation!

                          Backtracking, for me, means just popping out items from the private stack. Remember this is about simulating recursion, so such backtracking is also simulated. In the end, there's two options for the program, either using the recently freed memory, or behaving like if there was no more, in case that memory was immediately seized by other things. Each option has been considered and handled by code.

                          1 Reply Last reply
                          0

                          • Login

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