473,396 Members | 2,004 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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
Jul 21 '05 #1
13 2143
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:
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

Jul 21 '05 #2
faktujaa <fa******@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #3
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:
faktujaa <fa******@discussions.microsoft.com> wrote:
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.

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

Jul 21 '05 #4
Nishith Pathak <Ni***********@discussions.microsoft.com> wrote:
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...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #5
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:
Nishith Pathak <Ni***********@discussions.microsoft.com> wrote:
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...

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

Jul 21 '05 #6
On Tue, 26 Oct 2004 08:11:03 -0700, faktujaa wrote:
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.

--
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.
Jul 21 '05 #7
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:
On Tue, 26 Oct 2004 08:11:03 -0700, faktujaa wrote:
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.

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

Jul 21 '05 #8
faktujaa <fa******@discussions.microsoft.com> wrote:
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.


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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #9
faktujaa <fa******@discussions.microsoft.com> wrote:
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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #10
<Chris Dunaway <"dunawayc[[at]_lunchmeat_sbcglobal[dot]]net">> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #11
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:
faktujaa <fa******@discussions.microsoft.com> wrote:
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

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

Jul 21 '05 #12
Nishith Pathak <Ni***********@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #13
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/de...fVBSpec9_6.asp

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

http://msdn.microsoft.com/library/de...lrfNewOpPG.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

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om... <Chris Dunaway <"dunawayc[[at]_lunchmeat_sbcglobal[dot]]net">> wrote:
> 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.

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

Jul 21 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: John | last post by:
If a value type is immutable, I guess it's threadsafe to read it? But not threadsafe to assign a new value to it (can any value type be truely immutable? Isn't assigning a totally new value to it,...
5
by: Zach | last post by:
When it is being said that, "value types are created on the stack or inline as part of an object". If a value type is created in an object, and that object is being called, the value type in that...
2
by: bs | last post by:
I have a hashtable that contains structures. Is it possible to modify a value of one of these structures. It only seems to work if the hash contains class objects, not structures?
19
by: Dennis | last post by:
I have a public variable in a class of type color declared as follows: public mycolor as color = color.Empty I want to check to see if the user has specified a color like; if mycolor =...
13
by: faktujaa | last post by:
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...
6
by: jjturon | last post by:
Can anyone help me?? I am trying to pass a Select Query variable to a table using Dlookup and return the value to same select query but to another field. Ex. SalesManID ...
11
by: garyusenet | last post by:
I have 'cli via c# on order', and in the mean time am reading 'Pro C# 2005 and the .NET platform' (Andrew Troelson). I'm just reading about the 'five types defined in the CTS'. Specifically Struct....
10
by: MasterGaurav \(www.edujini-labs.com\) | last post by:
Hi, I have a, probably very basic, query on value-types. Why cannot I use the == operator directly for value-types (not primitive types like int, float, double etc)? Why do I need to either...
22
by: Nehil | last post by:
Does C follow call by value convention or call by reference? i see that there is nothing like reference in C standard but it is referenced. still, what should be the answer for the above...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.