www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - stack out of scope ?

reply Alain De Vos <devosalain ymail.com> writes:
Why doesn't this program dups core ?
Is s not recycled ?
```
import std.stdio:writeln;
void main(){
//I point to the heap
int[] p=[1,2,3];	
	{
		int[3]s=[1,2,3];
		//I point to the stack
		p=s;
	}
	//Why do I still live ?
	writeln(p[0]);
}

```
May 16
next sibling parent reply Alain De Vos <devosalain ymail.com> writes:
This works also,

```
import std.stdio:writeln;

int [] fun(){
	int[3]s=[1,2,3];
	int[] r=s;
	return r;
}	

void main(){
	writeln(fun()[0]);
}
```
May 16
parent reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Sun, May 16, 2021 at 04:40:53PM +0000, Alain De Vos via Digitalmars-d-learn
wrote:
 This works also,
 
 ```
 import std.stdio:writeln;
 
 int [] fun(){
 	int[3]s=[1,2,3];
 	int[] r=s;
 	return r;
 }	
 
 void main(){
 	writeln(fun()[0]);
 }
 ```
https://issues.dlang.org/show_bug.cgi?id=15932 Though I believe if you compile with -dip25 -dip1000 the compiler should emit an error for the above code. If not, please file a bug against -dip1000. T -- Obviously, some things aren't very obvious.
May 16
parent reply Alain De Vos <devosalain ymail.com> writes:
On Sunday, 16 May 2021 at 16:58:15 UTC, H. S. Teoh wrote:
 On Sun, May 16, 2021 at 04:40:53PM +0000, Alain De Vos via 
 Digitalmars-d-learn wrote:
 This works also,
 
 ```
 import std.stdio:writeln;
 
 int [] fun(){
 	int[3]s=[1,2,3];
 	int[] r=s;
 	return r;
 }
 
 void main(){
 	writeln(fun()[0]);
 }
 ```
https://issues.dlang.org/show_bug.cgi?id=15932 Though I believe if you compile with -dip25 -dip1000 the compiler should emit an error for the above code. If not, please file a bug against -dip1000. T
I use ldc2. No dip flags here.
May 16
next sibling parent reply ag0aep6g <anonymous example.com> writes:
On 16.05.21 19:24, Alain De Vos wrote:
 On Sunday, 16 May 2021 at 16:58:15 UTC, H. S. Teoh wrote:
[...]
 Though I believe if you compile with -dip25 -dip1000 the compiler 
 should emit an error for the above code.  If not, please file a bug 
 against -dip1000.


 T
I use ldc2. No dip flags here.
ldc2 understands `-dip1000`. It also understands the newer `-preview=dip1000`. More importantly, the code needs to be safe. system code is not checked for this kind of error.
May 16
parent reply Alain De Vos <devosalain ymail.com> writes:
the flag dip1000 was not shown in the help file.
And indeed for the second program compiling with dip1000 results 
in,
test.d(6): Error: scope variable r may not be returned

But the first program still compiles and runs without problem, 
even with dip1000.
May 16
parent reply Alain De Vos <devosalain ymail.com> writes:
Is there a list of compiler flags not shown ?
May 16
parent reply Kyle <kbcomm protonmail.com> writes:
On Sunday, 16 May 2021 at 18:30:49 UTC, Alain De Vos wrote:
 Is there a list of compiler flags not shown ?
ldc2 -help-hidden
May 16
parent reply Alain De Vos <devosalain ymail.com> writes:
Thanks. Alot of info,but the flag dip1000 is not show in help 
hidden.

The solution seems to be compiling with flag dip1000 and putting 
 safe on top on the file then all the errors are catched by the 
compiler.
May 17
parent Alain De Vos <devosalain ymail.com> writes:
I now compile with the following flags,
```







boundschecks, contracts and invariants) as well as acting as 
-d-debug=1.


ldc2 --dip1000 --dip25 --safe-stack-layout --boundscheck=on --D 
--g --w --de --d-debug test.d
```
May 17
prev sibling parent reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Sun, May 16, 2021 at 05:24:40PM +0000, Alain De Vos via Digitalmars-d-learn
wrote:
 On Sunday, 16 May 2021 at 16:58:15 UTC, H. S. Teoh wrote:
 On Sun, May 16, 2021 at 04:40:53PM +0000, Alain De Vos via
 Digitalmars-d-learn wrote:
 This works also,
 
 ```
 import std.stdio:writeln;
 
 int [] fun(){
 	int[3]s=[1,2,3];
 	int[] r=s;
 	return r;
 }
 
 void main(){
 	writeln(fun()[0]);
 }
 ```
https://issues.dlang.org/show_bug.cgi?id=15932 Though I believe if you compile with -dip25 -dip1000 the compiler should emit an error for the above code. If not, please file a bug against -dip1000.
[...]
 I use ldc2. No dip flags here.
---------snip--------- import std.stdio:writeln; int [] fun() safe { // N.B.: need safe int[3]s=[1,2,3]; int[] r=s; return r; } void main() safe { // N.B.: need safe writeln(fun()[0]); } ---------snip--------- LDC output: ---------snip--------- $ ldc2 -dip1000 /tmp/test.d /tmp/test.d(6): Error: scope variable `r` may not be returned ---------snip--------- T -- People say I'm indecisive, but I'm not sure about that. -- YHL, CONLANG
May 16
next sibling parent reply Alain De Vos <devosalain ymail.com> writes:
On Sunday, 16 May 2021 at 18:27:40 UTC, H. S. Teoh wrote:
 On Sun, May 16, 2021 at 05:24:40PM +0000, Alain De Vos via 
 Digitalmars-d-learn wrote:
 On Sunday, 16 May 2021 at 16:58:15 UTC, H. S. Teoh wrote:
 On Sun, May 16, 2021 at 04:40:53PM +0000, Alain De Vos via 
 Digitalmars-d-learn wrote:
 This works also,
 
 ```
 import std.stdio:writeln;
 
 int [] fun(){
 	int[3]s=[1,2,3];
 	int[] r=s;
 	return r;
 }
 
 void main(){
 	writeln(fun()[0]);
 }
 ```
https://issues.dlang.org/show_bug.cgi?id=15932 Though I believe if you compile with -dip25 -dip1000 the compiler should emit an error for the above code. If not, please file a bug against -dip1000.
[...]
 I use ldc2. No dip flags here.
---------snip--------- import std.stdio:writeln; int [] fun() safe { // N.B.: need safe int[3]s=[1,2,3]; int[] r=s; return r; } void main() safe { // N.B.: need safe writeln(fun()[0]); } ---------snip--------- LDC output: ---------snip--------- $ ldc2 -dip1000 /tmp/test.d /tmp/test.d(6): Error: scope variable `r` may not be returned ---------snip--------- T
So I put everywhere safe ? When not ?
May 16
parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Sun, May 16, 2021 at 06:33:04PM +0000, Alain De Vos via Digitalmars-d-learn
wrote:
 On Sunday, 16 May 2021 at 18:27:40 UTC, H. S. Teoh wrote:
[...]
 ---------snip---------
 import std.stdio:writeln;
 
 int [] fun()  safe {		// N.B.: need  safe
 	int[3]s=[1,2,3];
 	int[] r=s;
 	return r;
 }
 
 void main()  safe {		// N.B.: need  safe
 	writeln(fun()[0]);
 }
 ---------snip---------
 
 
 LDC output:
 
 ---------snip---------
 $ ldc2 -dip1000 /tmp/test.d
 /tmp/test.d(6): Error: scope variable `r` may not be returned
 ---------snip---------
[...]
 So I put everywhere  safe ?
Or put ` safe:` at the top of the file.
 When not ?
Sometimes when you need to perform a system operation that you know is safe, but the compiler cannot prove is safe. Or you need to call a system function (e.g. a C library). T -- Маленькие детки - маленькие бедки.
May 16
prev sibling parent Alain De Vos <devosalain ymail.com> writes:
On Sunday, 16 May 2021 at 18:27:40 UTC, H. S. Teoh wrote:
 On Sun, May 16, 2021 at 05:24:40PM +0000, Alain De Vos via 
 Digitalmars-d-learn wrote:
 On Sunday, 16 May 2021 at 16:58:15 UTC, H. S. Teoh wrote:
 On Sun, May 16, 2021 at 04:40:53PM +0000, Alain De Vos via 
 Digitalmars-d-learn wrote:
 This works also,
 
 ```
 import std.stdio:writeln;
 
 int [] fun(){
 	int[3]s=[1,2,3];
 	int[] r=s;
 	return r;
 }
 
 void main(){
 	writeln(fun()[0]);
 }
 ```
https://issues.dlang.org/show_bug.cgi?id=15932 Though I believe if you compile with -dip25 -dip1000 the compiler should emit an error for the above code. If not, please file a bug against -dip1000.
[...]
 I use ldc2. No dip flags here.
---------snip--------- import std.stdio:writeln; int [] fun() safe { // N.B.: need safe int[3]s=[1,2,3]; int[] r=s; return r; } void main() safe { // N.B.: need safe writeln(fun()[0]); } ---------snip--------- LDC output: ---------snip--------- $ ldc2 -dip1000 /tmp/test.d /tmp/test.d(6): Error: scope variable `r` may not be returned ---------snip--------- T
Trying safe is painfull in practice. Large parge of functions are not safe and call functions which are not safe. Only tiny end-subroutines i can put safe.
May 22
prev sibling parent Steven Schveighoffer <schveiguy gmail.com> writes:
On 5/16/21 12:32 PM, Alain De Vos wrote:
 
 Why doesn't this program dups core ?
 Is s not recycled ?
 ```
 import std.stdio:writeln;
 void main(){
 //I point to the heap
 int[] p=[1,2,3];
      {
          int[3]s=[1,2,3];
          //I point to the stack
          p=s;
      }
      //Why do I still live ?
      writeln(p[0]);
 }
 
 ```
To answer your question, why does this not dump core, it's because you didn't access memory that was invalid. Stack memory, like most computer memory, is allocated in chunks to the program. The OS typically allocates pages at a time per request for it. Which means, even though the memory isn't technically in scope, it's not going to cause a segmentation fault (this is a hardware fault saying you accessed an address that doesn't exist in your process). In order for a dangling pointer (which is what this is) to cause a seg fault, you have to release that memory back to the OS, and make that address invalid before accessing. D very rarely gives memory back to the OS. The more common problem for dangling pointers is that you access a piece of memory that has since been reallocated to something else, and screw up that something (generally a pointer). -Steve
May 16