Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Recursive Code

Reply
Thread Tools

Recursive Code

 
 
cplusplusquestion@gmail.com
Guest
Posts: n/a
 
      06-18-2008
I've got a recursive function, but the problem is that I need to use a
very big data to test it. Always my program goes to segmentation
fault. Does the problem for RECURSIVE FUNCTION comes with memory
thing? Change to not recursive seems very difficult. Is there any
suggestion for that?
 
Reply With Quote
 
 
 
 
anon
Guest
Posts: n/a
 
      06-18-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> I've got a recursive function, but the problem is that I need to use a
> very big data to test it. Always my program goes to segmentation
> fault. Does the problem for RECURSIVE FUNCTION comes with memory


Sound like you are corrupting the memory. You can check your program
with memory checker like valgrind

> thing? Change to not recursive seems very difficult. Is there any
> suggestion for that?

 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      06-18-2008
(E-Mail Removed) wrote:
> I've got a recursive function, but the problem is that I need to use a
> very big data to test it. Always my program goes to segmentation
> fault. Does the problem for RECURSIVE FUNCTION comes with memory
> thing? Change to not recursive seems very difficult. Is there any
> suggestion for that?


You could be running out of stack (too many recursions). Run your
application under a debugger and see where it fails.

--
Ian Collins.
 
Reply With Quote
 
cplusplusquestion@gmail.com
Guest
Posts: n/a
 
      06-18-2008
On Jun 18, 6:01 pm, Ian Collins <(E-Mail Removed)> wrote:

> You could be running out of stack (too many recursions). Run your
> application under a debugger and see where it fails.
>


Yes. I guess it runs out of stack. But my problems are: too difficult
to change the code into non recursive. Any suggestion to reduce use
the stack or reuse the stack?
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      06-18-2008
(E-Mail Removed) wrote:
> On Jun 18, 6:01 pm, Ian Collins <(E-Mail Removed)> wrote:
>
>> You could be running out of stack (too many recursions). Run your
>> application under a debugger and see where it fails.
>>

>
> Yes. I guess it runs out of stack. But my problems are: too difficult
> to change the code into non recursive. Any suggestion to reduce use
> the stack or reuse the stack?


Use dynamic allocation where you can. It's hard to give decent advice
without seeing the code.

--
Ian Collins.
 
Reply With Quote
 
cplusplusquestion@gmail.com
Guest
Posts: n/a
 
      06-18-2008
> Use dynamic allocation where you can. It's hard to give decent advice
> without seeing the code.


My code like:

void fun(Array a){
for(int i=0; i<MAX; i++){

 
Reply With Quote
 
cplusplusquestion@gmail.com
Guest
Posts: n/a
 
      06-18-2008
On Jun 18, 6:22 pm, (E-Mail Removed) wrote:
> > Use dynamic allocation where you can. It's hard to give decent advice
> > without seeing the code.

>

My code like:

void fun(Array a){
for(int i=0; i<MAX; i++){
....
a = ...;
fun(a);
}
}

Here, "Array a" will updated each time and I need "Array a" keeps
previous status, so dynamic allocation for "Array a" does not work.
 
Reply With Quote
 
Lionel B
Guest
Posts: n/a
 
      06-18-2008
On Wed, 18 Jun 2008 01:25:51 -0700, cplusplusquestion wrote:

> On Jun 18, 6:22 pm, (E-Mail Removed) wrote:
>> > Use dynamic allocation where you can. It's hard to give decent
>> > advice without seeing the code.

>>

> My code like:
>
> void fun(Array a){
> for(int i=0; i<MAX; i++){
> ....
> a = ...;
> fun(a);
> }
> }
>
> Here, "Array a" will updated each time and I need "Array a" keeps
> previous status, so dynamic allocation for "Array a" does not work.


Perhaps you could implement the recursion with your own stack - e.g.
based on std::stack, which will by default use dynamic allocation - as
opposed to the function call stack. This should not be too tricky, as the
algorithm will be essentially the same: just push context (your Array)
when you would call the recursive function, pop context when you would
return.

--
Lionel B
 
Reply With Quote
 
anon
Guest
Posts: n/a
 
      06-18-2008
(E-Mail Removed) wrote:
> On Jun 18, 6:22 pm, (E-Mail Removed) wrote:
>>> Use dynamic allocation where you can. It's hard to give decent advice
>>> without seeing the code.

> My code like:
>
> void fun(Array a){
> for(int i=0; i<MAX; i++){
> ....
> a = ...;
> fun(a);
> }
> }
>
> Here, "Array a" will updated each time and I need "Array a" keeps
> previous status, so dynamic allocation for "Array a" does not work.


void fun(Array *a)
{
for(int i=0; i<MAX; i++){
....
std::auto_ptr< Array > b( new Array(a) );
fun(b.get());
}
}

Would this work?
 
Reply With Quote
 
cplusplusquestion@gmail.com
Guest
Posts: n/a
 
      06-18-2008
On Jun 18, 7:17 pm, anon <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
> > On Jun 18, 6:22 pm, (E-Mail Removed) wrote:
> >>> Use dynamic allocation where you can. It's hard to give decent advice
> >>> without seeing the code.

> > My code like:

>
> > void fun(Array a){
> > for(int i=0; i<MAX; i++){
> > ....
> > a = ...;
> > fun(a);
> > }
> > }

>
> > Here, "Array a" will updated each time and I need "Array a" keeps
> > previous status, so dynamic allocation for "Array a" does not work.

>
> void fun(Array *a)
> {
> for(int i=0; i<MAX; i++){
> ....
> std::auto_ptr< Array > b( new Array(a) );
> fun(b.get());
> }
>
> }
>
> Would this work?


It seems what I want it. I will try this one then. Thanks!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Embeding pipeline stages to a recursive adder tree code capitan VHDL 3 09-25-2011 09:39 AM
Recursive functions Vs Non-recursive functions - performance aspect vamsi C Programming 21 03-09-2009 10:53 PM
Two recursive calls inside of a recursive function n00m C++ 12 03-13-2008 03:18 PM
detect recursive C code Einar ?rn C Programming 6 11-10-2004 05:29 AM
detect recursive C code Einar ?rn C Programming 0 10-28-2004 02:18 PM



Advertisments