C programming question

Community Forums/General Help/C programming question

Yasha(Posted 2010) [#1]
To anyone who is fluent with C or the details of assembly -

I had the idea that it might be fun to show off a bit, and implement a GC in an assignment for my C programming course. I have a reasonably clear idea of how I would like to go about this, but it involves tracing through the stack to see if references to an object exist.

Question: is it permissible/possible to get the address of a variable on the bottom of the stack, and then count down from it (stack always grows downwards?) to the current position in a linear fashion? Can I rely on the stack to be linear? Is accessing this memory permissible? (it works on simple tests on my computer, but I haven't been able to find an answer online as to whether I can rely on this behaviour on other systems - within reasonable limits eg. x86 only.) I know this wouldn't cover global variables or registers, but don't much care as I'll be happy with a simple implementation.

Answer constraints: please don't post any source code, as this is for an academic assignment (this is the reason I haven't tried simply obtaining a third-party GC and looking through it - I looked at the documentation for the Boehm GC but didn't find anything that looked informative).

Thanks for reading!


Otus(Posted 2010) [#2]
The stack specifics depend both on architecture and on compiler - even with x86 and GCC there may in principle be differences depending on how the system was set up. You should also check compiler alignment options to make sure whether memory pointers can only appear at aligned positions.


Yasha(Posted 2010) [#3]
Thanks for the tip. Sadly, I didn't get it done in time to add it to my project. On the offchance that anyone's interested, and would be able to give further suggestions (solely as a matter of curiosity), this is as far as I got (it's really quite pathetic):

GC.c:


GC.h:


I think the moral of this story is that I have no idea what I'm doing here... isn't that always the fun way? In future I'll definitely be using the one Boehm, Demers and Weiser have very kindly put together in advance.