Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / General / October 2004

Tip: Looking for answers? Try searching our database.

Value Types - Structures

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
faktujaa - 26 Oct 2004 04:03 GMT
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 - 26 Oct 2004 05:23 GMT
hi
the struct gets allocated on the stack.
What  'new '  does is initialize the value type's  fields to null/zero

regards
Ansil

> Hi All,
> Microsoft says that structures are value types. Also primitive data types
[quoted text clipped - 4 lines]
> Thanks in advance.
> Faktujaa
Jon Skeet [C# MVP] - 26 Oct 2004 08:45 GMT
> Microsoft says that structures are value types. Also primitive data types
> are value types. And memory for value types is allocated on the stack.

Memory for value types is only allocated on the stack in some cases.
See http://www.pobox.com/~skeet/csharp/memory.html

> 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.

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

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nishith Pathak - 26 Oct 2004 12:59 GMT
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

> > Microsoft says that structures are value types. Also primitive data types
> > are value types. And memory for value types is allocated on the stack.
[quoted text clipped - 8 lines]
> "new" just means "call a constructor". It doesn't have anything to do
> with where things are allocated.
Jon Skeet [C# MVP] - 26 Oct 2004 13:28 GMT
> 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

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

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

faktujaa - 26 Oct 2004 16:11 GMT
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

> > New has nothing to do with allocation of memory. It is used to call the
> > constructor.
[quoted text clipped - 4 lines]
> The constructor probably actually sets member variables to *other*
> values, actually. It's not much use as a constructor otherwise...
Chris Dunaway - 26 Oct 2004 16:54 GMT
> 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

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

Signature

Chris

dunawayc[AT]sbcglobal_lunchmeat_[DOT]net

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

faktujaa - 26 Oct 2004 19:35 GMT
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

> > 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
>
> Correct me if I'm wrong, but in C++, new is an operator whereas in VB New
> is a method name.
Jon Skeet [C# MVP] - 26 Oct 2004 19:59 GMT
> 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
[quoted text clipped - 3 lines]
> the memory on the stack. Any help on this is highly appreciated. Thanks in
> advance.

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

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Jon Skeet [C# MVP] - 26 Oct 2004 22:20 GMT
> > 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
>
> Correct me if I'm wrong, but in C++, new is an operator whereas in VB New
> is a method name.

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

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Jay B. Harlow [MVP - Outlook] - 28 Oct 2004 17:04 GMT
Jon (& faktujaa),
> I'm not sure that it counts as an operator as such, but it's certainly
> a keyword rather than a method.
VB.NET considers New an operator (when its not the identifier for the
constructor, aka a method name):

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbls7/html/vblr
fVBSpec9_6.asp


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

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclr
fNewOpPG.asp


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

>> > name(class). Actually i was thinking that by creating a variable with
>> > new
[quoted text clipped - 6 lines]
> I'm not sure that it counts as an operator as such, but it's certainly
> a keyword rather than a method.
Jon Skeet [C# MVP] - 26 Oct 2004 20:02 GMT
> 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.

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

http://www.pobox.com/~skeet/csharp/memory.html

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nishith Pathak - 27 Oct 2004 10:45 GMT
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

> > 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
[quoted text clipped - 6 lines]
>
> http://www.pobox.com/~skeet/csharp/memory.html
Jon Skeet [C# MVP] - 27 Oct 2004 11:16 GMT
> 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

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.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.