Thread: Searching for a particular address in a given range

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    3

    Searching for a particular address in a given range

    pointers
    Last edited by xyz1987; 11-20-2010 at 05:03 AM.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    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.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    3
    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

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    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.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    3
    malloc does put things on the heap.. but the pointers which point to these objects are in the stack

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by xyz1987 View Post
    malloc does put things on the heap.. but the pointers which point to these objects are in the stack
    That's not necessarily true. There is nothing preventing a programmer from forgetting to store the return value from malloc() into a pointer. If that happens, the pointers to such memory will not be anywhere in the stack, or anywhere reachable by walking from 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%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help in structure comparison
    By lokachari in forum C Programming
    Replies: 6
    Last Post: 11-04-2009, 03:55 PM
  2. I am very new . . . :(
    By Eternalglory47 in forum C++ Programming
    Replies: 6
    Last Post: 09-05-2008, 11:29 AM
  3. Replies: 10
    Last Post: 09-04-2008, 01:27 PM
  4. address out of bounds in sockets program
    By newbie_socketsp in forum Networking/Device Communication
    Replies: 2
    Last Post: 08-05-2008, 06:41 AM
  5. DX - CreateDevice - D3DERR_INVALIDCALL
    By Tonto in forum Game Programming
    Replies: 3
    Last Post: 12-01-2006, 07:17 PM

Tags for this Thread