Value Types - Structures

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

    Value Types - Structures

    Hi All,
    Microsoft says that structures are value types. Also primitive data types
    are value types. And memory for value types is allocated on the stack. Then
    why we need new operator to allocate memory for structure value types and not
    for primitive data types(they r allocated memory on stack as well)??? Please
    help.
    Thanks in advance.
    Faktujaa
  • Ansil MCAD

    #2
    RE: Value Types - Structures

    hi
    the struct gets allocated on the stack.
    What 'new ' does is initialize the value type's fields to null/zero

    regards
    Ansil

    "faktujaa" wrote:
    [color=blue]
    > Hi All,
    > Microsoft says that structures are value types. Also primitive data types
    > are value types. And memory for value types is allocated on the stack. Then
    > why we need new operator to allocate memory for structure value types and not
    > for primitive data types(they r allocated memory on stack as well)??? Please
    > help.
    > Thanks in advance.
    > Faktujaa[/color]

    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: Value Types - Structures

      faktujaa <faktujaa@discu ssions.microsof t.com> wrote:[color=blue]
      > Microsoft says that structures are value types. Also primitive data types
      > are value types. And memory for value types is allocated on the stack.[/color]

      Memory for value types is only allocated on the stack in some cases.
      See http://www.pobox.com/~skeet/csharp/memory.html
      [color=blue]
      > Then why we need new operator to allocate memory for structure value
      > types and not for primitive data types(they r allocated memory on
      > stack as well)??? Please help.[/color]

      "new" just means "call a constructor". It doesn't have anything to do
      with where things are allocated.

      --
      Jon Skeet - <skeet@pobox.co m>
      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

      If replying to the group, please do not mail me too

      Comment

      • Nishith Pathak

        #4
        Re: Value Types - Structures

        New has nothing to do with allocation of memory. It is used to call the
        constructor.
        so by using the new with a structure , you are calling a constructor of the
        structure which sets all its members variables to 0, false or null depending
        on their type

        Nishith Pathak

        "Jon Skeet [C# MVP]" wrote:
        [color=blue]
        > faktujaa <faktujaa@discu ssions.microsof t.com> wrote:[color=green]
        > > Microsoft says that structures are value types. Also primitive data types
        > > are value types. And memory for value types is allocated on the stack.[/color]
        >
        > Memory for value types is only allocated on the stack in some cases.
        > See http://www.pobox.com/~skeet/csharp/memory.html
        >[color=green]
        > > Then why we need new operator to allocate memory for structure value
        > > types and not for primitive data types(they r allocated memory on
        > > stack as well)??? Please help.[/color]
        >
        > "new" just means "call a constructor". It doesn't have anything to do
        > with where things are allocated.
        >
        > --
        > Jon Skeet - <skeet@pobox.co m>
        > http://www.pobox.com/~skeet
        > If replying to the group, please do not mail me too
        >[/color]

        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: Value Types - Structures

          Nishith Pathak <NishithPathak@ discussions.mic rosoft.com> wrote:[color=blue]
          > New has nothing to do with allocation of memory. It is used to call the
          > constructor.
          > so by using the new with a structure , you are calling a constructor of the
          > structure which sets all its members variables to 0, false or null depending
          > on their type[/color]

          The constructor probably actually sets member variables to *other*
          values, actually. It's not much use as a constructor otherwise...

          --
          Jon Skeet - <skeet@pobox.co m>
          Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

          If replying to the group, please do not mail me too

          Comment

          • faktujaa

            #6
            Re: Value Types - Structures

            Thanks for the inputs. Then how does CLR knows that it has to allocate the
            memory on stack/heap. By this, i think it determines based on the type
            name(class). Actually i was thinking that by creating a variable with new
            keyword, we assign the memory on the heap as in C/C++. Anyways thanks for the
            help but still the question of how does the CLR knows abt where to allocate
            the memory is bothering me. Please help.
            Faktujaa

            "Jon Skeet [C# MVP]" wrote:
            [color=blue]
            > Nishith Pathak <NishithPathak@ discussions.mic rosoft.com> wrote:[color=green]
            > > New has nothing to do with allocation of memory. It is used to call the
            > > constructor.
            > > so by using the new with a structure , you are calling a constructor of the
            > > structure which sets all its members variables to 0, false or null depending
            > > on their type[/color]
            >
            > The constructor probably actually sets member variables to *other*
            > values, actually. It's not much use as a constructor otherwise...
            >
            > --
            > Jon Skeet - <skeet@pobox.co m>
            > http://www.pobox.com/~skeet
            > If replying to the group, please do not mail me too
            >[/color]

            Comment

            • Chris Dunaway

              #7
              Re: Value Types - Structures

              On Tue, 26 Oct 2004 08:11:03 -0700, faktujaa wrote:
              [color=blue]
              > name(class). Actually i was thinking that by creating a variable with new
              > keyword, we assign the memory on the heap as in C/C++. Anyways thanks for the[/color]

              Correct me if I'm wrong, but in C++, new is an operator whereas in VB New
              is a method name.

              --
              Chris

              dunawayc[AT]sbcglobal_lunch meat_[DOT]net

              To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
              replace certain words in my E-Mail address.

              Comment

              • faktujaa

                #8
                Re: Value Types - Structures

                Hi,
                Im was not talking of C++ and VB but of C#/VB.NET and C++/VB. Anyways im not
                a VB guy so i don't know anything abt it. Also as per microsoft, the new
                keyword can be used as an operator or as a modifier in C#. As an operator, is
                used to create objects on the heap and invoke constructors. Then by using the
                same new operator to create an object of structure type, why do it allocates
                the memory on the stack. Any help on this is highly appreciated. Thanks in
                advance.
                faktujaa

                "Chris Dunaway" <"dunawayc[[at]_lunchmeat" wrote:
                [color=blue]
                > On Tue, 26 Oct 2004 08:11:03 -0700, faktujaa wrote:
                >[color=green]
                > > name(class). Actually i was thinking that by creating a variable with new
                > > keyword, we assign the memory on the heap as in C/C++. Anyways thanks for the[/color]
                >
                > Correct me if I'm wrong, but in C++, new is an operator whereas in VB New
                > is a method name.
                >
                > --
                > Chris
                >
                > dunawayc[AT]sbcglobal_lunch meat_[DOT]net
                >
                > To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
                > replace certain words in my E-Mail address.
                >[/color]

                Comment

                • Jon Skeet [C# MVP]

                  #9
                  Re: Value Types - Structures

                  faktujaa <faktujaa@discu ssions.microsof t.com> wrote:[color=blue]
                  > Im was not talking of C++ and VB but of C#/VB.NET and C++/VB. Anyways im not
                  > a VB guy so i don't know anything abt it. Also as per microsoft, the new
                  > keyword can be used as an operator or as a modifier in C#. As an operator, is
                  > used to create objects on the heap and invoke constructors. Then by using the
                  > same new operator to create an object of structure type, why do it allocates
                  > the memory on the stack. Any help on this is highly appreciated. Thanks in
                  > advance.[/color]

                  It just creates new instances, wherever they should go. It's as simple
                  as that.

                  --
                  Jon Skeet - <skeet@pobox.co m>
                  Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                  If replying to the group, please do not mail me too

                  Comment

                  • Jon Skeet [C# MVP]

                    #10
                    Re: Value Types - Structures

                    faktujaa <faktujaa@discu ssions.microsof t.com> wrote:[color=blue]
                    > Thanks for the inputs. Then how does CLR knows that it has to allocate the
                    > memory on stack/heap. By this, i think it determines based on the type
                    > name(class). Actually i was thinking that by creating a variable with new
                    > keyword, we assign the memory on the heap as in C/C++. Anyways thanks for the
                    > help but still the question of how does the CLR knows abt where to allocate
                    > the memory is bothering me. Please help.[/color]

                    Did you read the article I linked to? It explains it all there.

                    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.


                    --
                    Jon Skeet - <skeet@pobox.co m>
                    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                    If replying to the group, please do not mail me too

                    Comment

                    • Jon Skeet [C# MVP]

                      #11
                      Re: Value Types - Structures

                      <Chris Dunaway <"dunawayc[[at]_lunchmeat_sbcg lobal[dot]]net">> wrote:[color=blue][color=green]
                      > > name(class). Actually i was thinking that by creating a variable with new
                      > > keyword, we assign the memory on the heap as in C/C++. Anyways thanks for the[/color]
                      >
                      > Correct me if I'm wrong, but in C++, new is an operator whereas in VB New
                      > is a method name.[/color]

                      I'm not sure that it counts as an operator as such, but it's certainly
                      a keyword rather than a method.

                      --
                      Jon Skeet - <skeet@pobox.co m>
                      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                      If replying to the group, please do not mail me too

                      Comment

                      • Nishith Pathak

                        #12
                        Re: Value Types - Structures

                        CLR determin the allocation on heap or stack by checking its type. For
                        example if it is int since it is been a System,int32 structure so it is
                        always be allocated on stack and if it string which represent. System.String
                        class then it is allocated on the Heap.CLR does not look for the new keyword
                        to allocate it in Heap or stack. New keyword is just to initialise the memory
                        through the use of constructor

                        Hope it helps you

                        Nishith

                        "Jon Skeet [C# MVP]" wrote:
                        [color=blue]
                        > faktujaa <faktujaa@discu ssions.microsof t.com> wrote:[color=green]
                        > > Thanks for the inputs. Then how does CLR knows that it has to allocate the
                        > > memory on stack/heap. By this, i think it determines based on the type
                        > > name(class). Actually i was thinking that by creating a variable with new
                        > > keyword, we assign the memory on the heap as in C/C++. Anyways thanks for the
                        > > help but still the question of how does the CLR knows abt where to allocate
                        > > the memory is bothering me. Please help.[/color]
                        >
                        > Did you read the article I linked to? It explains it all there.
                        >
                        > http://www.pobox.com/~skeet/csharp/memory.html
                        >
                        > --
                        > Jon Skeet - <skeet@pobox.co m>
                        > http://www.pobox.com/~skeet
                        > If replying to the group, please do not mail me too
                        >[/color]

                        Comment

                        • Jon Skeet [C# MVP]

                          #13
                          Re: Value Types - Structures

                          Nishith Pathak <NishithPathak@ discussions.mic rosoft.com> wrote:[color=blue]
                          > CLR determin the allocation on heap or stack by checking its type. For
                          > example if it is int since it is been a System,int32 structure so it is
                          > always be allocated on stack[/color]

                          No it won't. For instance:

                          class Test
                          {
                          int i;
                          }

                          The integer here is part of a class, and will always be on the heap,
                          despite being a value type. Claiming that value types are always
                          allocated on the stack is misleading and incorrect.

                          Please see the article I linked to previously.

                          --
                          Jon Skeet - <skeet@pobox.co m>
                          Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                          If replying to the group, please do not mail me too

                          Comment

                          • Jay B. Harlow [MVP - Outlook]

                            #14
                            Re: Value Types - Structures

                            Jon (& faktujaa),[color=blue]
                            > I'm not sure that it counts as an operator as such, but it's certainly
                            > a keyword rather than a method.[/color]
                            VB.NET considers New an operator (when its not the identifier for the
                            constructor, aka a method name):



                            C# considers new an operator (when its not a modifier):



                            In both case the New operator creates an "object" & calls a constructor. As
                            you know in the case of a value type the "object" is created "inline" (stack
                            or another object), while for reference types the "object" is created on the
                            heap.

                            I've always considered new an operator in C++ also, as the new operator (in
                            C++) also allocates space for the object & calls the constructor.

                            Hope this helps
                            Jay

                            "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
                            news:MPG.1be8bd 82ecf4fbe898b80 9@msnews.micros oft.com...[color=blue]
                            > <Chris Dunaway <"dunawayc[[at]_lunchmeat_sbcg lobal[dot]]net">> wrote:[color=green][color=darkred]
                            >> > name(class). Actually i was thinking that by creating a variable with
                            >> > new
                            >> > keyword, we assign the memory on the heap as in C/C++. Anyways thanks
                            >> > for the[/color]
                            >>
                            >> Correct me if I'm wrong, but in C++, new is an operator whereas in VB New
                            >> is a method name.[/color]
                            >
                            > I'm not sure that it counts as an operator as such, but it's certainly
                            > a keyword rather than a method.
                            >
                            > --
                            > Jon Skeet - <skeet@pobox.co m>
                            > http://www.pobox.com/~skeet
                            > If replying to the group, please do not mail me too[/color]


                            Comment

                            Working...