Thread: Recursion && given stack memory challenge.

  1. #16
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    The non-assembler version is more or less just this.
    Code:
    unsigned int get_stack_address( void )
    {
        unsigned int r = 0;
        return (unsigned int)&r;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  2. #17
    Registered User
    Join Date
    Jan 2024
    Posts
    25
    Quote Originally Posted by Salem View Post
    The non-assembler version is more or less just this.
    Code:
    unsigned int get_stack_address( void )
    {
        unsigned int r = 0;
        return (unsigned int)&r;
    }

    Not getting you exactly , how this gives you the current stack address? like on assembler version it uses esp register where it points to current stack address ..

  3. #18
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    r is a local variable on the stack.

    You take it's address with &

    The assembler "__asm mov dword ptr [r], esp" puts the current stack pointer into a variable.

    The address of a local variable is a small constant offset from the current stack pointer within the same stack frame.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #19
    Registered User
    Join Date
    Jan 2024
    Posts
    25
    Quote Originally Posted by Salem View Post
    r is a local variable on the stack.

    You take it's address with &

    The assembler "__asm mov dword ptr [r], esp" puts the current stack pointer into a variable.

    The address of a local variable is a small constant offset from the current stack pointer within the same stack frame.
    Got you , it's a tricky way you are trying to do.

    like lets say main function its stack frame for instance 0x0 - 0x8 (assumption) now you are saying that getting address of r will like give us 0x9 lets say so by this somehow "approximately " we know know current stack frame size ..

    Yes?


    thanks.

  5. #20
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Yes.

    Because the offset is constant, when you subtract here
    begin_address - get_stack_address()
    the offset goes away.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #21
    Registered User
    Join Date
    Jan 2024
    Posts
    25
    Quote Originally Posted by Salem View Post
    Yes.

    Because the offset is constant, when you subtract here
    begin_address - get_stack_address()
    the offset goes away.
    offset is actually the size of data type " unsigned intr = 0 " , but why offset goes away? I understand that we are doing subtraction ..

    kindly , can you please give an example with some address to example like starting from calling main () and then one iteration rec() executed then to show how offset goes away?

    thanks much.

  7. #22
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Print all the values.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #23
    Registered User
    Join Date
    Jan 2024
    Posts
    25
    Quote Originally Posted by Salem View Post
    Print all the values.

    What do you mean?

  9. #24
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
    unsigned int get_stack_address( void )
    {
        unsigned int r = 0;
        __asm mov dword ptr [r], esp;
        printf("get_stack_address returning %u\n", r );
        return r;
    }
    void rec( int x, const unsigned int begin_address )
    {
        printf("rec: x=%d, begin_address=%u\n", x, begin_address)
    Add more until you understand what's going on.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #25
    Registered User
    Join Date
    Jan 2024
    Posts
    25
    Quote Originally Posted by Salem View Post
    Code:
    unsigned int get_stack_address( void )
    {
        unsigned int r = 0;
        __asm mov dword ptr [r], esp;
        printf("get_stack_address returning %u\n", r );
        return r;
    }
    void rec( int x, const unsigned int begin_address )
    {
        printf("rec: x=%d, begin_address=%u\n", x, begin_address)
    Add more until you understand what's going on.

    I understand , thanks much for your cooperation.

  11. #26
    Registered User
    Join Date
    Dec 2023
    Posts
    7
    you can change your recursion to tail recursion What is Tail Recursion - GeeksforGeeks
    i read book Mastering Algorithms with C Kyle Loudon it is in 3rd chapter and they say compilers are designed for detection tail recursion, it is reuse same stack frame sorry if i am offtopic.

  12. #27
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Tail call optimization isn't a guaranteed feature of C. Many compilers can do it, but you can't count on it in general.

  13. #28
    Registered User
    Join Date
    Dec 2023
    Posts
    7
    Quote Originally Posted by christop View Post
    Tail call optimization isn't a guaranteed feature of C. Many compilers can do it, but you can't count on it in general.
    ok i see, thx i will be careful

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. recursion and stack - trying to understand
    By bos1234 in forum C Programming
    Replies: 2
    Last Post: 09-01-2013, 01:58 AM
  2. Replies: 9
    Last Post: 04-29-2011, 09:26 AM
  3. Stack overflow in recursion
    By Nom1fan in forum C Programming
    Replies: 4
    Last Post: 11-24-2010, 12:51 PM
  4. Callback recursion: stack overflow
    By nicoqwertyu in forum C++ Programming
    Replies: 8
    Last Post: 03-16-2010, 11:09 AM
  5. stack and recursion help needed!
    By LouB in forum C++ Programming
    Replies: 3
    Last Post: 07-01-2002, 02:19 PM

Tags for this Thread