I'm trying to test if a variable has been allocated to the heap or the stack. I'm looking for a plugin/IDE that can clearly point out if a variable is in the heap or stack.

I've been looking into DotMemory and Visual Studio's default memory profiling, but I'm not sure where to look. The only thing I've been able to do is to find out if an object is in the heap or not.

Here's some example code of what I'm testing:

        // EXAMPLE 1 // ----------------------------------------------------------- //

    public void Example_1()
        int v1 = 11;
        float v2 = 22.0f;
        int? v3 = 33;
        float? v4 = 44.0f;
        Console.WriteLine("Finished Example 1");

    // EXAMPLE 2 // ----------------------------------------------------------- //

    public delegate int PerformCalculation(int x, int y);

    public int Calculate(int x, int y)
        return x + y;

    public class Example2
        public int v1;

    public void Example_2()
        Example2 e = new Example2();
        PerformCalculation v2 = Calculate;
        Console.WriteLine("Finished Example 2");

I want to be able to say v1 goes in the stack and look here's the proof.

This might seem odd but I plan on writing an article depicting different scenarios where a variable will go to either the stack or heap depending on the function. And I want to be 100% correct and have proof.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.