Struct should be invalid after move

H. S. Teoh hsteoh at quickfur.ath.cx
Wed Nov 28 16:58:32 UTC 2018


On Wed, Nov 28, 2018 at 04:49:02PM +0000, Atila Neves via Digitalmars-d wrote:
> On Wednesday, 28 November 2018 at 09:17:39 UTC, sanjayss wrote:
> > On Tuesday, 27 November 2018 at 08:00:22 UTC, Sebastiaan Koppe wrote:
> > > [...]
> > 
> > I have always wanted a feature in C that would let me explicitly
> > tell the compiler that a variable is no longer in scope (some sort
> > of unset of a variable). This would be useful to do defensive
> > programming against use-after-free of pointers to allocated memory
> > and such.
> 
> 
> {
>     void* ptr = malloc(5);
> }
> 
> // ptr no longer in scope

Yeah, this seems to be a not-very-well-known aspect of syntax common
across C, C++, Java, and D:

	ReturnType myFunc(Args args) {
		int var1;
		someCode();
		{
			int var2;
			someOtherCode();
		}
		// var2 no longer in scope, but var1 still is.
		{
			// Can reuse identifier 'var2' without conflict
			float var2;
			yetMoreCode();
		}
		etcetera();
	}

It's a very useful construct in ensuring that temporaries don't last
longer than they ought to. (Syntactically anyway... the compiler may or
may not actually translate that directly in the executable. But the
point is to avoid programmer slip-ups.)


T

-- 
In theory, there is no difference between theory and practice.


More information about the Digitalmars-d mailing list