www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - vector crash

reply zjh <fqbqrr 163.com> writes:
```d
import dparse.ast;
import dparse.lexer;
import dparse.parser : parseModule;
import dparse.rollback_allocator : RollbackAllocator;
import core.stdcpp.vector;
import core.stdcpp.string;

Vector!string d=[];toV(a,d);
print(d);//3
foreach(e;d){
     string b=d[i];
     string m=readText(b);ff(m,b);
}

void ff(string B,string s)
{
     LexerConfig config;
     auto cache = StringCache(StringCache.defaultBucketCount);
     auto tokens = getTokensForParser(B, config, &cache);
     RollbackAllocator rba;
     auto m = parseModule(tokens,s, &rba);
     auto v= new V();
     v.visit(m);v.out(s);
}

```
Why can `b` still affect `e` here? Isn't `b` copied?
here foreach crashes.
Jan 17
next sibling parent reply zjh <fqbqrr 163.com> writes:
On Thursday, 18 January 2024 at 03:07:13 UTC, zjh wrote:

 Why can `b` still affect `e` here? Isn't `b` copied?
 here foreach crashes.
Starting from the `third one`, it crashes. It's clearly `vector.d`, but the result is`+Object`. ```d string b=d[i];//the 3th:vector.d //`+Object`,crashes! ```
Jan 17
parent zjh <fqbqrr 163.com> writes:
On Thursday, 18 January 2024 at 03:11:57 UTC, zjh wrote:

 ```d
 string b=d[i];//the 3th:vector.d
 //`+Object`,crashes!
 ```
```d //d[i]==>e string b=e; ``` in the foreach.
Jan 17
prev sibling next sibling parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On Thursday, 18 January 2024 at 03:07:13 UTC, zjh wrote:
 ```d
 import dparse.ast;
 import dparse.lexer;
 import dparse.parser : parseModule;
 import dparse.rollback_allocator : RollbackAllocator;
 import core.stdcpp.vector;
 import core.stdcpp.string;
 ...
 ```
I have no experience with using cpp from D, and I'm not sure exactly what you are trying to do (your code is not complete), but using `string` in this context does not mean C++ std::string, it's a D string (`immutable(char)[]`). Are you sure this is what you are wanting to do? -Steve
Jan 17
parent reply zjh <fqbqrr 163.com> writes:
On Thursday, 18 January 2024 at 03:53:09 UTC, Steven 
Schveighoffer wrote:

 Are you sure this is what you are wanting to do?

 -Steve
```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); } } ``` `gg`: I want to traverse the 'a' file and then call the `ff` function . But there was an error with the `vector` here, the file had 3 or 4 lines, but now it crashes on the 3rd line, and when calling print seperately, the output is normal. I think that here `b` copied `e`, even if the `ff` function goes wrong, it won't affect `e`. I really don't know why it crashed at `.1`?.
Jan 17
parent reply zjh <fqbqrr 163.com> writes:
On Thursday, 18 January 2024 at 04:20:12 UTC, zjh wrote:

     foreach(e;d){
```d foreach(e;d){//.0 string b=e; string m=readText(b);ff(m,b); } ``` or here `.0`? why crash?
Jan 17
parent reply zjh <fqbqrr 163.com> writes:
On Thursday, 18 January 2024 at 04:24:18 UTC, zjh wrote:

```d
class V : ASTVisitor
{
     Vector!string f=[];
     alias visit = ASTVisitor.visit;
     override void visit(const FunctionDeclaration decl)
     {
         writeln(' '.repeat(indentLevel * 4), decl.name.text);
         f.push_back(decl.name.text);
         indentLevel++;
         scope (exit) indentLevel--;
         decl.accept(this);
     }
     void out(string s){
         string b=s~".t";toF(f,b);
     }
}

```
Jan 17
parent Renato <renato athaydes.com> writes:
On Thursday, 18 January 2024 at 04:31:52 UTC, zjh wrote:
 On Thursday, 18 January 2024 at 04:24:18 UTC, zjh wrote:

 ```d
 class V : ASTVisitor
 {
     Vector!string f=[];
     alias visit = ASTVisitor.visit;
     override void visit(const FunctionDeclaration decl)
     {
         writeln(' '.repeat(indentLevel * 4), decl.name.text);
         f.push_back(decl.name.text);
         indentLevel++;
         scope (exit) indentLevel--;
         decl.accept(this);
     }
     void out(string s){
         string b=s~".t";toF(f,b);
     }
 }

 ```
I don't want to sound like a StackOverflow moderator, but if you just post some random code and say something like "why crash", you won't get any helpful responses here or anywhere. The code snippets you've posted don't make any sense and as others mentioned, is obviously incomplete and won't compile. Please explain what you're trying to do, post short code snippets that are readable (don't use names like `a` and `e`, we don't have the context at all of what you're doing - we cannot guess with names like that without spending lots of time basically de-obfuscating your code) and explain what you expected, and how the outcome differs from that. If you do that, lots of people will help, I can guarantee!
Jan 18
prev sibling next sibling parent reply zjh <fqbqrr 163.com> writes:
On Thursday, 18 January 2024 at 03:07:13 UTC, zjh wrote:


```d
import std.stdio, std.range,std.file;
import std.string:strip;

import dparse.ast;
import dparse.lexer;
import dparse.parser : parseModule;
import dparse.rollback_allocator : RollbackAllocator;
import core.stdcpp.vector;
import core.stdcpp.string;

void push(ref vector!string l,string i){
     l.push_back(i);
}
string Rff(string a){
     return readText(a);
}

void Wf(string a,string b){
     std.file.write(a,b);
}

void toF(ref vector!string b,string a){
     writeln("toV");writeln(b.length);
     string d;
     foreach(ref string e;b)d~=e~"\n";
     Wf(a,d);
}

void Qwk(ref vector!string b){
     while(1){
         auto t=b.back();if(t.length!=0)break;
         b.pop_back();
     }
}

void Pr(ref vector!string b){
     foreach(ref string a;b)writeln(a);
}//
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);
}//
string du(char c,int i){
     char[]n;n.length=i;n[]=c;return n.idup;
}

string Jjc(int l,string b){
     string a=du('+',l);return a~b;
}

class V : ASTVisitor
{
     alias visit = ASTVisitor.visit;
     vector!string f=[];
     int l;
     override void visit(const FunctionDeclaration decl)
     {
         writeln(' '.repeat(l * 4), decl.name.text);
         f.push_back(decl.name.text);
         l++;
         scope (exit) l--;
         decl.accept(this);
     }
     void _out(string s){
         string b=s~".t";toF(f,b);
     }
}

void ff(string B,string s)
{
     LexerConfig config;
     auto cache = StringCache(StringCache.defaultBucketCount);
     auto tokens = getTokensForParser(B, config, &cache);
     RollbackAllocator rba;
     auto m = parseModule(tokens,s, &rba);
     auto v= new V();
     v.visit(m);v._out(s);
}
void gg(string a){
     vector!string d=[];toV(a,d);
     Pr(d);//3,OK
     foreach(e;d){
         string b=e;//.1
         string m=readText(b);ff(m,b);
     }
}


void main(){
     string a="filename.txt";
     gg(a);
}

```

A fully compiled version is too tiring, mainly due to the need to 
change the Chinese function name.
Some names are simply changed, otherwise it would be too tiring.
Jan 18
parent zjh <fqbqrr 163.com> writes:
On Friday, 19 January 2024 at 02:13:08 UTC, zjh wrote:


...

`Compile command`:
```d
dmd -i cy2.d dparse.lib
```

need libdparse lib.
Jan 18
prev sibling parent reply zjh <fqbqrr 163.com> writes:
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!
Jan 20
parent reply atzensepp <webwicht web.de> writes:
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?
Jan 21
parent zjh <fqbqrr 163.com> writes:
On Sunday, 21 January 2024 at 18:38:42 UTC, atzensepp wrote:

 What if you add more lines to the file. Does it crash then 
 later?
It's a problem with `vectors`. `vector!string` has a problem , I just use 'string []', and it's OK. The `vector` is unstable, possibly because the `later data` overwrites the `previous data`.
Jan 21