-10
public class Order
{
    static Customer cust = new Customer();
    string sEmpty = "";

    public static void main(String args[])
    {
        int iTotal = 10;
        string sProductName = "Salt";
        Ship shp = new Ship();
    }
}

At the above code, which object and reference is created in the which part of memory? (I mean Heap and Stack)

alt text
(source: c-sharpcorner.com)

Glorfindel
  • 19,729
  • 13
  • 67
  • 91
uzay95
  • 14,378
  • 28
  • 105
  • 167
  • 3
    Shouldn't you be doing your own homework? – timdev Sep 13 '09 at 07:51
  • No because i am not student :) – uzay95 Sep 13 '09 at 07:51
  • Then ask a question that doesn't look 100% like a homework question... – Guffa Sep 13 '09 at 07:55
  • 1
    This link might help: http://tinyurl.com/qs6mzj – Kyle Walsh Sep 13 '09 at 07:57
  • This has been asked before. Lots. Just not by someone demanding to get answers for both .NET and Java. A quick search would have turned up the already answered questions. – blowdart Sep 13 '09 at 07:58
  • @Guffa, ok then. i will not try to ask simple. – uzay95 Sep 13 '09 at 07:59
  • 6
    Wow, the picture you loaded comes directly from this article (http://www.c-sharpcorner.com/UploadFile/rmcochran/csharp_memory01122006130034PM/csharp_memory.aspx) which gives a pretty clear explanation of what you are asking. That plus the link I gave below should be enough to get you going... – Kyle Walsh Sep 13 '09 at 08:08

2 Answers2

4

Since you tagged your question Java, I'll assume you meant in Java. Straight from the horse's mouth:

The Java virtual machine has a heap that is shared among all Java virtual machine threads. The heap is the runtime data area from which memory for all class instances and arrays is allocated.

JVM Spec

Here is a link to a previous SO question that goes into this in serious detail (and is a language-agnostic discussion on the topic).

Here's a link to an article from C# corner detailing the issue in C#.

Community
  • 1
  • 1
Kyle Walsh
  • 2,734
  • 4
  • 26
  • 24
  • 1
    @uzay95 In a word, no. This quote from the C# corner link should help you: "The Stack is more or less responsible for keeping track of what's executing in our code (or what's been "called"). The Heap is more or less responsible for keeping track of our objects (our data, well... most of it - we'll get to that later.)." – Kyle Walsh Sep 13 '09 at 08:10
  • from 1.6 onwards this is no longer true in all cases from an *implementation* perspective. It never was totally true for certain objects like string literals. – ShuggyCoUk Sep 14 '09 at 22:42
1

Order and Customer are on the heap. Though Customer may be a struct, it is a composed member of a reference type (e.g., a class).

All strings are reference types and are created on the heap.

I'm not sure about the Ship class because I don't have its declaration (i.e., I don't know if it is a struct or a class).

The int iTotal variable is created on the stack.

This is true for C#. Java may have different rules at play.

David Andres
  • 29,621
  • 7
  • 42
  • 35
  • David, what do you mean by "Customer may be a struct, it is a composed member of a reference type" ? – Preets Sep 13 '09 at 08:09
  • Customer may have been declared as struct Customer {}. struct makes Customer a Value Type and will under normal circumstances be placed on the stack. However, because Customer is a member of class Order, which is a reference type that is placed on the heap, Customer is placed on the heap as well. – David Andres Sep 13 '09 at 08:14
  • Ah ! Thanks for the explanation ! – Preets Sep 13 '09 at 08:25
  • I always miss a bit of information in all this type of questions in C#/Java. While the real objects (instances) are in the heap, the references that point to them (that take real memory) are stack allocated: sProductName is a stack allocated reference to a heap allocated string... Then again, the language tries to hide the fact that the reference and the object is not the same... unluckily. – David Rodríguez - dribeas Sep 13 '09 at 08:39
  • @dribeas...and then there's boxing. – David Andres Sep 13 '09 at 09:17