CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 25
  1. #1
    Join Date
    Jun 2019
    Posts
    30

    Circular Buffer POP()

    Hi,

    I have Circular buffer with POP() method,

    If the buffer is empty then i will return 0,but when i try to use buffer with other object which is not convertible to zero then it will fail to compile

    thanks in advance

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Circular Buffer POP()

    If empty, you could raise an exception rather than try to return something.

    Another way would be to return a type optional See https://en.cppreference.com/w/cpp/utility/optional (only c++17)
    and if the buffer is empty return optional without a value.

    You could also just ignore trying to pop on an empty buffer and let an error occur - make it the users responsibility to test that the buffer is not empty before trying to pop.

    If this is a templated class/function with the underlying data of type T, then if the buffer is empty you could just return T {} - which is a default object for type T (0 for int etc) (assuming T allows a default).
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Feb 2017
    Posts
    677

    Re: Circular Buffer POP()

    Yet another option you sometimes encounter is this call signature,

    Code:
    bool try_pop(T& value) { // non-blocking pop
    A variable of type T is passed to try_pop by reference. If try_pop returns true the variable has been assigned an object from the buffer, otherwise the buffer was empty (leaving the content of the variable undefined).
    Last edited by wolle; June 16th, 2019 at 03:53 AM.

  4. #4
    Join Date
    Jun 2019
    Posts
    30

    Re: Circular Buffer POP()

    Quote Originally Posted by 2kaud View Post
    If empty, you could raise an exception rather than try to return something.

    Another way would be to return a type optional See https://en.cppreference.com/w/cpp/utility/optional (only c++17)
    and if the buffer is empty return optional without a value.

    You could also just ignore trying to pop on an empty buffer and let an error occur - make it the users responsibility to test that the buffer is not empty before trying to pop.

    If this is a templated class/function with the underlying data of type T, then if the buffer is empty you could just return T {} - which is a default object for type T (0 for int etc) (assuming T allows a default).

    Is there any other method other that returning T{}?

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Circular Buffer POP()

    Raise an exception.
    Last edited by 2kaud; June 17th, 2019 at 10:18 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  6. #6
    Join Date
    Feb 2017
    Posts
    677

    Re: Circular Buffer POP()

    Quote Originally Posted by Raj90 View Post
    Is there any other method other that returning T{}?
    You can use the call signature I suggested in #3.

  7. #7
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Circular Buffer POP()

    Basically for dealing with this type of situation you have 3 options:

    1) Remove the test code and do nothing. The calling code will probably cause a run-time memory error. This is common in c/c++ code where bounds are not usually checked for speed performance reasons. That's why with say vector[] and vector at, [] doesn't do any checks whereas at does - but is slower. You could have another function, say empty(), which returns true if the buffer is empty and false if not. This again is a common practice.

    2) Raise an exception. Again this is common in c++. The exception would be out_of_range.

    3) Indicate an error via a return value. This is not considered particularly good c++ practice. However, if you really want to do this you have a few options:

    a) return T{} - default value for type T

    b) have a return type of optional<T> and for error return nullopt otherwise return the value.

    c) return a type pair<bool, T> where bool indicates whether the value of T is valid or not.

    d) As per Wolle's post #3 - return a bool and have the argument to pop() a non-const reference for the value if no issue arises.

    How you want to do this is really down to you and what interfaces you are providing - or is the interface given? is this a homework exercise?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  8. #8
    Join Date
    Jun 2019
    Posts
    30

    Re: Circular Buffer POP()

    Quote Originally Posted by 2kaud View Post
    Basically for dealing with this type of situation you have 3 options:

    1) Remove the test code and do nothing. The calling code will probably cause a run-time memory error. This is common in c/c++ code where bounds are not usually checked for speed performance reasons. That's why with say vector[] and vector at, [] doesn't do any checks whereas at does - but is slower. You could have another function, say empty(), which returns true if the buffer is empty and false if not. This again is a common practice.

    2) Raise an exception. Again this is common in c++. The exception would be out_of_range.

    3) Indicate an error via a return value. This is not considered particularly good c++ practice. However, if you really want to do this you have a few options:

    a) return T{} - default value for type T

    b) have a return type of optional<T> and for error return nullopt otherwise return the value.

    c) return a type pair<bool, T> where bool indicates whether the value of T is valid or not.

    d) As per Wolle's post #3 - return a bool and have the argument to pop() a non-const reference for the value if no issue arises.

    How you want to do this is really down to you and what interfaces you are providing - or is the interface given? is this a homework exercise?

    I can't raise an exception, because we don't use them

    return nullopt--> this is only used in c++17,as we are using c++11?

    Is there any other way to solve?

  9. #9
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Circular Buffer POP()

    See d) above and Wolle's post #3
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  10. #10
    Join Date
    Jun 2019
    Posts
    30

    Re: Circular Buffer POP()

    Quote Originally Posted by wolle View Post
    You can use the call signature I suggested in #3.

    Can you please provide me the code snippet for this?

  11. #11
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Circular Buffer POP()

    What's your current pop() code?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  12. #12
    Join Date
    Jun 2019
    Posts
    30

    Re: Circular Buffer POP()

    Quote Originally Posted by 2kaud View Post
    What's your current pop() code?
    Code:
    T pop()
    {
        if (isEmpty())
        {
            return T{};
        } 
    
        size -= 1;
    
        int index = tail;
        updateIndex(tail);
    
        return buffer[index];
    }
    Last edited by 2kaud; June 18th, 2019 at 09:34 AM. Reason: Added code tags

  13. #13
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Circular Buffer POP()

    [When posting code, please use code tags. Go Advanced, select the formatted code and click '#'.]

    For Wolle's suggestion consider:

    Code:
    bool try_pop(T& value)
    {
        if (isEmpty())
            return false;
     
        --size;
    
        int index = tail;
        updateIndex(tail);
    
        value = buffer[index];
        return true;
    }
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  14. #14
    Join Date
    Jun 2019
    Posts
    30

    Re: Circular Buffer POP()

    Quote Originally Posted by 2kaud View Post
    [When posting code, please use code tags. Go Advanced, select the formatted code and click '#'.]

    For Wolle's suggestion consider:

    Code:
    bool try_pop(T& value)
    {
        if (isEmpty())
            return false;
     
        --size;
    
        int index = tail;
        updateIndex(tail);
    
        value = buffer[index];
        return true;
    }

    Return type of method should not change it should return T type and it should not pass any arguments?

    With this requirement is there any idea for the above issue?

  15. #15
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Circular Buffer POP()

    You have already been given the possibilities.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured