473,434 Members | 1,833 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,434 software developers and data experts.

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

}

Jul 28 '07 #1
10 2927
"Lamefif" <La*****@gmail.comwrote in message
news:11**********************@k79g2000hse.googlegr oups.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.
Jul 28 '07 #2
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.DLLrPDBOpenValidate5EnvironmentDirec torySOFTWA
RE\Microsoft\VisualStudio\8.0\Setup
\VSRegCloseKeyRegQueryValueExARegOpenKeyExAAD
VAPI32.DLLRSDS ÿ Î7v)Mĺ ñ/_ ¹,

Jul 28 '07 #3
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.
Jul 28 '07 #4
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
Jul 28 '07 #5

"Lamefif" <La*****@gmail.comwrote in message
news:11**********************@k79g2000hse.googlegr oups.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.
Jul 28 '07 #6
On Jul 28, 6:19 am, "Default User" <defaultuse...@yahoo.comwrote:
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 :)
Jul 28 '07 #7
Lamefif wrote:
On Jul 28, 6:19 am, "Default User" <defaultuse...@yahoo.comwrote:
>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.
Jul 28 '07 #8

"Ian Collins" <ia******@hotmail.comwrote in message
news:5h*************@mid.individual.net...
Lamefif wrote:
>On Jul 28, 6:19 am, "Default User" <defaultuse...@yahoo.comwrote:
>>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.
Jul 28 '07 #9
Lamefif wrote:
On Jul 28, 6:19 am, "Default User" <defaultuse...@yahoo.comwrote:
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

Jul 28 '07 #10
Jim Langston wrote:
"Ian Collins" <ia******@hotmail.comwrote in message
news:5h*************@mid.individual.net...
>Lamefif wrote:
>>On Jul 28, 6:19 am, "Default User" <defaultuse...@yahoo.comwrote:
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.

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

Jul 30 '07 #11

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

Similar topics

4
by: Dan | last post by:
I've encountered some strange behavior in a recursive procedure I'm writing for a bill of materials. First let me ask directly if what I think is happening is even possible: It seems like the...
6
by: Thomas Mlynarczyk | last post by:
Hi, Say I have an array containing a reference to itself: $myArray = array(); $myArray =& $myArray; How can I, looping iteratively through the array, detect the fact that $myArray will "take"...
43
by: Lorenzo Villari | last post by:
I've tried to transform this into a not recursive version but without luck... #include <stdio.h> void countdown(int p) { int x;
5
by: J. Wesolowski | last post by:
Hello, This is my first approach to comp.lang.c, so hello to everyone. I learn c programming from "Teach yourself c " by Aitken & Jones. So far everything is going well and I'm quite excited about...
2
by: jw | last post by:
what is the relation between a stack and recursion func. and if i have recursion funct(a return type void), void print(int n,int base){ static string digits="0123456789abcdef"; if(n>=base){...
19
by: Kay Schluehr | last post by:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496691
3
by: mathon | last post by:
hi, I wanted to solve a little problem with a recursion, so the number of stars should be written to the output according to the parameter m and n. I tried to implement it with a recursion but...
9
by: nishit.gupta | last post by:
Can somebody please help me for algorithm for postorder bst traversal without recursion. It should use stack. i am not able to implement it. thnx
24
by: proctor | last post by:
hello, i have a small function which mimics binary counting. it runs fine as long as the input is not too long, but if i give it input longer than 8 characters it gives RuntimeError: maximum...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.