how to determine if an object was allocated on the heap

This is a discussion on how to determine if an object was allocated on the heap within the C++ Programming forums, part of the General Programming Boards category; is there a C or C++ standard library function to determine if a pointer points to an object that was ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    2,545

    how to determine if an object was allocated on the heap

    is there a C or C++ standard library function to determine if a pointer points to an object that was allocated on the heap via malloc() or new? I suspect it would be platform/implementation dependent, but I was just curious about this.

    for example:
    Code:
    void blah()
    {
      char* foo = new char[42];
      char* bar = foo + 23;
      char* baz = is_heap_ptr(bar); // returns a pointer to the base of the allocated block (foo) if bar is on the heap, NULL otherwise;
    }

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    is there a C or C++ standard library function to determine if a pointer points to an object that was allocated on the heap via malloc() or new?
    No, there is no standard library function to do this. Depending on your platform, there may be an OS system function which can do this.
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,535
    Quote Originally Posted by Elkvis View Post
    is there a C or C++ standard library function to determine if a pointer points to an object that was allocated on the heap via malloc() or new?
    Technically? Yes.
    You overload operator new and create some "tracing" mechanism that will let you indirectly know whether the object is on the heap. Can be as simple as an array of pointers to heap allocated objects. Might want to also overload delete to clean up after yourself.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Quote Originally Posted by Mario F. View Post
    Technically? Yes.
    You overload operator new and create some "tracing" mechanism that will let you indirectly know whether the object is on the heap. Can be as simple as an array of pointers to heap allocated objects. Might want to also overload delete to clean up after yourself.
    That will only tell him if an object was declared with "new" or not. It will not say whether a pointer points to memory that was allocated on the heap.

    I will reiterate that there is no standard library function that will tell you if an object was created on the stack or the heap.
    bit∙hub [bit-huhb] n. A source and destination for information.

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,535
    Quote Originally Posted by bithub View Post
    That will only tell him if an object was declared with "new" or not. It will not say whether a pointer points to memory that was allocated on the heap.
    uh?
    If the memory address pointed to by your pointer is on that array, your object is on the heap... because it was declared with new.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    What if the pointer points to memory that was allocated with malloc()? What if it was allocated with VirtualAlloc()? What if it was allocated with posic_memalign()? Or any of the other functions that can be used to allocate heap memory?
    bit∙hub [bit-huhb] n. A source and destination for information.

  7. #7
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,535
    Ok. You are being picky. He asked for C and C++. I gave him a C++ answer. If you want I can give a C answer:

    Do all your allocation/deallocation through functions. Implement similar functionality on those functions.

    I can also give a VirtualAlloc() answer... which is similar to the C answer... or a posic_memalign() answer which is similar to the C answer too.

    I thought my answer would enlighten the OP enough.

    But if you want, I can go the extra mile:

    Before you allocate something add it's memory location to an array. Before you deallocate anything remove it from that array.

    Sheesh! Remind me why I avoid the C++ forum...
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  8. #8
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Look, I never said there was not a way to do this. I said, "there is no standard library function to do this". The OP asked for a standard library function function. One does not exist. The fact that there are different tricks to do what he wanted with various allocation functions is interesting, but it doesn't change the fact that the answer to his question is still "no".
    bit∙hub [bit-huhb] n. A source and destination for information.

  9. #9
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,535
    Yes. Your initial answer was right. I never questioned it. While the rest of your diatribe was unnecessary.

    No. I didn't show him a standard function. I showed him a standard portable way. With one correction...

    Before you allocate something add it's memory location to an array.
    After you allocate something
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. synchronization object choosing
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 03-22-2008, 05:33 AM
  2. Replies: 4
    Last Post: 01-23-2008, 06:21 AM
  3. heap
    By George2 in forum Windows Programming
    Replies: 2
    Last Post: 11-10-2007, 11:49 PM
  4. C++ Object heap stack
    By vasanth in forum C++ Programming
    Replies: 4
    Last Post: 06-08-2006, 06:47 PM
  5. ERRPR: Object reference not set to an instance of an object
    By blackhack in forum C++ Programming
    Replies: 1
    Last Post: 07-13-2005, 06:27 PM

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