Thread: are methods stored in the heap or the stack?

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    71

    are methods stored in the heap or the stack?

    I know that memory addresses in the stack can contain either values or references to other memory addresses, but do these memory addresses also contain methods or are the methods themselves located in the heap?

    The confusion comes from the fact that in C# a delegate variable can be assigned either a method's identifier, an inline function, a lambda expression, or a new instance of the delegate type with the method's identifier passed as an argument to the constructor. My guess is that assigning the method's identifier directly to the delegate variable is just a simplified way of calling the delegate type's constructor with the method's identifier as an argument to the parameter, something that the compiler handles for you.

    But even in this last case, the delegate variable is said to point toward the method itself. In that case, does it mean that methods are stored in the heap, just as reference type values are?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    While I'm not claiming to know the inner workings of C#, you might want to read:

    Code segment - Wikipedia, the free encyclopedia
    Data segment - Wikipedia, the free encyclopedia


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    are methods stored in the heap or the stack?
    Answer: no.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stack or Heap ?
    By Stuart Dickson in forum C Programming
    Replies: 4
    Last Post: 05-06-2010, 12:40 PM
  2. Should I use the Heap or the Stack?
    By Swerve in forum C++ Programming
    Replies: 2
    Last Post: 10-08-2009, 12:38 PM
  3. stack and heap objects
    By John_L in forum C++ Programming
    Replies: 4
    Last Post: 03-18-2008, 10:20 AM
  4. heap or stack?
    By sawer in forum C++ Programming
    Replies: 4
    Last Post: 01-24-2006, 02:36 AM
  5. Stack and Heap
    By Barjor in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 01-29-2002, 09:11 AM