473,890 Members | 1,333 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2188
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******@discu ssions.microsof t.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.co m>
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******@discu ssions.microsof t.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.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Jul 21 '05 #4
Nishith Pathak <Ni***********@ discussions.mic rosoft.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.co m>
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.mic rosoft.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.co m>
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_lunch meat_[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_lunch meat_[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******@discu ssions.microsof t.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.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #9
faktujaa <fa******@discu ssions.microsof t.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.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #10

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

Similar topics

9
3200
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, like doing an modification, when no references are involved? I don't know enough about CLR) At the moment the whole: lock(anobject) {
5
2107
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 object, is still created on the stack, I would say, so I don't understand this inline business. Apart from the fact that it is my understanding that "inline" as it exists in C++ doesn't exist in C#. Could someone please shed some light on this...
2
5029
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
2669
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 = Color.Empty then..... or if mycolor is Color.Empty then .......
13
231
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 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
6
4859
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 SalesManName AT Alan Time
11
2382
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. Now Troelson described the struct type as 'a lightweight class type having value based semantics'. Looking at his example I cant see any difference from a class here other than it is defined with the struct keyword? what is the difference...
10
4489
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 have an implicit operator (to type-cast to primitive type) or override/overlaod the == operator myself?
22
4204
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 question?
0
9978
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11222
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10811
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10919
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10461
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9625
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6041
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4270
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3275
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.