access variable through program stack

I am working on garbage collector in C?
How should :confused: I find the part of heap where the variable are stored. It there any compiler (GCC) support for this.

Most variables in C are on the stack - anyway -- regardless,
&variable_name returns the address of the variable itself. If it is a pointer then the contents of memory at that location refer to the start of the data.

If you are messing around with heap and your program calls malloc, be careful. Most malloc implementations set aside metadata areas in heap. You mess with these and you core dump. For example Doug Lea's malloc originally had what amounts to a descriptor
for each malloc call - a pointer to the start of data storage and a 32 bit word that is length of data.

GCC has a number of hooks which can be used to modify the behavior of malloc, realloc, and free by specifying appropriate hook functions. Check out __malloc_hook, __realloc_hook, etc. You can do heap consistency checking using mcheck() and probing using mprobe(). You can use the mallinfo structure to return information about the allocated memory and mallopt to tune memory allocation. However to implement garbage collection in C you are going to have to basically replace malloc and not just "modify" it. Not for the faint hearted!

There are a number of publicly available libraries which implement garbage collection generally using some variation of a mark-sweep algorithm. These libraries allow you to allocate memory as you normally would, i.e. using C malloc or C++ new, without the need to explicitly deallocate memory. One such implementation is the Boehm conservative garbage collector. Have a look at the source code to get an idea of the algorithms used. Another is the Solaris libgc library.