Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: VC++ 2013, malloc() and free() Date: Tue, 25 Oct 2016 09:29:21 -0700 Organization: None to speak of Lines: 29 Message-ID: References: <50ddf174-e477-4b57-b576-a17623b2ce68@googlegroups.com> <20161025172315.44bb53a4@maxa-pc.cpe.bvcom.net> <52ae27e2-0157-4f29-8ca8-cbd656f32247@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="0a5b782d68e23f17f10cdd6bfe85a146"; logging-data="26230"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19rP8UApwx2hXeJQb9s/b21" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:NCRwEb8m8PKk3LHQwwx+jaEMibg= sha1:bt5bEwIrahHhq96M3RNGpX4lzgU= Xref: csiph.com comp.lang.c:95296 supercat@casperkitty.com writes: [...] > Trying to handle allocation failures when they occur is ugly, but it's at > least possible to write code to recover from allocation failures somewhat > gracefully. If a call to malloc() may yield a pointer to an address that > could cause a critical failure when code tries to use it, and if there's > no requirement that applications be able to tell when malloc() might have > done so, is there any way to write code that uses malloc() storage and is > guaranteed not to invoke UB? Undefined behavior is behavior that's not defined by the C standard. The C standard defines the behavior of malloc(). It either successfully allocates memory which the program can then use, or it fails and returns a null pointer. If a program calls malloc() and gets a non-null pointer, but the program crashes when it tries to use the allocated memory, that's not UB on the program's part, it's a failure in the environment. On the other hand, a program can crash at any time for any reason, such as a sledgehammer to the CPU, and nothing the standard says can prevent that. -- Keith Thompson (The_Other_Keith) kst-u@mib.org Working, but not speaking, for JetHead Development, Inc. "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"