An example (maybe)

By: Simon Farnsworth (simon.delete@this.farnz.org.uk), April 2, 2020 2:21 am
Room: Moderated Discussions
Doug S (foo.delete@this.bar.bar) on April 1, 2020 11:01 am wrote:
> Mark Roulo (nothanks.delete@this.xxx.com) on March 31, 2020 4:07 pm wrote:
> > Heikki Kultala (heikki.kultala.delete@this.tuni.fi) on March 31, 2020 1:01 pm wrote:
> > > ⚛ (0xe2.0x9a.0x9b.delete@this.gmail.com) on March 31, 2020 8:02 am wrote:
> > > > Linus Torvalds (torvalds.delete@this.linux-foundation.org) on March 28, 2020 9:40 am wrote:
> > > > > Again, the argument that zeroing variables hides bugs is complete garbage.
> > > >
> > > > A crucial point is that C, like most current programming
> > > > languages, does not have a variable discard statement.
> > >
> > > C has a "free" operator for dynamically allocated variakles.
> > >
> > > > The runtime-time cost of this feature would be zero, unless using a compiler
> > > > flag to randomize the contents of discarded memory locations.
> > >
> > > If the run-time cost would be zero, then would also be the benefit.
> > >
> > > If would just be a source for now kind of bugs.
> > >
> > > > C compiler is unable to accurately track the lifetime of variables
> > > > because it does not know when the life of the variable is ending.
> > >
> > > Life of a local variable ends when the variable goes out of the
> > > scope. And the compiler knows exactly when the scope ends.
> > >
> > > Life of a global variable never ends.
> > >
> > > Life of an heap-allocated variable ends when free is called.
> > >
> > >
> > > When you are proposing makes absolutely no sense.
> > > It's much better than the compiler performs static analysis on when a local variable
> > > is alive than the programmer would have to care about this and could call this "discard"
> > > method. Because the programmer WOULD sometimes call this too early.
> >
> > I don't know if this is what the poster with a non-ASCII name has in mind, but
> > consider the following code:
> >
> >

> > func MakeNoun(myData string[])
> > {
> > sortedData = malloc( ... )
> > copy (sortedData, myData, ...)
> >
> > // Using myData after here is a mistake. I want to be able to
> > // tag myData as unusable ...
> >
> > sort (sortedData)
> >
> > :
> >
>
> > I can imagine other uses. You can often hack around this with nested scopes, but
> > sometimes it would be nicer to just be able to express "You should only be accessing
> > the processed data after this point"
>
> It would be pretty simple to do that with pragmas though you are limited to compilers
> that have added the functionality. In cases like this where the variable is a pointer
> simply set it to NULL after you're done with it so a later dereference will fault.

FWIW, in Rust where the compiler warns of accesses to a possibly uninitialized variable, permits shadowing, and has move semantics for anything non-trivial, there are at least two ways to make this work.

Via shadowing: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=8faf5452303fa45631a8a4416a313c6e - this hides the old name, and thus works to hide the old initialization even for Copy types like u32.

Via move semantics: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=5c365aafbbc813832a20db3e3de5cf01 - this moves the data out, and thus deinitializes the old variable. You would have to move new data into foo to make it initialized again.

The second of those would be hard to translate to a language without move semantics (like C), but you could allow shadowing of a variable with a `void` type to mean "no further access allowed":


func MakeNoun(myData string[])
{
sortedData = malloc( ... )
copy (sortedData, myData, ...)

// Using myData after here is a mistake. I want to be able to
// tag myData as unusable ...

void myData // The use of void here indicates that myData is no longer usable.

sort (sortedData)

:
< Previous Post in ThreadNext Post in Thread >
TopicPosted ByDate
Why not initialize all variables to zero?Doug S2020/03/26 12:13 PM
  Most (all?) modern programming languages do this, right?Mark Roulo2020/03/26 04:44 PM
    Most (all?) modern programming languages do this, right?Konrad Scharz2020/03/27 01:09 AM
      Most (all?) modern programming languages do this, right?Gionatan Danti2020/03/27 03:13 AM
        Most (all?) modern programming languages do this, right?Foo_2020/03/27 03:45 AM
          Most (all?) modern programming languages do this, right?Gionatan Danti2020/03/27 09:19 AM
            Most (all?) modern programming languages do this, right?Foo_2020/03/28 02:32 AM
              Most (all?) modern programming languages do this, right?Montaray Jack2020/03/28 10:26 AM
      Most (all?) modern programming languages do this, right?Jeff S.2020/03/27 07:22 AM
        Most (all?) modern programming languages do this, right?anonymou52020/03/27 01:08 PM
  Why not initialize all variables to zero?Etienne2020/03/27 01:56 AM
    Why not initialize all variables to zero?NoSpammer2020/03/27 02:31 AM
  Why not initialize all variables to zero?Carlie Coats2020/03/27 06:17 AM
    Why not initialize all variables to zero?Jukka Larja2020/03/27 10:14 PM
      Why not initialize all variables to zero?Anon2020/03/28 12:01 AM
        Why not initialize all variables to zero?Jukka Larja2020/03/28 08:25 AM
          Why not initialize all variables to zero?Anon2020/03/28 11:20 AM
            Why not initialize all variables to zero?Jukka Larja2020/03/28 11:45 AM
              Why not initialize all variables to zero?Anon2020/03/28 01:21 PM
                Why not initialize all variables to zero?Jukka Larja2020/03/28 09:49 PM
    Why not initialize all variables to zero?Doug S2020/03/28 11:27 AM
      Why not initialize all variables to zero?Anon2020/03/28 01:24 PM
        Why not initialize all variables to zero?Carlie Coats2020/03/29 06:56 AM
  Why not initialize all variables to zero?Gabriele Svelto2020/03/27 06:52 AM
    Why not initialize all variables to zero?Foo_2020/03/28 02:34 AM
      Why not initialize all variables to zero?Linus Torvalds2020/03/28 09:40 AM
        Why not initialize all variables to zero?Doug S2020/03/28 11:21 AM
          Why not initialize all variables to zero?Linus Torvalds2020/03/28 01:01 PM
            Why not initialize all variables to zero?Etienne2020/04/02 01:14 AM
              Why not initialize all variables to zero?gallier22020/04/02 05:41 AM
              Why not initialize all variables to zero?Doug S2020/04/02 09:51 AM
        Why not initialize all variables to zero?Gabriele Svelto2020/03/28 01:46 PM
          Why not initialize all variables to zero?Linus Torvalds2020/03/28 04:28 PM
            Why not initialize all variables to zero?Anon32020/03/29 04:23 AM
            Why not initialize all variables to zero?Gabriele Svelto2020/03/29 12:28 PM
              Why not initialize all variables to zero?Anon32020/03/29 01:05 PM
                Why not initialize all variables to zero?Gabriele Svelto2020/03/30 12:52 AM
        Why not initialize all variables to zero?Carlie Coats2020/03/29 07:03 AM
        Why not initialize all variables to zero?gallier22020/03/29 11:48 PM
          Why not initialize all variables to zero?Michael S2020/03/30 02:24 AM
            Why not initialize all variables to zero?gallier22020/03/30 03:11 AM
        Why not discard variables after last use?2020/03/31 08:02 AM
          Makes no sense at allHeikki Kultala2020/03/31 01:01 PM
            An example (maybe)Mark Roulo2020/03/31 04:07 PM
              An example (maybe)Doug S2020/04/01 11:01 AM
                An example (maybe)Simon Farnsworth2020/04/02 02:21 AM
            Why not discard variables after last use?2020/04/02 12:41 PM
    Why not initialize all variables to zero?j2020/03/28 09:16 AM
    Why not initialize all variables to zero?Montaray Jack2020/03/28 11:42 AM
  Why not initialize all variables to zero?blaine2020/03/27 01:23 PM
    Why not initialize all variables to zero?James2020/03/28 03:18 AM
      Why not initialize all variables to zero?Anon32020/03/28 05:14 AM
      Why not initialize all variables to zero?Doug S2020/03/28 11:32 AM
        Why not initialize all variables to zero?Anon32020/03/28 11:45 AM
    Why not initialize all variables to zero?gallier22020/03/30 12:03 AM
  Why not initialize all variables to zero?gallier22020/03/29 11:32 PM
    Why not initialize all variables to zero?Michael S2020/03/30 02:30 AM
      Why not initialize all variables to zero?gallier22020/03/30 03:45 AM
Reply to this Topic
Name:
Email:
Topic:
Body: No Text
How do you spell purple?