recursion output

L

Lamefif

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) ;
//}

}
 
J

Jim Langston

Lamefif said:
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.
 
L

Lamefif

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ĺ ñ/_ ¹,
 
I

Ian Collins

Lamefif said:
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.
 
D

Default User

Lamefif said:
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
 
J

Jim Langston

Lamefif said:
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.
 
L

Lamefif

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 :)
 
I

Ian Collins

Lamefif said:
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.
 
J

Jim Langston

Ian Collins said:
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.

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.
 
D

Default User

Lamefif said:
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.
Access violation error perhaps?

Hard to say. Maybe, maybe not.



Brian
 
I

Ismo Salonen

Jim said:
Or happen to come across a memory address with 0 in it.

In the example it just skips those characters whose value is 0.
It always goes to recursion. If the braces where in place then it would
work as you described ( C strings end with 0 and ends looping).
The indentation suggest otherwise but it does not count.

ismo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,290
Messages
2,571,453
Members
48,129
Latest member
DianneCarn

Latest Threads

Top