C ++ / C-> isOnStack () Object

I would like to be able to determine if the pointer is on the stack or not at runtime for a number of reasons. For example, if I pass it into a function call, I can determine if I need to clone it or not. or I need to delete it.

In Microsft C (VC 6,7,8), is there a way to restrict pointer checking to see if it is on the stack or not? I'm only interested in defining this in the thread that owns the stack on which the object was placed.

sort of

static const int __stack_size and __stack_top

???? Thanks!

+5
source share
9 answers

, , , . , , , , .

, , , , . , , , . , .

, , . , ( - - ).

, , - . .

+9

.

, , . . isOnStack (void * ptr) , "ptr" - .

, . , , .

, .

+7

- , .

, . . ! , ( ).

Boost. , , .

+5

. , . /. . , .

, fastcall . . MSDN. , , . MS thiscall . & - .

, , , .

, !

+3

, IMO . ( WIntel):

, (, ).

IsOnStack , .

abotu . , .


, boost:: shared_ptr, . ( boost , "" , ).

"" :

inline void boost_null_deleter(void *) {}

template <typename T> inline
boost::shared_ptr<T> unmanaged_ptr(T * x)
{
   return boost::shared_ptr<T>(x, ::boost_null_deleter);
}

Foo local = { ... };
FooPtr heapy(new Foo);

FunnyFunc(unmanaged_ptr(&local));
FunnyFunc(heapy);
0

​​ ++, . , , - , , , , , , , , - auto_ptr boost:: shared_ptr - .

0

, , , , , . . , ...

-, C ++. , asm{ }.

-, . V++/x86 , , , ESP EBP .

ESP ( , ) , EBP (Extended Base Pointer) . x86.

, , .. , . , .

, . (EBP). - Oy V++. EBP , . .

, , , ? a ? , , , ( :-))

, , , - Gh flag. _penter .. , , , , .

, ....

0

. , ( ), . , .

, - .

auto_ptr , , .

0

MSVC Windows. , , , . , , , :)

bool __isOnStack (const void * ptr)
{

  • // FS: [0x04] 4 Win9x and NT Top of stack
    // FS: [0x08] 4 Win9x and NT Current bottom of the stack
    const char * sTop;
    const char * SBot;
     __asm ​​{
    mov EAX, FS: [04h]
           mov [sTop], EAX
           mov EAX, FS: [08h]
           mov [sBot], EAX

    }
     return (sTop> ((const char *) ptr) && ((const char *) ptr)> sBot);

}

0
source

All Articles