How to location of variable on stack or on heap

anonymous

I have some unusual requirement that a variable should always be there on heap and not on stack. Now I tried doing this using private destructor and static method of class which will simply take pointer and call delete on it.

Class A
{
public :
static void method(const A* ptr)
{
delete ptr;
}
private :
~A();
};

But now I am just curious to see better altrnative and one thing came to my mind is if I can add some pre-check to each method to see wheather variable is on stack or on heap then I do not have to declare static method. Can anyone tell me how to do that? I just got one solution on my mind is to use sbrk(0) or pthread API to get boundary of current stack and then compare it with address of class variable but not portable.

Thanks Niraj Rathi

Matthieu M.

Actually, your solution does not work.

using Buffer = std::aligned_storage<sizeof(A), alignof(A)>::type;

int main() {
    // Allocate scratch area to build a 'A'
    Buffer buffer;

    // Build a 'A' using placement new
    new (&buffer) A();

    // Ensure proper deletion at end of scope using `unique_ptr`
    // and a custom deleter (and cast to `A*` as we go).
    std::unique_ptr<A, A::method> a(reinterpret_cast<A*>(&buffer));

    a->method(0);
    // ...
}

So, on top of preventing arbitrary destruction, you also need to prevent arbitrary construction (which includes copy-construction and move-construction). You can leave assignment public because it assigns to an existing object, and by controlling construction you ensured that all existing objects are placed where you wish them to.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to demonstrate where a c# variable is stored, stack or heap

From Dev

Java variable placed on stack or heap

From Dev

How is memory arranged in stack, heap?

From Dev

Pointer variable inside a function points to stack or heap?

From Dev

How to find whether a given address is in heap or in stack

From Dev

How to translate assembly to C with stack implemented on heap?

From Dev

How to find out if the memory belongs to heap or stack?

From Dev

Stack or heap?Which one to choose for known size variable?

From Dev

Where are the variable/reference names or types stored in memory for stack/heap variables?

From Dev

Where is the local final variable in method stored (Stack/Heap)?

From Dev

What part of an array local variable is stored on the stack? And which part is on the heap?

From Dev

Java reference variable declared outside method lives on stack or on heap

From Dev

How can I tell whether a pointer points to the stack or to the heap?

From Dev

How to examine the heap and stack of an RTEMS application using gdb?

From Dev

How exactly do stack vs. heap allocation work in Rust?

From Dev

For how big object heap should be used instead stack?

From Dev

How does the paging concept work with heap and stack memory?

From Dev

Are these created on the heap or stack

From Java

What and where are the stack and heap?

From Dev

Ref - Parameters - Stack or Heap

From Dev

Stack/Heap in JVM

From Dev

Stack and heap in PHP?

From Dev

Heap and Stack segment

From Dev

A vector in the heap vs in the stack

From Dev

Member values on stack or heap?

From Dev

Initialize struct on stack or heap ?

From Dev

Strings - Stack and heap in java

From Dev

primitive in object, heap or stack?

From Dev

Prefer heap over stack?

Related Related

  1. 1

    How to demonstrate where a c# variable is stored, stack or heap

  2. 2

    Java variable placed on stack or heap

  3. 3

    How is memory arranged in stack, heap?

  4. 4

    Pointer variable inside a function points to stack or heap?

  5. 5

    How to find whether a given address is in heap or in stack

  6. 6

    How to translate assembly to C with stack implemented on heap?

  7. 7

    How to find out if the memory belongs to heap or stack?

  8. 8

    Stack or heap?Which one to choose for known size variable?

  9. 9

    Where are the variable/reference names or types stored in memory for stack/heap variables?

  10. 10

    Where is the local final variable in method stored (Stack/Heap)?

  11. 11

    What part of an array local variable is stored on the stack? And which part is on the heap?

  12. 12

    Java reference variable declared outside method lives on stack or on heap

  13. 13

    How can I tell whether a pointer points to the stack or to the heap?

  14. 14

    How to examine the heap and stack of an RTEMS application using gdb?

  15. 15

    How exactly do stack vs. heap allocation work in Rust?

  16. 16

    For how big object heap should be used instead stack?

  17. 17

    How does the paging concept work with heap and stack memory?

  18. 18

    Are these created on the heap or stack

  19. 19

    What and where are the stack and heap?

  20. 20

    Ref - Parameters - Stack or Heap

  21. 21

    Stack/Heap in JVM

  22. 22

    Stack and heap in PHP?

  23. 23

    Heap and Stack segment

  24. 24

    A vector in the heap vs in the stack

  25. 25

    Member values on stack or heap?

  26. 26

    Initialize struct on stack or heap ?

  27. 27

    Strings - Stack and heap in java

  28. 28

    primitive in object, heap or stack?

  29. 29

    Prefer heap over stack?

HotTag

Archive