High address of stack in C

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nk28
    New Member
    • Jan 2010
    • 26

    High address of stack in C

    First of all I would like to mention that this is required as a part of project that I am making.

    What I want to do is identify the objects made up at runtime in C for a conservative garbage collector.

    Here I want to search the stack for all the pointer values.
    So I can find one end by simply declaring a variable and finding the address of it.

    The problem comes in finding the other end of stack.

    How can I know its value.

    I guess it would be saying as finding the sp pointer just before my program starts its execution in the cpu.

    Please help...
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    I guess I am stuck at how you know there are no references to an allocation left in the program so that your garbage collector knows it's safe to delete the allocation.

    In C++ you can use a managed pointer but I don't know how to to that in C.

    Comment

    • nk28
      New Member
      • Jan 2010
      • 26

      #3
      More on it....

      Is it possible that I take a pointer and increment its value till it exceeds stack boundary and I get some error ??

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Yes you could do that, of course the error you get might be a segmentation fault.

        Your question lacks the detail to answer since the C standard does not even mandate that there should be a stack. Therefore all stacks are platform defined and finding the end of a stack would have to be a platform defined operation.

        However you have not mentioned the platform.

        Also you seem to be assuming that there is only 1 stack. Are you not running in a multi-threaded environment?

        Comment

        • nk28
          New Member
          • Jan 2010
          • 26

          #5
          yeah ... sorry for that....

          I am working on linux platform and yes for the time being I am assuming a single stack and not associated with a multithreading environment....

          Is there a particular error like SIGBUS that I can check for
          ???

          Comment

          • Oralloy
            Recognized Expert Contributor
            • Jun 2010
            • 988

            #6
            nk28,

            I think you're talking about SIGSEGV - segmentation violation.

            From recollection, there are lots of problems with catching this signal. On some systems it may be impossible. Check your documentation very carefully.

            The other thing you might attempt is to look into the complier's documentation and find the global variable that is supposed to have that value. Most compilers have it, the trick is finding the documentation on the default C-language start-up sequence.

            All that said, Banfa has the right of it. Nothing in the specification states that there has to be a stack of any sort. We just assume a stack, because it's the common implementation mechanism.

            That said, you also have to know if the stack is contiguous - in the old IBM 360/370/390 architecture, the stack was a series of threaded frames in memory. Not only that, but they were statically allocated, if at all possible.

            Good luck!

            Comment

            Working...