Date: Mon, 11 Oct 2021 12:35:16 +0200
pon., 11 paź 2021 o 11:52 Anubhav Guleria via Std-Discussion
<std-discussion_at_[hidden]> napisał(a):
>
> Is it not true that in either way the thread will get penalize for that check? In case if the check is done by container an undefined behavior could be avoided.
>
Make a container wrapper that does that, this is why C++ way is
correct as you can always add more checks,
but you can't remove checks without rewriting the class from scratch.
And remember most UB is required to have 100% possible performance,
why take a 10% performance drop when you can prove you can safy use
some operation?
This is why vector have `at` and `[]` to do the same operation.
> On Mon, Oct 11, 2021, 1:18 PM Bo Persson via Std-Discussion <std-discussion_at_[hidden]> wrote:
>>
>> On 2021-10-11 at 08:53, Anubhav Guleria via Std-Discussion wrote:
>> > Thanks for clarifying.
>> >
>> > Any specific reason why container's pop_back method can't check for
>> > current size and if it is 0 then make the pop operation a no-op?
>>
>> That would be a cost that everyone would have to pay, even when you are
>> certain that the stack is never empty.
>>
>> It is also extremely easy for user code to add the check, if and when
>> necessary:
>>
>> if (!s.empty())
>> s.pop();
>>
>>
>>
>>
>>
>> >
>> > Thanks,
>> > Anubhav
>> >
>> > On Mon, Oct 11, 2021 at 6:17 AM Keenan Horrigan via Std-Discussion
>> > <std-discussion_at_[hidden]> wrote:
>> >>
>> >> This is expected as std::stack::pop calls the underlying container's pop_back method, and performing pop_back on an empty vector is undefined behavior.
>> >>
>> >> ‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐
>> >>
>> >> On Sunday, October 10th, 2021 at 7:37 PM, Anubhav Guleria via Std-Discussion <std-discussion_at_[hidden]> wrote:
>> >>
>> >>> Hi,
>> >>>
>> >>> The following piece of code is giving Segmentation Fault.
>> >>>
>> >>> Probably the size is getting overflowed and the condition is not
>> >>>
>> >>> handled in implementation.
>> >>>
>> >>> Is this a known issue?
>> >>>
>> >>> #include<iostream>
>> >>>
>> >>> #include<stack>
>> >>>
>> >>> #include<vector>
>> >>>
>> >>> using namespace std;
>> >>>
>> >>> int main(){
>> >>>
>> >>> stack<int, vector<int>> s;
>> >>>
>> >>> s.pop();
>> >>>
>> >>> cout<<s.size()<<endl;
>> >>>
>> >>> if(!s.empty()){
>> >>>
>> >>> cout<<s.top()<<endl;
>> >>>
>> >>> }
>> >>>
>> >>> return 0;
>> >>>
>> >>> }
>> >>>
>> >>> I compiled with: g++ -std=c++17 test.cpp
>> >>>
>> >>> Output I got:
>> >>>
>> >>> 18446744073709551615
>> >>>
>> >>> Segmentation fault (core dumped)
>> >>>
>> >>> Output with -fsanitize=address flag
>> >>>
>> >>> g++ -std=c++17 -fsanitize=address test.cpp
>> >>>
>> >>> anubhav_at_turing:~/Documents/Programming$ ./a.out
>> >>>
>> >>> 18446744073709551615
>> >>>
>> >>> AddressSanitizer:DEADLYSIGNAL
>> >>> ===============================================================================================================================================================================================================================================================================================================================================================================================
>> >>>
>> >>> ==6143==ERROR: AddressSanitizer: SEGV on unknown address
>> >>>
>> >>> 0x000000000000 (pc 0x559da7fa34e2 bp 0x7ffdefc01bf0 sp 0x7ffdefc01b40
>> >>>
>> >>> T0)
>> >>>
>> >>> ==6143==The signal is caused by a READ memory access.
>> >>>
>> >>> ==6143==Hint: address points to the zero page.
>> >>>
>> >>> #0 0x559da7fa34e1 in main (/home/anubhav/Documents/Programming/a.out+0x14e1)
>> >>>
>> >>> #1 0x7ff37989b0b2 in __libc_start_main
>> >>>
>> >>> (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)
>> >>>
>> >>> #2 0x559da7fa330d in _start
>> >>>
>> >>> (/home/anubhav/Documents/Programming/a.out+0x130d)
>> >>>
>> >>> AddressSanitizer can not provide additional info.
>> >>>
>> >>> SUMMARY: AddressSanitizer: SEGV
>> >>>
>> >>> (/home/anubhav/Documents/Programming/a.out+0x14e1) in main
>> >>>
>> >>> ==6143==ABORTING
>> >>>
>> >>> g++ Version:
>> >>>
>> >>> g++ (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
>> >>>
>> >>> Copyright (C) 2019 Free Software Foundation, Inc.
>> >>>
>> >>> This is free software; see the source for copying conditions. There is NO
>> >>>
>> >>> warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
>> >>>
>> >>> Operating System:
>> >>>
>> >>> lsb_release -a
>> >>>
>> >>> No LSB modules are available.
>> >>>
>> >>> Distributor ID: Ubuntu
>> >>>
>> >>> Description: Ubuntu 20.04.3 LTS
>> >>>
>> >>> Release: 20.04
>> >>>
>> >>> Codename: focal
>> >>>
>> >>> Kernel: Linux 5.11.0-37-generic
>> >>>
>> >>> Thanks,
>> >>>
>> >>> Anubhav
>> >>> -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
>>
>>
>> --
>> Std-Discussion mailing list
>> Std-Discussion_at_[hidden]
>> https://lists.isocpp.org/mailman/listinfo.cgi/std-discussion
>
> --
> Std-Discussion mailing list
> Std-Discussion_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-discussion
<std-discussion_at_[hidden]> napisał(a):
>
> Is it not true that in either way the thread will get penalize for that check? In case if the check is done by container an undefined behavior could be avoided.
>
Make a container wrapper that does that, this is why C++ way is
correct as you can always add more checks,
but you can't remove checks without rewriting the class from scratch.
And remember most UB is required to have 100% possible performance,
why take a 10% performance drop when you can prove you can safy use
some operation?
This is why vector have `at` and `[]` to do the same operation.
> On Mon, Oct 11, 2021, 1:18 PM Bo Persson via Std-Discussion <std-discussion_at_[hidden]> wrote:
>>
>> On 2021-10-11 at 08:53, Anubhav Guleria via Std-Discussion wrote:
>> > Thanks for clarifying.
>> >
>> > Any specific reason why container's pop_back method can't check for
>> > current size and if it is 0 then make the pop operation a no-op?
>>
>> That would be a cost that everyone would have to pay, even when you are
>> certain that the stack is never empty.
>>
>> It is also extremely easy for user code to add the check, if and when
>> necessary:
>>
>> if (!s.empty())
>> s.pop();
>>
>>
>>
>>
>>
>> >
>> > Thanks,
>> > Anubhav
>> >
>> > On Mon, Oct 11, 2021 at 6:17 AM Keenan Horrigan via Std-Discussion
>> > <std-discussion_at_[hidden]> wrote:
>> >>
>> >> This is expected as std::stack::pop calls the underlying container's pop_back method, and performing pop_back on an empty vector is undefined behavior.
>> >>
>> >> ‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐
>> >>
>> >> On Sunday, October 10th, 2021 at 7:37 PM, Anubhav Guleria via Std-Discussion <std-discussion_at_[hidden]> wrote:
>> >>
>> >>> Hi,
>> >>>
>> >>> The following piece of code is giving Segmentation Fault.
>> >>>
>> >>> Probably the size is getting overflowed and the condition is not
>> >>>
>> >>> handled in implementation.
>> >>>
>> >>> Is this a known issue?
>> >>>
>> >>> #include<iostream>
>> >>>
>> >>> #include<stack>
>> >>>
>> >>> #include<vector>
>> >>>
>> >>> using namespace std;
>> >>>
>> >>> int main(){
>> >>>
>> >>> stack<int, vector<int>> s;
>> >>>
>> >>> s.pop();
>> >>>
>> >>> cout<<s.size()<<endl;
>> >>>
>> >>> if(!s.empty()){
>> >>>
>> >>> cout<<s.top()<<endl;
>> >>>
>> >>> }
>> >>>
>> >>> return 0;
>> >>>
>> >>> }
>> >>>
>> >>> I compiled with: g++ -std=c++17 test.cpp
>> >>>
>> >>> Output I got:
>> >>>
>> >>> 18446744073709551615
>> >>>
>> >>> Segmentation fault (core dumped)
>> >>>
>> >>> Output with -fsanitize=address flag
>> >>>
>> >>> g++ -std=c++17 -fsanitize=address test.cpp
>> >>>
>> >>> anubhav_at_turing:~/Documents/Programming$ ./a.out
>> >>>
>> >>> 18446744073709551615
>> >>>
>> >>> AddressSanitizer:DEADLYSIGNAL
>> >>> ===============================================================================================================================================================================================================================================================================================================================================================================================
>> >>>
>> >>> ==6143==ERROR: AddressSanitizer: SEGV on unknown address
>> >>>
>> >>> 0x000000000000 (pc 0x559da7fa34e2 bp 0x7ffdefc01bf0 sp 0x7ffdefc01b40
>> >>>
>> >>> T0)
>> >>>
>> >>> ==6143==The signal is caused by a READ memory access.
>> >>>
>> >>> ==6143==Hint: address points to the zero page.
>> >>>
>> >>> #0 0x559da7fa34e1 in main (/home/anubhav/Documents/Programming/a.out+0x14e1)
>> >>>
>> >>> #1 0x7ff37989b0b2 in __libc_start_main
>> >>>
>> >>> (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)
>> >>>
>> >>> #2 0x559da7fa330d in _start
>> >>>
>> >>> (/home/anubhav/Documents/Programming/a.out+0x130d)
>> >>>
>> >>> AddressSanitizer can not provide additional info.
>> >>>
>> >>> SUMMARY: AddressSanitizer: SEGV
>> >>>
>> >>> (/home/anubhav/Documents/Programming/a.out+0x14e1) in main
>> >>>
>> >>> ==6143==ABORTING
>> >>>
>> >>> g++ Version:
>> >>>
>> >>> g++ (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
>> >>>
>> >>> Copyright (C) 2019 Free Software Foundation, Inc.
>> >>>
>> >>> This is free software; see the source for copying conditions. There is NO
>> >>>
>> >>> warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
>> >>>
>> >>> Operating System:
>> >>>
>> >>> lsb_release -a
>> >>>
>> >>> No LSB modules are available.
>> >>>
>> >>> Distributor ID: Ubuntu
>> >>>
>> >>> Description: Ubuntu 20.04.3 LTS
>> >>>
>> >>> Release: 20.04
>> >>>
>> >>> Codename: focal
>> >>>
>> >>> Kernel: Linux 5.11.0-37-generic
>> >>>
>> >>> Thanks,
>> >>>
>> >>> Anubhav
>> >>> -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
>>
>>
>> --
>> Std-Discussion mailing list
>> Std-Discussion_at_[hidden]
>> https://lists.isocpp.org/mailman/listinfo.cgi/std-discussion
>
> --
> Std-Discussion mailing list
> Std-Discussion_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-discussion
Received on 2021-10-11 05:35:30