object on heap or stack or data segment? 
Author Message
 object on heap or stack or data segment?

Hi!

Given a pointer value, is there a way to tell if it is on the heap, or
stack or global data segment.  Basically I want to be able to write a
function like this:

#define HEAP 0
#define STACK 1
#define DATA 2

int where_is_pointer (void *ptr) {
  if (ptr_is_on_heap)
    return HEAP;
  else if (ptr_is_on_stack)
    return STACK;
  else if (ptr_is_on_data_segment)
    return DATA;

Quote:
}

An email would be much appreciated, sometimes there is a considerable delay
before our news server picks the articles.

Thanks,

Prasad

{ In short, No.  But if you rewrite your own allocator function new() to provide
  a did_you_allocate(void* p) function, you could get somewhere. -mod}


      [ about comp.lang.c++.moderated. First time posters: do this! ]



Fri, 16 Apr 1999 03:00:00 GMT  
 object on heap or stack or data segment?



Quote:

>Hi!

>Given a pointer value, is there a way to tell if it is on the heap, or
>stack or global data segment.

Not portable i.e. there is no way defined by the C language.

Quote:
> Basically I want to be able to write a
>function like this:

>#define HEAP 0
>#define STACK 1
>#define DATA 2

>int where_is_pointer (void *ptr) {
>  if (ptr_is_on_heap)
>    return HEAP;
>  else if (ptr_is_on_stack)
>    return STACK;
>  else if (ptr_is_on_data_segment)
>    return DATA;
>}

While there may be ways to do this on particular C compiler/platforms
you'll be locking your code into that compiler platform. I suggest that
you avoid trying to do this.

--
-----------------------------------------


-----------------------------------------



Fri, 16 Apr 1999 03:00:00 GMT  
 object on heap or stack or data segment?

Quote:

> Given a pointer value, is there a way to tell if it is on the heap, or
> stack or global data segment.  Basically I want to be able to write a
> function like this:

> #define HEAP 0
> #define STACK 1
> #define DATA 2

> int where_is_pointer (void *ptr) {
>   if (ptr_is_on_heap)
>     return HEAP;
>   else if (ptr_is_on_stack)
>     return STACK;
>   else if (ptr_is_on_data_segment)
>     return DATA;
> }

As the moderator said:

Quote:
> { In short, No.  But if you rewrite your own allocator function new() to provide
>   a did_you_allocate(void* p) function, you could get somewhere. -mod}

No it's not possible in general.

But why would you want to do that ?
1) debugging: you want to check a ptr before deleting it
   Try:
     - to find a special debuuging heap
     - make one yourself; it's not hard if you don't care about speed
       and if you only want to do basic tests

2) foo get a ptr; if it's in the heap, delete it, then realloc it;
   more generaly any function whose behaviour depend on the fact ptr
   can be desallocated or not:
      never do that; it has no sens; I only write it because I know
      some people who have thought about that

--

Valentin Bonnard

http://www.pratique.fr/~bonnardv (Informations sur le C++ en Francais)


      [ about comp.lang.c++.moderated. First time posters: do this! ]



Sat, 17 Apr 1999 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. how to get stack, data segment and heap address

2. stack, heap and data segment

3. setting the stack and data segment on Windows CE for x86

4. memory flush of text, data, heap, stack of C under HP Unix

5. /STACK increase the stack, means decrease the heap ?

6. Changing a data segment to a code segment in OS/2

7. /STACK increase the stack, means decrease the heap ?

8. Newbie Question - Stack vs Heap

9. Stack/heap-based inconsistency

10. Static properties: Stack or Heap?

11. Heap and Stack

12. Stack and heap

 

 
Powered by phpBB® Forum Software