Searching for a particular address in a given range

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...

  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,171
    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,528
    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 in reply to you, it is likely you deserve it. Suck it up, sunshine, and read this, this, and this before posting again.

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

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, 12:29 PM
  3. Replies: 10
    Last Post: 09-04-2008, 02: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, 07: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


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21