Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #79589

Re: Why does this work in MSVC 2019...

From Christian Gollwitzer <auriocus@gmx.de>
Newsgroups comp.lang.c++
Subject Re: Why does this work in MSVC 2019...
Date 2021-05-19 07:57 +0200
Organization A noiseless patient Spider
Message-ID <s829br$dld$1@dont-email.me> (permalink)
References <s7uss6$12f5$1@gioia.aioe.org> <s8182f$r9c$1@dont-email.me> <s81dul$1j9v$2@gioia.aioe.org>

Show all headers | View raw


Am 19.05.21 um 00:09 schrieb Chris M. Thomasson:
> On 5/18/2021 1:29 PM, Alf P. Steinbach wrote:
>> On 18.05.2021 01:05, Chris M. Thomasson wrote:
>>> I don't think this should work at all! It should segfault.
>>>
>>>      set_t::iterator const& search_id(unsigned long id) const
>>
>> Well UB is UB. That includes that things may appear to work.
> 
> Indeed. Mike Terry mentioned something about a stack issue.
> 

You are basically accessing memory that was "freed"[*] before. That is 
totally undefined, of course, but there is no guarantee that it 
segfaults. The CPU can not check every small allocation on the stack or 
heap for correctness. Instead, the memory is structured into pages of 
typically 4kB in size. The CPU only checks that the page was allocated 
for the current process, so as long as there is any other valid object 
in this page, there is no segfault. This also explains why programs with 
memory errors often crash in a totally different location. If a pointer 
gets mangled, then it segfaults at the point where the pointer is used 
and suddenly points into the woods.

You can use a memory debugger to detect these problems, like valgrind on 
Linux (one of the best) or the flag -fsanitize=address with clang. Then, 
every object is allocated on its own page, making the program very 
bloated and slow, but the program will then segfault immediately at the 
point where the deleted object is accessed.


[*] not on the heap, so not "malloc/free-type freed", but "stack-freed"

Best regards,

	Christian

Back to comp.lang.c++ | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Why does this work in MSVC 2019... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-05-17 16:05 -0700
  Re: Why does this work in MSVC 2019... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-05-17 16:11 -0700
    Re: Why does this work in MSVC 2019... Branimir Maksimovic <branimir.maksimovic@gmail.com> - 2021-05-17 23:24 +0000
  Re: Why does this work in MSVC 2019... "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2021-05-18 22:29 +0200
    Re: Why does this work in MSVC 2019... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-05-18 15:09 -0700
      Re: Why does this work in MSVC 2019... Christian Gollwitzer <auriocus@gmx.de> - 2021-05-19 07:57 +0200
        Re: Why does this work in MSVC 2019... James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-05-19 09:58 -0400
  Re: Why does this work in MSVC 2019... Mike Terry <news.dead.person.stones@darjeeling.plus.com> - 2021-05-18 17:01 +0100
    Re: Why does this work in MSVC 2019... Bonita Montero <Bonita.Montero@gmail.com> - 2021-05-18 18:12 +0200
      Re: Why does this work in MSVC 2019... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-05-18 14:50 -0700
        Re: Why does this work in MSVC 2019... Bonita Montero <Bonita.Montero@gmail.com> - 2021-05-19 01:42 +0200
          Re: Why does this work in MSVC 2019... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-05-18 16:48 -0700
            Re: Why does this work in MSVC 2019... Öö Tiib <ootiib@hot.ee> - 2021-05-18 17:55 -0700
              Re: Why does this work in MSVC 2019... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-05-18 20:06 -0700
                Re: Why does this work in MSVC 2019... Paavo Helde <myfirstname@osa.pri.ee> - 2021-05-19 09:50 +0300
                Re: Why does this work in MSVC 2019... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-05-19 00:45 -0700
            Re: Why does this work in MSVC 2019... Bonita Montero <Bonita.Montero@gmail.com> - 2021-05-19 07:02 +0200
              Re: Why does this work in MSVC 2019... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-05-19 00:46 -0700
      Re: Why does this work in MSVC 2019... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-05-18 15:10 -0700
    Re: Why does this work in MSVC 2019... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-05-18 14:59 -0700
  Re: Why does this work in MSVC 2019... Andrey Tarasevich <andreytarasevich@hotmail.com> - 2021-05-20 03:44 +0300
    Re: Why does this work in MSVC 2019... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-05-19 20:38 -0700

csiph-web