473,383 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

naked func for "one-way recurrsion"

Hi all,
I have a tree object I'm writing that searches itself using a recursive
search. Thing is, I'm wanting to also include the ability to simply
transfer execution if I want ("one-way recursion"). _One problem_ with
doing it this way is stack overflow.
What I'm thinking of doing is using "naked" function calls for the
recursive search so that if I want to simply transfer execution, I could
clean up the stack myself. This seems an acceptable solution to me but, I
would be interested in other's opinions of this and of other possible
approaches.
--
Best wishes,
Allen

Jul 22 '05 #1
8 1394
On Sun, 04 Apr 2004 17:04:07 GMT in comp.lang.c++, "Allen"
<allen-terri-ngNO!@#SPAMworldnet.att.net> wrote,
Hi all,
I have a tree object I'm writing that searches itself using a recursive
search. Thing is, I'm wanting to also include the ability to simply
transfer execution if I want ("one-way recursion"). _One problem_ with
doing it this way is stack overflow.
What I'm thinking of doing is using "naked" function calls for the
recursive search so that if I want to simply transfer execution, I could
clean up the stack myself. This seems an acceptable solution to me but, I
would be interested in other's opinions of this and of other possible
approaches.


What the heck are you talking about doing? There are quite a few terms
and phrases in your post that are unclear to me, such as
"naked function"
"transfer execution"
"one-way recursion"
"clean up stack myself"

The best way to "clean up the stack" is to return from the function.
The second best way is to throw an exception and catch it in the caller.
A very distant third is setjump/longjump, and don't even consider it.
There is no fourth way.

The whole point of having a stack is so that you can get back where you
came from. If you don't intend to return, you wouldn't use a recursive
call in the first place, but just search using a loop.

A typical case would be where you want to search for something and when
it is found stop looking. This is implemented by returning the thing
found from your recursive search, until you return to the top level,
which then uses the result or performs the desired operation upon it.

Jul 22 '05 #2
On Sun, 04 Apr 2004 17:04:07 GMT in comp.lang.c++, "Allen"
<allen-terri-ngNO!@#SPAMworldnet.att.net> wrote,
Hi all,
I have a tree object I'm writing that searches itself using a recursive
search. Thing is, I'm wanting to also include the ability to simply
transfer execution if I want ("one-way recursion"). _One problem_ with
doing it this way is stack overflow.
What I'm thinking of doing is using "naked" function calls for the
recursive search so that if I want to simply transfer execution, I could
clean up the stack myself. This seems an acceptable solution to me but, I
would be interested in other's opinions of this and of other possible
approaches.


What the heck are you talking about doing? There are quite a few terms
and phrases in your post that are unclear to me, such as
"naked function"
"transfer execution"
"one-way recursion"
"clean up stack myself"

The best way to "clean up the stack" is to return from the function.
The second best way is to throw an exception and catch it in the caller.
A very distant third is setjump/longjump, and don't even consider it.
There is no fourth way.

The whole point of having a stack is so that you can get back where you
came from. If you don't intend to return, you wouldn't use a recursive
call in the first place, but just search using a loop.

A typical case would be where you want to search for something and when
it is found stop looking. This is implemented by returning the thing
found from your recursive search, until you return to the top level,
which then uses the result or performs the desired operation upon it.

Jul 22 '05 #3
"David Harmon" <so****@netcom.com> wrote in message
news:40***************@news.west.earthlink.net...
On Sun, 04 Apr 2004 17:04:07 GMT in comp.lang.c++, "Allen"
<allen-terri-ngNO!@#SPAMworldnet.att.net> wrote,
Hi all,
I have a tree object I'm writing that searches itself using a recursivesearch. Thing is, I'm wanting to also include the ability to simply
transfer execution if I want ("one-way recursion"). _One problem_ with
doing it this way is stack overflow.
What I'm thinking of doing is using "naked" function calls for the
recursive search so that if I want to simply transfer execution, I could
clean up the stack myself. This seems an acceptable solution to me but, Iwould be interested in other's opinions of this and of other possible
approaches.


What the heck are you talking about doing? There are quite a few terms
and phrases in your post that are unclear to me, such as
"naked function"
"transfer execution"
"one-way recursion"
"clean up stack myself"

The best way to "clean up the stack" is to return from the function.
The second best way is to throw an exception and catch it in the caller.
A very distant third is setjump/longjump, and don't even consider it.
There is no fourth way.

The whole point of having a stack is so that you can get back where you
came from. If you don't intend to return, you wouldn't use a recursive
call in the first place, but just search using a loop.

A typical case would be where you want to search for something and when
it is found stop looking. This is implemented by returning the thing
found from your recursive search, until you return to the top level,
which then uses the result or performs the desired operation upon it.

I looked up "naked" in my docs and found out it is platform
specific--sorry, didn't realize it was.
__declspec(naked) causes the compiler to emit code without a
prolog/epilog allowing you to write your own in asm. What I'm wanting to do
is have a recursive function that I don't have to "un-wind" from if I don't
want to. If I decide that I don't want to return then, I need some way of
removing the return data from the stack. Ugly--I know.

Using a loop to search implies an external object acting on a data set
(doesn't it?). What I'm trying to write is a "self-searching data set" with
some "ANN-ish" ideas thrown in as well (Artificial Neural Net). I need the
ability to either search or execute code--even that's not a real good
explanation of what I'm writing.
I know I've probably butchered numerous terms and not been very clear;
this is the best I can explain without boring everyone. The question
concerns the use of naked functions in my recursive search. Obviously, I
need to be very careful handling the stack. Do you see any other specific
problems with this?
--
Best wishes,
Allen

Jul 22 '05 #4
"David Harmon" <so****@netcom.com> wrote in message
news:40***************@news.west.earthlink.net...
On Sun, 04 Apr 2004 17:04:07 GMT in comp.lang.c++, "Allen"
<allen-terri-ngNO!@#SPAMworldnet.att.net> wrote,
Hi all,
I have a tree object I'm writing that searches itself using a recursivesearch. Thing is, I'm wanting to also include the ability to simply
transfer execution if I want ("one-way recursion"). _One problem_ with
doing it this way is stack overflow.
What I'm thinking of doing is using "naked" function calls for the
recursive search so that if I want to simply transfer execution, I could
clean up the stack myself. This seems an acceptable solution to me but, Iwould be interested in other's opinions of this and of other possible
approaches.


What the heck are you talking about doing? There are quite a few terms
and phrases in your post that are unclear to me, such as
"naked function"
"transfer execution"
"one-way recursion"
"clean up stack myself"

The best way to "clean up the stack" is to return from the function.
The second best way is to throw an exception and catch it in the caller.
A very distant third is setjump/longjump, and don't even consider it.
There is no fourth way.

The whole point of having a stack is so that you can get back where you
came from. If you don't intend to return, you wouldn't use a recursive
call in the first place, but just search using a loop.

A typical case would be where you want to search for something and when
it is found stop looking. This is implemented by returning the thing
found from your recursive search, until you return to the top level,
which then uses the result or performs the desired operation upon it.

I looked up "naked" in my docs and found out it is platform
specific--sorry, didn't realize it was.
__declspec(naked) causes the compiler to emit code without a
prolog/epilog allowing you to write your own in asm. What I'm wanting to do
is have a recursive function that I don't have to "un-wind" from if I don't
want to. If I decide that I don't want to return then, I need some way of
removing the return data from the stack. Ugly--I know.

Using a loop to search implies an external object acting on a data set
(doesn't it?). What I'm trying to write is a "self-searching data set" with
some "ANN-ish" ideas thrown in as well (Artificial Neural Net). I need the
ability to either search or execute code--even that's not a real good
explanation of what I'm writing.
I know I've probably butchered numerous terms and not been very clear;
this is the best I can explain without boring everyone. The question
concerns the use of naked functions in my recursive search. Obviously, I
need to be very careful handling the stack. Do you see any other specific
problems with this?
--
Best wishes,
Allen

Jul 22 '05 #5
On Sun, 04 Apr 2004 19:25:46 GMT in comp.lang.c++, "Allen"
<allen-terri-ngNO!@#SPAMworldnet.att.net> wrote,
Using a loop to search implies an external object acting on a data set
(doesn't it?). What I'm trying to write is a "self-searching data set" with
some "ANN-ish" ideas thrown in as well (Artificial Neural Net).
No, a loop could just as easily be in a member function.
concerns the use of naked functions in my recursive search. Obviously, I
need to be very careful handling the stack. Do you see any other specific
problems with this?


Well, you asked for opinions and you've got mine: just say no. "Naked"
functions sounds like a good way for the compiler to let you implement
specific low-level necessarily platform specific things e.g. interrupt
handlers, and a really lousy thing to use in ordinary application code.

Among the great many problems with it are:
- It would be a huge effort to check for objects on the stack needing
destruction and properly call their destructors. The compiler will do
that for you. Not doing that is simply wrong.
- Murphy's law sez that as soon as you have put all that effort into
writing platform specific code, you will discover a need for a
platform-independent solution.

Returning from the function is quick and easy and I haven't heard
anything from you that suggests to me that you need anything else.
If nothing else, write it that way first and get it working.
Don't optimize before profiling.

Jul 22 '05 #6
On Sun, 04 Apr 2004 19:25:46 GMT in comp.lang.c++, "Allen"
<allen-terri-ngNO!@#SPAMworldnet.att.net> wrote,
Using a loop to search implies an external object acting on a data set
(doesn't it?). What I'm trying to write is a "self-searching data set" with
some "ANN-ish" ideas thrown in as well (Artificial Neural Net).
No, a loop could just as easily be in a member function.
concerns the use of naked functions in my recursive search. Obviously, I
need to be very careful handling the stack. Do you see any other specific
problems with this?


Well, you asked for opinions and you've got mine: just say no. "Naked"
functions sounds like a good way for the compiler to let you implement
specific low-level necessarily platform specific things e.g. interrupt
handlers, and a really lousy thing to use in ordinary application code.

Among the great many problems with it are:
- It would be a huge effort to check for objects on the stack needing
destruction and properly call their destructors. The compiler will do
that for you. Not doing that is simply wrong.
- Murphy's law sez that as soon as you have put all that effort into
writing platform specific code, you will discover a need for a
platform-independent solution.

Returning from the function is quick and easy and I haven't heard
anything from you that suggests to me that you need anything else.
If nothing else, write it that way first and get it working.
Don't optimize before profiling.

Jul 22 '05 #7

"Allen" <allen-terri-ngNO!@#SPAMworldnet.att.net> schrieb im
Newsbeitrag
news:be********************@bgtnsc04-news.ops.worldnet.att.net...
Hi all,
I have a tree object I'm writing that searches itself using a recursive search. Thing is, I'm wanting to also include the ability to simply
transfer execution if I want ("one-way recursion"). _One problem_ with doing it this way is stack overflow.


Think of this flood fill:
void Fill(long x, long y)
{
Fill(x+1, y); Fill(x, y+1); ...
}

Now, you could do this:
queue<points> have_to_fill;
have_to_fill.add_last(pt);
while(have_to_fill.size())
{
fill_around_first_of_queue_and_append_to_last(&hav e_to_fill);
have_to_fill.remove_head();
}

Know what I mean? Use a dymanic array instead of recursion if you fear
a stack overflow.
HTH,

--
-Gernot

Post here, don't email. If you feel you have to mail, revert my
forename from:
to******************************@invalid.com
________________________________________
Looking for a good game? Do it yourself!
GLBasic - you can do
www.GLBasic.com
Jul 22 '05 #8

"Allen" <allen-terri-ngNO!@#SPAMworldnet.att.net> schrieb im
Newsbeitrag
news:be********************@bgtnsc04-news.ops.worldnet.att.net...
Hi all,
I have a tree object I'm writing that searches itself using a recursive search. Thing is, I'm wanting to also include the ability to simply
transfer execution if I want ("one-way recursion"). _One problem_ with doing it this way is stack overflow.


Think of this flood fill:
void Fill(long x, long y)
{
Fill(x+1, y); Fill(x, y+1); ...
}

Now, you could do this:
queue<points> have_to_fill;
have_to_fill.add_last(pt);
while(have_to_fill.size())
{
fill_around_first_of_queue_and_append_to_last(&hav e_to_fill);
have_to_fill.remove_head();
}

Know what I mean? Use a dymanic array instead of recursion if you fear
a stack overflow.
HTH,

--
-Gernot

Post here, don't email. If you feel you have to mail, revert my
forename from:
to******************************@invalid.com
________________________________________
Looking for a good game? Do it yourself!
GLBasic - you can do
www.GLBasic.com
Jul 22 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Christian Seberino | last post by:
I specify packages in my setup.py but what if I want all py files in a directory but one??? How remove just that one?? (Without listing all files I want explicitly??) I need header files to...
0
by: Chan | last post by:
Hi I am getting the following error when I call a web service method from .Net 2002 Application "One or more header elements marked as mustUnderstand was not understood BUT it works from...
26
by: Michel Rouzic | last post by:
I have a binary file used to store the values of variables in order to use them again. I easily know whether the file exists or not, but the problem is, in case the program has been earlier...
8
by: Grant Richard | last post by:
Using the TcpListener and TcpClient I created a program that just sends and receives a short string - over and over again. The program is fine until it gets to around 1500 to 1800 messages. At...
3
by: JohnR | last post by:
I have a form with a number of text boxes, comboboxes etc. What I would like to do is create an event handler for the "mouseenter" event for each of the controls whereby I display information...
5
by: Francesc | last post by:
Hi, I'm programming some classes in order to be able to import different text formats (TXT, RTF, HTML, ...) to my own format called RUF. I've defined one Solution within several Projects, each...
1
by: myth.drannon | last post by:
Hi , I'm trying to find a simple solution to this question.. ( not just making two match parts) I have <xsl:template match=" one | two " > and then I want to test what match I have and do...
3
by: sklett | last post by:
This BUG that is so ridiculous I can't believe they shipped 05 is making my life hell. I have tried the various solutions found on the web and none of them have worked thus far. What's even more...
12
by: spibou | last post by:
Why is a pointer allowed to point to one position past the end of an array but not to one position before the beginning of an array ? Is there any reason why the former is more useful than the...
4
by: rempit | last post by:
In 1 form..have 2 "DIV".. "DIV" one is for SEARCH.. "DIV" two is for showing the RESULT from Database of "DIV" one Button.. Everything in one page.. Anyone can help me please..
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.