heap overflow error

Discussions related to Visual Prolog
Paul Cerkez
VIP Member
Posts: 202
Joined: 6 Mar 2000 0:01

heap overflow error

Unread post by Paul Cerkez » 24 Oct 2011 14:49

good morning everyone, having a brain cramp. :oops:

the following code snippet is throwing a "heap overflow" error after 1,973 items.

Code: Select all

class predicates    processLists : (string*, string*) (i,i).   clauses     processLists([],_):-!.     processLists(_,[]):-!.     processLists([H1|T1],[H2|T2]):-!, %           ...set a directory, pull image file 1 (name listed in H1), %           ...set another directory, pull image file 2 (name listed in H2), %           close 1st image, close 2nd image %          merge two images into a 3rd one with vpi::Draw/4 %          close 3rd image %          save 3rd image to hard drive     processLists(T1,T2), !.  
code does exactly what I need it to do but at exactly 1,973 images, I get the heap error. Both lists contain 10,000 items and I am doing a 1 for 1 image merge to create a 3rd image (10,000 total). I haven't 'experienced a heap overflow since the VIP 4.0 days and I know I am missing something obvious.

any suggestions.
AI Rules!
P.

User avatar
Tonton Luc
VIP Member
Posts: 502
Joined: 16 Oct 2001 23:01

Unread post by Tonton Luc » 24 Oct 2011 15:44

:idea:

Code: Select all

clauses     processLists([],_):-!.     processLists(_,[]):-!.     processLists([H1|T1],[H2|T2]):-         ...your actions here ...         !,         processLists(T1,T2). % only one dot here !!!     processLists([_|T1],[_|T2]):-!, % if your actions failed         processLists(T1,T2).  

Paul Cerkez
VIP Member
Posts: 202
Joined: 6 Mar 2000 0:01

Unread post by Paul Cerkez » 24 Oct 2011 15:52

Tonton,

thanks for the suggestion and that will work is my code hits a fail and has to back up but how does that solve the heap overflow error?
AI Rules!
P.

Harrison Pratt
VIP Member
Posts: 287
Joined: 5 Nov 2000 0:01

Unread post by Harrison Pratt » 24 Oct 2011 21:46

Your code is not tail recursive, because the recursive call is not the last clause. There should be no untried solutions when you finally make the recursive call. The stack frame is not discarded before the last recursive call ... so the stack keeps growing.

I think your image procesing clauses might need to be externalized to a single predicate call which never fails -- then you can eliminate the last clause that handles the error condition:

Code: Select all

processLists(T1,T2), !.

Paul Cerkez
VIP Member
Posts: 202
Joined: 6 Mar 2000 0:01

Unread post by Paul Cerkez » 25 Oct 2011 0:41

thanks Harrison, I reached a similar conclusion about 2 hours ago (did not have internet connectivity so I did not see your post until now.)

I had flash back to the old VIP 3 and 4 days and here is how I solved it;

Code: Select all

class predicates    processLists : (string*, string*) (i,i).   processLists_1 : (string, string) (i,i).   clauses     processLists([],_):-!.     processLists(_,[]):-!.     processLists([H1|T1],[H2|T2]):-!,                 processLists_1(H1,H2),                 processLists(T1,T2), !.        processLists_1(H1,H2):- %           ...set a directory, pull image file 1 (name listed in H1), %           ...set another directory, pull image file 2 (name listed in H2), %           close 1st image, close 2nd image %          merge two images into a 3rd one with vpi::Draw/4 %          close 3rd image %          save 3rd image to hard drive           !.  
works perfectly, created 10,000 merged images in about 2 minutes.
AI Rules!
P.

Harrison Pratt
VIP Member
Posts: 287
Joined: 5 Nov 2000 0:01

Unread post by Harrison Pratt » 25 Oct 2011 13:59

I was reading your mind while I was posting my thoughts! :wink:

Paul Cerkez
VIP Member
Posts: 202
Joined: 6 Mar 2000 0:01

Unread post by Paul Cerkez » 25 Oct 2011 14:05

I guess its true that great minds think alike :-)
AI Rules!
P.

Post Reply