int number = new int();

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tony Johansson

    int number = new int();

    Hello!

    If I write this statement
    int number = new int();
    I don't get compiler error or run time error
    but how will the compiler read such a statement ?

    For example will number be a reference to a new int on the heap?

    //Tony



  • Jon Skeet [C# MVP]

    #2
    Re: =?ISO-8859-1?Q?int_number_ =3D_new_int();? =

    Tony Johansson <johansson.ande rsson@telia.com wrote:
    If I write this statement
    int number = new int();
    I don't get compiler error or run time error
    but how will the compiler read such a statement ?
    >
    For example will number be a reference to a new int on the heap?
    No. The above is the same as writing:

    int number = 0;

    "new" doesn't imply "create object on the heap" in C# - it just implies
    "call the constructor". For value types, that doesn't create a new
    object, it just initializes a new value.

    --
    Jon Skeet - <skeet@pobox.co m>
    Web site: http://www.pobox.com/~skeet
    Blog: http://www.msmvps.com/jon.skeet
    C# in Depth: http://csharpindepth.com

    Comment

    • Cor Ligthert [MVP]

      #3
      Re: int number = new int();

      Hi,

      int is in C# a shortcut for Int32 (even in 64bits computers).

      Int32 is a structure they never are placed on the heap.

      Cor


      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: int number = new int();

        On Sep 26, 11:58 am, "Cor Ligthert [MVP]" <notmyfirstn... @planet.nl>
        wrote:
        int is in C# a shortcut for Int32 (even in 64bits computers).
        Correct.
        Int32 is a structure they never are placed on the heap.
        Well, they're on the heap:
        1) If they're boxed
        2) If they're part of another value which is on the heap

        For instance:

        class Foo
        {
        int x;
        }

        Here, any instance of Foo lives on the heap, including its x variable.

        "Structs live on the stack" is a myth.
        See http://pobox.com/~skeet/csharp/memory.html for more info.

        Jon

        Comment

        • Tony Johansson

          #5
          Re: int number = new int();

          Hello!

          This is not really true:
          Int32 is a structure they never are placed on the heap.
          because of
          If you box an int like.
          int number = 1;
          object o = number; // here number is still a value type of type Int32 but is
          copied to heap when being boxed
          // so o is stored on the stack and will
          reference number which is stored on the heap.

          //Tony


          "Cor Ligthert [MVP]" <notmyfirstname @planet.nlwrote in message
          news:e2HuvZ8HJH A.2156@TK2MSFTN GP05.phx.gbl...
          Hi,
          >
          int is in C# a shortcut for Int32 (even in 64bits computers).
          >
          Int32 is a structure they never are placed on the heap.
          >
          Cor
          >

          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: int number = new int();

            On Sep 26, 1:55 pm, "Tony Johansson" <t.johans...@lo gica.comwrote:
            This is not really true:Int32 is a structure they never are placed on the heap.
            >
            because of
            If you box an int like.
            int number = 1;
            object o = number; // here number is still a value type of type Int32 but is
            copied to heap when being boxed
                                          // so o is stored on the stack and will
            reference number which is stored on the heap.
            No, the variable "number" is still on the stack. The value of "o" is a
            reference to a boxed copy of the value which number had at the time of
            the assignment. If you change the value of number, that won't be seen
            if you then look at o.

            Jon

            Comment

            • Tony Johansson

              #7
              Re: int number = new int();

              Hello!

              Assume you have the following.
              int number = 1;
              object o = number;
              If you here change the value of number it won't change what o is refering to
              because that
              is stored on the heap.
              So we have one value of number which is stored on the stack and when we do
              object o = number
              a copy of this actual value number is put on the heap. f we now change
              number by using this statement
              number = 2;
              the number that o is refering to is still 1;

              I almost 99.99 % sure that this is correct.

              //Tony

              "Jon Skeet [C# MVP]" <skeet@pobox.co mwrote in message
              news:7686af60-2029-4dce-bd98-2a52ccdc084c@e5 3g2000hsa.googl egroups.com...
              On Sep 26, 1:55 pm, "Tony Johansson" <t.johans...@lo gica.comwrote:
              This is not really true:Int32 is a structure they never are placed on
              the heap.
              >
              because of
              If you box an int like.
              int number = 1;
              object o = number; // here number is still a value type of type Int32 but
              is
              copied to heap when being boxed
              // so o is stored on the stack and will
              reference number which is stored on the heap.
              No, the variable "number" is still on the stack. The value of "o" is a
              reference to a boxed copy of the value which number had at the time of
              the assignment. If you change the value of number, that won't be seen
              if you then look at o.

              Jon

              Comment

              • Jon Skeet [C# MVP]

                #8
                Re: int number = new int();

                On Sep 26, 2:28 pm, "Tony Johansson" <t.johans...@lo gica.comwrote:
                Assume you have the following.
                int number = 1;
                object o = number;
                If you here change the value of number it won't change what o is referingto
                because that is stored on the heap.
                Yes.
                So we have one value of number which is stored on the stack and when we do
                object o = number
                a copy of this actual value number is put on the heap.
                Yes.
                If we now change number by using this statement
                number = 2;
                the number that o is refering to is still 1;
                Correct.
                I almost 99.99 % sure that this is correct.
                Absolutely. I was merely correcting this statement that you made
                before:

                // so o is stored on the stack and will
                // reference number which is stored on the heap.

                "o" doesn't reference "number". It reference an object which was
                created with a value which happened to be the value of "number" at the
                time. The two variables are independent immediately after the
                assignment.

                Jon

                Comment

                • Tony Johansson

                  #9
                  Re: int number = new int();

                  Hello!

                  I just wonder if it's then correct to say that
                  Int32 is a structure they never are placed on the heap.
                  I mean when you are boxing a copy is actually stored on the heap
                  as I described

                  //Tony


                  "Jon Skeet [C# MVP]" <skeet@pobox.co mwrote in message
                  news:73b37d53-7e92-4e35-b5d8-f5d96ce40be2@k3 0g2000hse.googl egroups.com...
                  On Sep 26, 2:28 pm, "Tony Johansson" <t.johans...@lo gica.comwrote:
                  Assume you have the following.
                  int number = 1;
                  object o = number;
                  If you here change the value of number it won't change what o is refering
                  to
                  because that is stored on the heap.
                  Yes.
                  So we have one value of number which is stored on the stack and when we do
                  object o = number
                  a copy of this actual value number is put on the heap.
                  Yes.
                  If we now change number by using this statement
                  number = 2;
                  the number that o is refering to is still 1;
                  Correct.
                  I almost 99.99 % sure that this is correct.
                  Absolutely. I was merely correcting this statement that you made
                  before:

                  // so o is stored on the stack and will
                  // reference number which is stored on the heap.

                  "o" doesn't reference "number". It reference an object which was
                  created with a value which happened to be the value of "number" at the
                  time. The two variables are independent immediately after the
                  assignment.

                  Jon

                  Comment

                  • Jon Skeet [C# MVP]

                    #10
                    Re: int number = new int();

                    On Sep 26, 3:15 pm, "Tony Johansson" <t.johans...@lo gica.comwrote:
                    I just wonder if it's then correct to say thatInt32 is a structure theynever are placed on the heap.
                    >
                    I mean when you are boxing a copy is actually stored on the heap
                    as I described
                    Indeed. It's also on the heap when it's part of another object. See my
                    response to Cor.

                    (Even a local variable can be on the heap if it's captured as part of
                    an iterator block or anonymous function...)

                    Jon

                    Comment

                    • OD

                      #11
                      Re: int number = new int();

                      Well, they're on the heap:
                      1) If they're boxed
                      2) If they're part of another value which is on the heap
                      that's just like an optical effect, it seems to be on the heap but
                      strictly speaking you can't say the int is on the heap in both cases
                      you're listing. It can be confusing for beginners.
                      In case 1) the value is transformed into an object and this object is
                      stored on the heap, initial value is not really on the heap.
                      In case 2) the value is localy stored in a structure (parent object's
                      memory area) that is on the heap, it is not really and directly on the
                      heap by itself.

                      It's a bit like saying an egg can fly... if you bring it with you in a
                      plane ! :-)

                      --


                      OD___



                      Comment

                      • Jon Skeet [C# MVP]

                        #12
                        Re: =?ISO-8859-1?Q?int_number_ =3D_new_int();? =

                        OD <OD <webmaster @ e-naxos dot com>wrote:
                        Well, they're on the heap:
                        1) If they're boxed
                        2) If they're part of another value which is on the heap
                        >
                        that's just like an optical effect, it seems to be on the heap but
                        strictly speaking you can't say the int is on the heap in both cases
                        you're listing.
                        The data which represents the integer lives somewhere in memory, right?
                        That memory is on the heap. Therefore the value is on the heap, to my
                        mind. It's certainly not on the stack.
                        It can be confusing for beginners.
                        In case 1) the value is transformed into an object and this object is
                        stored on the heap, initial value is not really on the heap.
                        No, but the value *in the box* is on the heap. The object contains the
                        same data (as a copy) as the original value.
                        In case 2) the value is localy stored in a structure (parent object's
                        memory area) that is on the heap, it is not really and directly on the
                        heap by itself.
                        I didn't say it was on the heap *by itself*. I said it was on the heap.
                        I believe that to be entirely accurate, and with appropriate
                        explanation (linked in the post) I don't think it confuses people.
                        Compare that with the "structs are on the stack, classes are on the
                        heap" myth: at that point there's a contradiction when you consider a
                        class containing a value type instance variable.
                        It's a bit like saying an egg can fly... if you bring it with you in a
                        plane ! :-)
                        No, it would be like saying an egg can be 10,000 feet above the ground
                        - which it certainly can if it's on a plane.

                        --
                        Jon Skeet - <skeet@pobox.co m>
                        Web site: http://www.pobox.com/~skeet
                        Blog: http://www.msmvps.com/jon.skeet
                        C# in Depth: http://csharpindepth.com

                        Comment

                        • xcal

                          #13
                          Re: int number = new int();

                          hi group,

                          is it really important to know if some object goes to the heap or to the
                          stack?

                          thanks, Carlos

                          "Jon Skeet [C# MVP]" <skeet@pobox.co mwrote in message
                          news:a39a2f50-5f49-466f-ac12-279a4b01bf32@c5 8g2000hsc.googl egroups.com...
                          On Sep 26, 3:15 pm, "Tony Johansson" <t.johans...@lo gica.comwrote:
                          I just wonder if it's then correct to say thatInt32 is a structure they
                          never are placed on the heap.
                          >
                          I mean when you are boxing a copy is actually stored on the heap
                          as I described
                          Indeed. It's also on the heap when it's part of another object. See my
                          response to Cor.

                          (Even a local variable can be on the heap if it's captured as part of
                          an iterator block or anonymous function...)

                          Jon


                          Comment

                          • Peter Duniho

                            #14
                            Re: int number = new int();

                            On Sat, 27 Sep 2008 17:37:36 -0700, xcal <a@a.comwrote :
                            is it really important to know if some object goes to the heap or to the
                            stack?
                            It could be. Lifetime of an object is managed differently depending on
                            where it's stored. Inasmuch as one may need to correctly understand the
                            lifetime of an object, it's important to know where that object is stored.

                            More importantly, it's _always_ important to be accurate in one's
                            statements about behavior in a programming language. People should not
                            make untrue statements about a language, even if they believe that those
                            untrue statements have no practical relevance.

                            The question of whether an object is a reference type or a value type is
                            independent from the question of where the data for that object is
                            stored. Saying otherwise is incorrect.

                            Pete

                            Comment

                            • Jon Skeet [C# MVP]

                              #15
                              Re: =?ISO-8859-1?Q?int_number_ =3D_new_int();? =

                              xcal <a@a.comwrote :
                              is it really important to know if some object goes to the heap or to the
                              stack?
                              That's a very perceptive question. Arguably not, so long as the
                              CLR/framework/compiler hides the details in an efficient enough manner.

                              See http://csharpindepth.com/ViewNote.aspx?NoteID=41

                              However, it can occasionally be an interesting way of thinking about
                              other aspects of behaviour. It's easy enough to see that if data is to
                              be shared between threads, it has to be on the heap - and conversely,
                              that if data is only on the stack, it's threadsafe. (Assuming no
                              *really* nasty hacks, admittedly.)

                              --
                              Jon Skeet - <skeet@pobox.co m>
                              Web site: http://www.pobox.com/~skeet
                              Blog: http://www.msmvps.com/jon.skeet
                              C# in Depth: http://csharpindepth.com

                              Comment

                              Working...