# Too many stacked Strings

**URL:** <https://swi-prolog.discourse.group/t/too-many-stacked-strings/4219>\
**Category:** Help!\
**Created:** [July 30, 2021, 6:48pm UTC](https://swi-prolog.discourse.group/t/too-many-stacked-strings/4219 "2021-07-30T18:48:48Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![ferdasonmez](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/ferdasonmez/32/3578_2.png) [@ferdasonmez](https://swi-prolog.discourse.group/u/ferdasonmez)\
**Post date:** [July 30, 2021, 6:48pm UTC](https://swi-prolog.discourse.group/t/too-many-stacked-strings/4219/1 "2021-07-30T18:48:48Z")

</div>

Hi,  
I am having this error, using prolog from python. In stackoverflow it says that, I could call the below command  
swipl -G100g -T20g -L2g  
or I could add the below lines.

```prolog
:- set_prolog_stack(global, limit(100000000000)).
:- set_prolog_stack(trail, limit(20000000000)).
:- set_prolog_stack(local, limit(2000000000)).

```

None of them solved the problem in my case.  
I found out the problem occurs due to a variable MAX\_LG\_STACKED\_STRINGS which is set to 20. The related files are:  
src/os/pl-buffer.h and src/os/pl-buffer.h

 ![image](https://global.discourse-cdn.com/free1/uploads/swiprolog/original/2X/5/5570070a3b0ff24436b719cd9f0e9d50e00fe67c.png)

Can you please help me how to solve this problem? I do not have a C compiler? What should I update?  
Many thanks,  
Ferda

---

<div class="post-metadata">

**Author:** ![ferdasonmez](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/ferdasonmez/32/3578_2.png) [@ferdasonmez](https://swi-prolog.discourse.group/u/ferdasonmez)\
**Post date:** [July 30, 2021, 10:24pm UTC](https://swi-prolog.discourse.group/t/too-many-stacked-strings/4219/2 "2021-07-30T22:24:10Z")

</div>

I have read that compiling for windows is very complicated here.  
[https://www.swi-prolog.org/build/windows.html](https://www.swi-prolog.org/build/windows.html)  
and here it is shown how to do it  
[https://www.swi-prolog.org/build/MinGW.txt](https://www.swi-prolog.org/build/MinGW.txt)

I do not have mingw and pacman in my pc.  
I would appreciate help to solve this issue please.

---

<div class="post-metadata">

**Author:** ![dmchurch](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/dmchurch/32/3427_2.png) [@dmchurch](https://swi-prolog.discourse.group/u/dmchurch)\
**Post date:** [July 30, 2021, 11:38pm UTC](https://swi-prolog.discourse.group/t/too-many-stacked-strings/4219/3 "2021-07-30T23:38:38Z")

</div>

Hi there! Could you tell us what the problem is, what error you’re getting, and what code (at least what query) you’re getting it in? I see a screenshot of the GitHub site but that doesn’t provide any information about what your problem actually _is_.

No matter what, you absolutely should not need to recompile Prolog itself, don’t worry about that!

---

<div class="post-metadata">

**Author:** ![ferdasonmez](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/ferdasonmez/32/3578_2.png) [@ferdasonmez](https://swi-prolog.discourse.group/u/ferdasonmez)\
**Post date:** [July 31, 2021, 7:48am UTC](https://swi-prolog.discourse.group/t/too-many-stacked-strings/4219/4 "2021-07-31T07:48:41Z")

</div>

> [@dmchurch](#):
>
> t the problem is, what error you’re getting, and what code (at least what query) you’re getting it in? I see a sc

Hi  
You are very right. I get the below error, when I run the pl code from python interface. It occurs on relatively larger data which caused more calls mainly (I guess).  
I think if someone could update that constant to like 2000 or if they can make it configurable then the problem will be solved.  
Many thanks and thank you for your interest…  
Ferda

SWI-Prolog: [FATAL ERROR: at Fri Jul 30 21:01:39 2021  
Too many stacked strings]

---

<div class="post-metadata">

**Author:** ![ferdasonmez](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/ferdasonmez/32/3578_2.png) [@ferdasonmez](https://swi-prolog.discourse.group/u/ferdasonmez)\
**Post date:** [July 31, 2021, 7:50am UTC](https://swi-prolog.discourse.group/t/too-many-stacked-strings/4219/5 "2021-07-31T07:50:29Z")

</div>

This link describes the error.

> <https://stackoverflow.com/questions/64095433/fatal-error-too-many-stacked-strings-in-swi-prolog>

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [July 31, 2021, 8:25am UTC](https://swi-prolog.discourse.group/t/too-many-stacked-strings/4219/7 "2021-07-31T08:25:59Z")

</div>

What would help others to help you would be to supply a [Minimal and reproducible working example](https://swi-prolog.discourse.group/t/minimal-and-reproducible-working-examples/2447). 🙂

---

<div class="post-metadata">

**Author:** ![ferdasonmez](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/ferdasonmez/32/3578_2.png) [@ferdasonmez](https://swi-prolog.discourse.group/u/ferdasonmez)\
**Post date:** [July 31, 2021, 8:31am UTC](https://swi-prolog.discourse.group/t/too-many-stacked-strings/4219/8 "2021-07-31T08:31:08Z")

</div>

Hi,  
I pointed out the lines where the error comes from. I think, at this point it would be harder to try to recreate the error in your PCs. It works together with (it is called based on) some metadata stored in python files. So, very difficult to recreate the error in your PCs. I would have to share all my software. Instead, I already found the error source in the C source file. It is due to a limitation of a defined low number.

PL code is not very complicated, it basically makes some path searches in a tree based on some conditions.

Thanks for your interest.  
Ferda

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [July 31, 2021, 8:49am UTC](https://swi-prolog.discourse.group/t/too-many-stacked-strings/4219/10 "2021-07-31T08:49:13Z")

</div>

> [@ferdasonmez](#):
>
> I have read that compiling for windows is very complicated

If you really think that building SWI-Prolog for Windows is one of your better options then see

> **[GitHub - SWI-Prolog/docker-swipl-build-mingw: Docker to cross-compile...](https://github.com/SWI-Prolog/docker-swipl-build-mingw)**
>
> Docker to cross-compile SWI-Prolog for Windows. Contribute to SWI-Prolog/docker-swipl-build-mingw development by creating an account on GitHub.

I have not used it myself but if I had to build a Windows version of SWI-Prolog that is the first thing I would try.

---

<div class="post-metadata">

**Author:** ![ferdasonmez](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/ferdasonmez/32/3578_2.png) [@ferdasonmez](https://swi-prolog.discourse.group/u/ferdasonmez)\
**Post date:** [July 31, 2021, 9:20am UTC](https://swi-prolog.discourse.group/t/too-many-stacked-strings/4219/11 "2021-07-31T09:20:51Z")

</div>

> [@anon95304481](#):
>
> be also switch on MSG\_STRING\_BUFFER debugger message,  
> these messages show me that [SWI-Prolog](https://www.swi-prolog.org/pldoc/doc_for?object=manual) uses multiple buffers:

Hi Again,  
In the PL file I do not do anything related to string processing.  
This is the content of the PL file. I call the path functions from python.

```prolog
path_generic(A,B,Path) :- path(X,Y,Path),sub_atom(X,0,_,_,A), sub_atom(Y,0,_,_,B).
path(A,B,Path) :- travel(A,B,[A],Q), reverse(Q,Path).
travel(A,B,P,[B|P]) :- arc(A,B).
travel(A,B,Visited,Path) :- arc(A,C), C \== B, \+member(C,Visited), travel(C,B,[C|Visited],Path).
%arc_ext(A,B) :- arc(X,Y),sub_atom(X,0,_,_,A), sub_atom(Y,0,_,_,B).

path_generic_nested(A,B,PathN) :- path_nested(X,Y,PathN),sub_atom(X,0,_,_,A), sub_atom(Y,0,_,_,B).
path_nested(A,B,PathN) :- travel_nested(A,B,[A],Q), reverse(Q,PathN).
travel_nested(A,B,P,[B|P]) :- arc(A,B).
travel_nested(A,B,Visited,PathN) :- arc(A,C), C \== B, \+member(C,Visited), travel_nested(C,B,[C|Visited],PathN).
adjacent(E0, E1, L) :- append(_, [E0,E1|_], L).
arc_generic(A,B) :- arc(X,Y),sub_atom(X,0,_,_,A),sub_atom(Y,0,_,_,B).
arc_start_generic(A,B) :- arc(X,Y),sub_atom(X,0,_,_,A).
arc_end_generic(A,B) :- arc(X,Y),sub_atom(Y,0,_,_,B).
arc_with_specific_start(A,B) :- arc(A,_).
arc_with_specific_end(A,B) :- arc(_,B).
path_start_generic(A,B) :- arc(X,Y), sub_atom(X,0,_,_,A).
path_end_generic(A,B) :- arc(X,Y),sub_atom(Y,0,_,_,B).

```

---

<div class="post-metadata">

**Author:** ![ferdasonmez](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/ferdasonmez/32/3578_2.png) [@ferdasonmez](https://swi-prolog.discourse.group/u/ferdasonmez)\
**Post date:** [July 31, 2021, 9:47am UTC](https://swi-prolog.discourse.group/t/too-many-stacked-strings/4219/13 "2021-07-31T09:47:44Z")

</div>

> [@anon95304481](#):
>
> And where does the fatal error happen? Can you bisect your code,  
> by commenting in and out, to figure out where it crashes.  
> Maybe using a debugger or some such.

The error occurs when I call path\_generic. I need to use sub\_atom’s cause my program needs it. This is all I can say. I am sorry. This is part of an academic study and I do not feel very comfortable discussing the details of my code and I am not allowed to do also.

As I said, I can not provide all the details to reproduce it, for it to happen, I need to share all my software which is large and I need to share my metadata and data( which are private). All I can share was the pl file.

I did my best to figure out the origin of the problem and found the lines from the source code. Please help me to solve this problem cause I already lost so much time on this ☹

Many thanks,  
Ferda

---

<div class="post-metadata">

**Author:** ![ferdasonmez](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/ferdasonmez/32/3578_2.png) [@ferdasonmez](https://swi-prolog.discourse.group/u/ferdasonmez)\
**Post date:** [July 31, 2021, 10:13am UTC](https://swi-prolog.discourse.group/t/too-many-stacked-strings/4219/15 "2021-07-31T10:13:13Z")

</div>

I call it like this

‘path\_generic(‘B’,‘21’, P)’

---

<div class="post-metadata">

**Author:** ![ferdasonmez](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/ferdasonmez/32/3578_2.png) [@ferdasonmez](https://swi-prolog.discourse.group/u/ferdasonmez)\
**Post date:** [July 31, 2021, 10:20am UTC](https://swi-prolog.discourse.group/t/too-many-stacked-strings/4219/17 "2021-07-31T10:20:14Z")

</div>

ARCS are defined as below in python and the path generic is called like

```prolog
prolog.assertz('arc(\'A\',\'1\')')

prolog.query('path_generic(\'B\',\'21\', P)')

```

---

<div class="post-metadata">

**Author:** ![Boris](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/boris/32/7486_2.png) [@Boris](https://swi-prolog.discourse.group/u/Boris)\
**Post date:** [July 31, 2021, 10:22am UTC](https://swi-prolog.discourse.group/t/too-many-stacked-strings/4219/18 "2021-07-31T10:22:47Z")

</div>

Please use code blocks for code. You can put three ticks on a line before and after the lines of code. There should be a stickied post about how to use markdown on Discourse

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [July 31, 2021, 10:29am UTC](https://swi-prolog.discourse.group/t/too-many-stacked-strings/4219/19 "2021-07-31T10:29:31Z")

</div>

> [@Boris](#):
>
> Please use code blocks for code

Boris is correct. However until just a moment ago Python was not one of the options. I just added Python to the list. See: [Language highlighting](https://swi-prolog.discourse.group/t/language-highlighting/2260)

---

<div class="post-metadata">

**Author:** ![Boris](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/boris/32/7486_2.png) [@Boris](https://swi-prolog.discourse.group/u/Boris)\
**Post date:** [July 31, 2021, 10:30am UTC](https://swi-prolog.discourse.group/t/too-many-stacked-strings/4219/20 "2021-07-31T10:30:46Z")

</div>

In parallel to figuring out the markdown on Discourse, try to narrow down your problem. This is a necessary first step to reporting it. As @anon95304481 suggested, you can try to use bisection of your code, among other things. Once you create a minimal reproducible example you can share, it becomes possible for others to reproduce your problem and troubleshoot it. You can increase the limit but it will only push the problem to a bigger input, not solve it, as @anon95304481 also explained.

---

<div class="post-metadata">

**Author:** ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)\
**Post date:** [July 31, 2021, 10:36am UTC](https://swi-prolog.discourse.group/t/too-many-stacked-strings/4219/21 "2021-07-31T10:36:20Z")

</div>

> [@anon95304481](#):
>
> Do you call it with string arguments or with atom arguments?

You can also use sub\_string/5, with either atom or string argument. This will also avoid adding to the atom table, if that is the cause of your problem:

```prolog
?- sub_string(abc, 0, _, _, A).
A = "" ;
A = "a" ;
A = "ab" ;
A = "abc".

```

Which version of swipl are you using? e.g.:

```prolog
?- prolog_flag(version, Z).
Z = 'SWI-Prolog 8.3.27 (x86_64-linux): Jul 13 2021, 13:43:28'.

```

(I ran this on Windows, using the Windows Subsystem for Linux (WSL).)

Anyway, from a quick look at your code, you appear to be doing a backtracking search for a path through a graph (with the graph defined by `arc/2` facts). This can quickly produce a huge amount of backtracking. How many facts are in the `arc/2` predicate? How large are the strings (or atoms) that are given to sub\_atom/5?

One more thing you can do, if you don’t want to show your data, is to give the output of a call to `jiti_list(user:_)` (assuming your code is in the default `user` module).

---

<div class="post-metadata">

**Author:** ![ferdasonmez](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/ferdasonmez/32/3578_2.png) [@ferdasonmez](https://swi-prolog.discourse.group/u/ferdasonmez)\
**Post date:** [July 31, 2021, 11:00am UTC](https://swi-prolog.discourse.group/t/too-many-stacked-strings/4219/22 "2021-07-31T11:00:16Z")

</div>

Hi,  
I am getting a feeling that nobody focuses on the lines pointed out in pl-buffer.c and pl-buffer.h.  
Please can you focus on the solution. As you see, I am doing some path searches not String processing. I have ARCS and PATHS. I use ’ not " but when I pasted, somehow it looked different (as if I copied from a word doc.). The error message is below. I get this on larger data (meaning more number of arcs, in my case 38 arcs) not for smaller datasets (in my case 10 arcs). In the larger dataset I have some arcs starting with an alphabet continuing with numbers like ‘23\_1’. The subatoms are needed to process that kind of arcs.  
I hope this information becomes useful.  
Many Thanks,  
Ferda

SWI-Prolog: [FATAL ERROR: at Sat Jul 31 11:35:15 2021  
Too many stacked strings]I am getting

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [July 31, 2021, 11:08am UTC](https://swi-prolog.discourse.group/t/too-many-stacked-strings/4219/24 "2021-07-31T11:08:19Z")

</div>

> [@Boris](#):
>
> There should be a stickied post about how to use markdown on [Discourse](https://www.discourse.org)

It is not easy to find as Markdown by itself because it is a link from this topic.

[Discourse New User Tips and Tricks](https://swi-prolog.discourse.group/t/discourse-new-user-tips-and-tricks/2752)

The relevant links:

[Markdown reference](https://commonmark.org/help/)  
[Markdown Tutorial](https://commonmark.org/help/tutorial/)

* * *

**EDIT**

In the topic [Minimal and reproducible working examples](https://swi-prolog.discourse.group/t/minimal-and-reproducible-working-examples/2447) is also a link to better [Markdown documentation](https://superuser.com/editing-help).

---

<div class="post-metadata">

**Author:** ![Boris](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/boris/32/7486_2.png) [@Boris](https://swi-prolog.discourse.group/u/Boris)\
**Post date:** [July 31, 2021, 11:19am UTC](https://swi-prolog.discourse.group/t/too-many-stacked-strings/4219/25 "2021-07-31T11:19:34Z")

</div>

This limit is there for a reason, we all assume. Maybe none of us truly understands the reason. With a reproducible example, it would be possible to establish that something that should obviously work is broken, and more importantly, know what it is. This way it is possible to fix it without violating the original reasoning behind this admittedly arbitrary limit.

---

<div class="post-metadata">

**Author:** ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)\
**Post date:** [July 31, 2021, 11:22am UTC](https://swi-prolog.discourse.group/t/too-many-stacked-strings/4219/26 "2021-07-31T11:22:26Z")

</div>

> [@ferdasonmez](#):
>
> I am getting a feeling that nobody focuses on the lines pointed out in pl-buffer.c and pl-buffer.h.  
> Please can you focus on the solution.

You’re making a large assumption that these lines are the cause of the problem and not merely a symptom. Because you cannot give a “minimal reproducible example”, we are having to guess as to the cause.

@anon95304481 has suggested a way of finding the problem. You might want to modify that advice slightly, e.g.:

```prolog
path_generic(A,B,Path) :- 
    writeln(user_error, 1-path_generic(A,B,Path)),
    path(X,Y,Path),
    writeln(user_error, 2-path_generic(A,B,Path)-[x=X,y=Y]),
    sub_atom(X,0,_,_,A), 
    writeln(user_error, 3-path_generic(A,B,Path)-[x=X,y=Y,a=A]),
    sub_atom(Y,0,_,_,B),
    writeln(user_error, 4-path_generic(A,B,Path)-[x=X,y=Y,a=A,b=B).

```

This might produce a lot of output, so you might wish to redirect the output to a file. I don’t know how to do this on Windows; on Linux, I’d do something like `swipl ... 2>/tmp/msgs`.

(Instead of using writeln/2, you might do:  
`format(user_error, '~q~n', [1-path_generic(A,B,Path)])`  
etc.)

[Next page](https://swi-prolog.discourse.group/t/too-many-stacked-strings/4219.md?page=2)
