vector crash

atzensepp webwicht at web.de
Sun Jan 21 18:38:42 UTC 2024


On Sunday, 21 January 2024 at 03:54:35 UTC, zjh wrote:
> On Thursday, 18 January 2024 at 03:07:13 UTC, zjh wrote:
>
>
> ```d
> void toV(string a,ref vector!string b){
>     auto f=File(a,"r");
>     while(!f.eof()){
>         string l=strip(f.readln());push(b,l);
>     }
>     Qwk(b);
> }//
> ```
>
> There is an issue with the `vector` here, I don't know why the 
> subsequent functions will affect the content of the `vector` 
> here!

Hello,

some thoughts on your issue:

1. you said that it fails in the foreach loop.
```d
void gg(string a){
     Vector!string d=[];toV(a,d);//File to Vector
     print(d);//3,OK
     foreach(e;d){
         string b=e;//.1
         string m=readText(b);ff(m,b);
     }
}
```

I was thinking that the foreach loop is essentially utilizing an 
iterator.
When a print function accesses the value it invokes the mechanics 
of the iterator to get the value. But if you assign the value it 
might be a pointer to the iterator instead of the value.

Can you try to iterate in a different manner without the iterator?

2. You perform push and pop functions of the vector to remove 
trailing empty lines that might change the memory layout. if you 
do not copy the values you might have outdated pointers.

What if you add more lines to the file. Does it crash then later?






More information about the Digitalmars-d-learn mailing list