This is a discussion on Searching for a particular address in a given range within the C Programming forums, part of the General Programming Boards category; pointers...
Last edited by xyz1987; 11-20-2010 at 04:03 AM.
Umm, you mean like if(ptr >= low && ptr <= high)?
What kind of pointer is ptr? What kind of pointers are low and high?
If you understand what you're doing, you're not learning anything.
basically i m making a garbage collector in c. so i have high and low address of stack. i want to check whether a memory location, which was allocated by malloc lies between high and low of stack. so i need a function for finding if a particular memory address lies in a given address range
Malloc doesn't put things on the stack, but on the heap.
Also, I am not sure you can do what you want in C for several reasons.
But most importantly, I think you need to understand:
a) If you are still confused with stack vs heap memory then you need to learn more before you can attempt such a project.
b) There is no real need for a garbage collector in C. The point was to keep C very close to the architecture and have the programs run very fast rather than drag some garbage utility along. Having a garbage collector goes completely against the ideals (or lack thereof) of the C language.
1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
3. Get rid of conio.h and other antiquated DOS crap headers.
4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.
malloc does put things on the heap.. but the pointers which point to these objects are in the stack
A memory leak due to losing track of allocated memory is quite a common programming error. A garbage collector that assumes all allocated memory is represented somewhere in the stack will never detect, let alone recover, such leaked memory.
In any event, if your purpose is writing a garbage collector, you might want to look up the Boehm garbage collector (eg here).
Right 98% of the time, and don't care about the other 3%.
Just in case anyone feels like still helping, when their answers are being duplicated...
Searching for a particular address in a given range - Dev Shed
Searching For A Particular Address In A Given Range - C And C++ | Dream.In.Code
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.
I support http://www.ukip.org/ as the first necessary step to a free Europe.