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

+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 11

recursion output

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

    }


  2. Default 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.



  3. Default 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ĺ ñ/_ ¹,


  4. Default 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.

  5. Default 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

  6. Default 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.



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



  8. Default 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.

  9. Default 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.



  10. Default 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


+ Reply to Thread
Page 1 of 2 1 2 LastLast

Similar Threads

  1. Template-Toolkit: Flushing Output or Incremental Output, how?
    By Application Development in forum Perl
    Replies: 0
    Last Post: 09-13-2007, 01:45 PM
  2. how to control position of output values on output console
    By Application Development in forum c++
    Replies: 3
    Last Post: 07-19-2007, 09:38 AM
  3. Strange problem - page output contains output from another request
    By Application Development in forum DOTNET
    Replies: 0
    Last Post: 04-10-2007, 09:44 AM
  4. input/output parameter: howto get the output from VB6
    By Application Development in forum basic.visual
    Replies: 1
    Last Post: 06-01-2005, 11:24 AM
  5. Output buffering problems during recursion
    By Application Development in forum Perl
    Replies: 1
    Last Post: 09-15-2003, 07:36 PM