recursion output - c++
This is a discussion on recursion output - c++ ; can anyone explain the output of this function.
im having trouble comprehending it
void ret_str(char* s)
{
if(*s != '\0')
//{
cout<<*(s) ;
ret_str(s+1);
//cout<<*(s) ;
//}
}...
-
recursion output
can anyone explain the output of this function.
im having trouble comprehending it
void ret_str(char* s)
{
if(*s != '\0')
//{
cout<<*(s) ;
ret_str(s+1);
//cout<<*(s) ;
//}
}
-
Re: recursion output
"Lamefif" <Lamefif> wrote in message
news:1185588731.056737.295620@k79g2000hse.googlegroups.com...
> can anyone explain the output of this function.
> im having trouble comprehending it
>
> void ret_str(char* s)
> {
> if(*s != '\0')
> //{
> cout<<*(s) ;
> ret_str(s+1);
> //cout<<*(s) ;
> //}
>
> }
It should just output a string.
char* s is a char pointer, in this case it will point to the beginning of a
c-style string. An array of characters terminated by a null \0.
Now, in your case, as it is, it will print the string forwards. If the
character that s is pointing to is not the end, it will output the
character, then call itself with the pointer incremented by 1.
For example, say the string is:
"hello" which would consist of 'h' 'e' 'l' 'l' 'o' '\0'
First time it's called s is pointing to the 'h'. Since it's not \0 it
outputs the 'h', then calls itself with s+1, or pointing to the 'e' This
continues until the null character is reached where it simply returns.
If the cout is after the recursion call, then it would print the string
backwards. An excercise to the reader to determine why.
-
Re: recursion output
that's what i was thinking too, thats why i dont understand this
output:
hello ? @Ç @Ç 0 @Ç 12crtexe.c__native_startup_state
== __initia
lizedUnknown Runtime Check Error
Stack memory around _alloca was corrupted
A local variable was used before it was initialized
Stack memory was corrupted
A cast to a smaller data type has caused a loss of data. If this was
intentiona
l, you should mask the source of the cast with the appropriate
bitmask. For exa
mple:
char c = (i & 0xFF);
Changing the code in this way will not affect the quality of the
resulting optim
ized code.
dd The value of ESP was not properly saved across a function call.
This is usua
lly a result of calling a function declared with one calling
convention with a f
unction pointer declared with a different calling convention.
Stack around the variable '' was corrupted.The variable '' is
being used wi
thout being initialized.Run-Time Check Failure #%d - %sUnknown Module
NameUnknow
n FilenameRun-Time Check Failure #%d - %sRuntime Check Error.
Unable to display RTC Message.Stack corrupted near unknown
variableStack area a
round _alloca memory reserved by this function is corrupted
%s%s%s%s>
%s%s%p%s%ld%s%d%sStack area around _alloca memory reserved by this
function is c
orrupted
Address: 0x
Size:
Allocation number within this function:
Data: <wsprintfAuser32.dll%.2X A variable is being used without being
initialize
d.Stack around _alloca corruptedLocal variable used before
initializationStack m
emory corruptionCast to smaller type causing loss of dataStack pointer
corruptio
ný A© A£ Ah A@ AáæA°æA _controlfp_s(((void *)0), 0x00010000,
0x00030000)_setd
efaultprecisionintel
\fp8.cMSPDB80.DLLrPDBOpenValidate5EnvironmentDirectorySOFTWA
RE\Microsoft\VisualStudio\8.0\Setup
\VSRegCloseKeyRegQueryValueExARegOpenKeyExAAD
VAPI32.DLLRSDS ÿ Î7v)Mĺ ñ/_ ¹,
-
Re: recursion output
Lamefif wrote:
> that's what i was thinking too, thats why i dont understand this
> output:
>
What were you thinking? Please retain context in your replies.
> hello ? @Ç @Ç 0 @Ç 12crtexe.c__native_startup_state
You get crap output because you commented out the braces, so you get
infinite recursion.
--
Ian Collins.
-
Re: recursion output
Lamefif wrote:
> can anyone explain the output of this function.
> im having trouble comprehending it
>
> void ret_str(char* s)
> {
> if(*s != '\0')
> //{
> cout<<*(s) ;
> ret_str(s+1);
> //cout<<*(s) ;
> //}
>
> }
Eventually, undefined behavior. You're going to walk right off the end
of the string, because there's no ending condition.
What did YOU think it would do, and why?
Brian
-
Re: recursion output
"Lamefif" <Lamefif> wrote in message
news:1185588731.056737.295620@k79g2000hse.googlegroups.com...
> can anyone explain the output of this function.
> im having trouble comprehending it
>
> void ret_str(char* s)
> {
> if(*s != '\0')
> //{
That bracket up there needs to be part of the program
> cout<<*(s) ;
> ret_str(s+1);
> //cout<<*(s) ;
> //}
as does that bracket
> }
Without the brackets it shoudl be formated:
void ret_str(char* s)
{
if(*s != '\0')
cout<<*(s) ;
ret_str(s+1);
}
Now the program will always call ret_str(s+1) even if it's \0
My bad for not catching that in the first place.
-
Re: recursion output
On Jul 28, 6:19 am, "Default User" <defaultuse...@yahoo.com> wrote:
> Lamefif wrote:
> > can anyone explain the output of this function.
> > im having trouble comprehending it
>
> > void ret_str(char* s)
> > {
> > if(*s != '\0')
> > //{
> > cout<<*(s) ;
> > ret_str(s+1);
> > //cout<<*(s) ;
> > //}
>
> > }
>
> Eventually, undefined behavior. You're going to walk right off the end
> of the string, because there's no ending condition.
what do you mean by undefined behavior?
what breaks the loop?
> What did YOU think it would do, and why?
Access violation error perhaps?
thanks for replying all 
-
Re: recursion output
Lamefif wrote:
> On Jul 28, 6:19 am, "Default User" <defaultuse...@yahoo.com> wrote:
>> Lamefif wrote:
>>> can anyone explain the output of this function.
>>> im having trouble comprehending it
>>> void ret_str(char* s)
>>> {
>>> if(*s != '\0')
>>> //{
>>> cout<<*(s) ;
>>> ret_str(s+1);
>>> //cout<<*(s) ;
>>> //}
>>> }
>> Eventually, undefined behavior. You're going to walk right off the end
>> of the string, because there's no ending condition.
>
> what do you mean by undefined behavior?
Exactly what Brian said, you walk past the end of the string and output
garbage until you most likely run out of stack or access something you
shouldn't and your program aborts.
> what breaks the loop?
>
In your case, the operating system or run time when you run out of stack
or access something you shouldn't.
--
Ian Collins.
-
Re: recursion output
"Ian Collins" <ian-news@hotmail.com> wrote in message
news:5h0ni3F3hgik7U7@mid.individual.net...
> Lamefif wrote:
>> On Jul 28, 6:19 am, "Default User" <defaultuse...@yahoo.com> wrote:
>>> Lamefif wrote:
>>>> can anyone explain the output of this function.
>>>> im having trouble comprehending it
>>>> void ret_str(char* s)
>>>> {
>>>> if(*s != '\0')
>>>> //{
>>>> cout<<*(s) ;
>>>> ret_str(s+1);
>>>> //cout<<*(s) ;
>>>> //}
>>>> }
>>> Eventually, undefined behavior. You're going to walk right off the end
>>> of the string, because there's no ending condition.
>>
>> what do you mean by undefined behavior?
>
> Exactly what Brian said, you walk past the end of the string and output
> garbage until you most likely run out of stack or access something you
> shouldn't and your program aborts.
>
>> what breaks the loop?
>>
> In your case, the operating system or run time when you run out of stack
> or access something you shouldn't.
Or happen to come across a memory address with 0 in it.
-
Re: recursion output
Lamefif wrote:
> On Jul 28, 6:19 am, "Default User" <defaultuse...@yahoo.com> wrote:
> > Lamefif wrote:
> > > can anyone explain the output of this function.
> > > im having trouble comprehending it
> >
> > > void ret_str(char* s)
> > > {
> > > if(*s != '\0')
> > > //{
> > > cout<<*(s) ;
> > > ret_str(s+1);
> > > //cout<<*(s) ;
> > > //}
> >
> > > }
> >
> > Eventually, undefined behavior. You're going to walk right off the
> > end of the string, because there's no ending condition.
>
> what do you mean by undefined behavior?
Behavior for which the standard imposes no definition. With no stopping
criteria, you'll eventually access outside the boundaries of that
object and that will be undefined behavior.
> what breaks the loop?
Nothing programatically.
> > What did YOU think it would do, and why?
>
> Access violation error perhaps?
Hard to say. Maybe, maybe not.
Brian
Similar Threads
-
By Application Development in forum Perl
Replies: 0
Last Post: 09-13-2007, 01:45 PM
-
By Application Development in forum c++
Replies: 3
Last Post: 07-19-2007, 09:38 AM
-
By Application Development in forum DOTNET
Replies: 0
Last Post: 04-10-2007, 09:44 AM
-
By Application Development in forum basic.visual
Replies: 1
Last Post: 06-01-2005, 11:24 AM
-
By Application Development in forum Perl
Replies: 1
Last Post: 09-15-2003, 07:36 PM