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: A glitch regarding the usual malloc() idiom Date: Wed, 20 Jul 2016 16:55:24 -0700 Organization: None to speak of Lines: 28 Message-ID: References: <4cd22745-22ad-4ac7-bdba-39ffa1c75e13@googlegroups.com> <7d995799-5f23-4784-a62e-ea423de00650@googlegroups.com> <36f1e05e-609e-4e0e-8819-86feee1f4d8c@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="945944de09706c9b4e29b53c9d2efdc2"; logging-data="8666"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX183jm4+6nInw67lThn9qIYF" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:QlwfBOUnhtHBfjr7r4IhnbQS73c= sha1:fw9g47DRZU9iO934duRi2wjIS4E= Xref: csiph.com comp.lang.c:90703 Malcolm McLean writes: > On Wednesday, July 20, 2016 at 8:30:01 PM UTC+1, Keith Thompson wrote: >> >> I don't *care* that I'm asking for 160 bytes. Or if I do, I can >> compute it easily enough. I care, in this case, that I'm allocating >> space for 40 objects of the type magptr points to. (I'd probably use >> some symbolic name rather than the constant 40, but that's not a big >> deal for this example.) >> > Sometimes it matters, most often it doesn't. > But even if there is no serious constraint on memory usage, it > helps to be able to see something concrete. It's likely we know > that a float will by 4 bytes, so sizeof(float) is meaningful, it > can mentally be replaced by "four". If you need to see something concrete, you can add a comment: magptr = malloc(40 * sizeof *magptr); /* 160 bytes */ Keeping the comment in sync with the code is left as an exercise. [...] -- 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"