473,790 Members | 2,481 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

operator new with "value type"


Hello,

I'm a developer coming from C++ and Java ... I've going thru
"Effective C#" (which highly recommend for people coming from other
languages wanting to learn C#), and it states that "value types" are
always created on the stack, but then showed the "creation" of one w/
operator new. I guess it makes sense, but is it true that:

MyValueType t;

and

MyValueType t = new MyValueType();

are equivalent w/ regard to where the object is stored? That just
seems odd to me. I suppose this is probably in a FAQ somewhere (I
welcome any pointers to any FAQ list that answers this), but I
couldn't find it stated obviously (tho I didn't spend too long
looking :) ...

-Charlie
Dec 7 '07 #1
24 2078
On Fri, 07 Dec 2007 12:22:29 -0800, carnold <Ch************ @gmail.com
wrote:
[...] I guess it makes sense, but is it true that:

MyValueType t;

and

MyValueType t = new MyValueType();

are equivalent w/ regard to where the object is stored? That just
seems odd to me.
To me too. But yes, the two are equivalent with respect to where the data
is stored. It just happens that the value types use the "new" operator
for initializing even though no actual memory is allocated in that case.

Pete
Dec 7 '07 #2
carnold <Ch************ @gmail.comwrote :
I'm a developer coming from C++ and Java ... I've going thru
"Effective C#" (which highly recommend for people coming from other
languages wanting to learn C#), and it states that "value types" are
always created on the stack
If it says exactly that, then it's wrong.
See http://pobox.com/~skeet/csharp/memory.html
but then showed the "creation" of one w/
operator new. I guess it makes sense, but is it true that:

MyValueType t;

and

MyValueType t = new MyValueType();

are equivalent w/ regard to where the object is stored?
Yes. The value of the variable is an instance of MyValueType, and it's
stored wherever the context of the variable is.
That just seems odd to me.
I think it makes perfect logical sense, but it *is* different to C++.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Dec 7 '07 #3
On Fri, 07 Dec 2007 15:57:17 -0800, carnold <Ch************ @gmail.com>
wrote:
[...]
It's a well understood design principal that you shouldn't change the
meaning of familiar constructs.
It's also well-understood that it's okay to have behaviors that are
different represented by the same syntax, as long as the behavior is
similar enough. That's why operator overloading, and even overloading of
methods, is not only but valuable in many cases.

It's not an either/or, black-and-white question.
"new" has specific implications in
both C++ and Java that don't hold in C# ... I guarantee you I'm not
the first one to think changing those implications a bad idea.
You don't need to guarantee him. He knows. He's heard me complain about
the very same thing in the past. :)

I still wish that a different syntax had been used, but I do understand
the basis for those who say that using "new" is better than other
alternatives. I don't necessarily agree, but I'm not the one who got to
design the language and the issue is too subjective for me to claim
there's an absolute truth to be found.

Pete
Dec 8 '07 #4
Just as a minor comment on the "new" thing... the use of the same
keyword certainly help keep things consistent when considering
generics... if there were two operators, then it would be a little
less clear what "new T()" meant for "SomeMethod<T>( ) where T : new()".
(I'm mainly thinking of default values, and "not found" return values
in the "bool TrySomething(ou t T)" pattern).

Yes, value-types have different behavior - but that isn't really
limited just to construction. They behave differently as method
arguments, for instance - yet the syntax is identical there... I'm not
sure it makes much benefit to single out construction for special
syntax. Personally I'm happy with the shared syntax, but I appreciate
that there is a little bit of a learning curve.

Marc
Dec 8 '07 #5
On Sat, 08 Dec 2007 00:23:15 -0800, Marc Gravell <ma**********@g mail.com>
wrote:
Just as a minor comment on the "new" thing... the use of the same
keyword certainly help keep things consistent when considering
generics... if there were two operators, then it would be a little
less clear what "new T()" meant for "SomeMethod<T>( ) where T : new()".
(I'm mainly thinking of default values, and "not found" return values
in the "bool TrySomething(ou t T)" pattern).
Yup, that's true. Anyone arguing against using "new" to initialize a
struct should be forced to come up with a suitable alternative syntax so
that a generic class or method that has to initialize/instantiate some
instance of one or more type parameters for the generic can still be done
without caring whether the type is a value or reference type.

So, do I have an alternative syntax? No, not at the moment. I'll get
back to you. :)
Yes, value-types have different behavior - but that isn't really
limited just to construction. They behave differently as method
arguments, for instance - yet the syntax is identical there... I'm not
sure it makes much benefit to single out construction for special
syntax. Personally I'm happy with the shared syntax, but I appreciate
that there is a little bit of a learning curve.
It doesn't bother me that much now. In fact, it never really created a
significant problem for me, per se. The main problem was that it for a
little while led me to believe a few types were classes instead of
structs. I also have a vague memory of some sort of uninitialized
variable error that was somehow related to this "new" business, but at the
moment I can't actually remember why it was related (whatever it was, it
was more complicated than just "oh, you need new to initialize your value
type" but I don't recall the details).

But it didn't cause me any hard-to-figure out coding problems and of
course I eventually got better at thinking value-vs-reference type without
getting confused by the overlapping syntax.

I can't say I would ever lobby hard to change the syntax. But I do have
sympathy for those who feel compelled to complain about it. :) And
believe me, if I had a good idea for how to deal with that situation
better, I'd talk about it. I do see the benefit in the current syntax,
and I have no suggestions for something better. But that doesn't mean I
have to like it. :)

Pete
Dec 8 '07 #6
Lew
carnold <Ch************ @gmail.comwrote :
>It's a well understood design principal [sic] that you shouldn't change the
meaning of familiar constructs. "new" has specific implications in
both C++ and Java that don't hold in C#
If that were true no language would look different from FORTRAN.

The trouble with saying "it's ... well understood" is that the passive voice
construction begs the question of *who* understands it. I sure didn't. And I
used to program in FORTRAN for a living. Face it, different languages have
different meanings for things that superficially are similar. (References
since C++, generics between Java and C#, to name two.)

In fact, within FORTRAN itself there were changes within "familiar
constructs". A loop index variable was scoped to the loop in some dialects,
but wider in others. The newer languages changed the meaning of the familiar
construct of "scope", and made it explicit, thus fixing a lot of weird bugs.

Jon Skeet said:
Another change from C++ and Java: you can't fall through from one
switch case to another (although you can have multiple labels for the
same case). Is that a bad idea too? It removes a common source of
errors.
Another case where it's a well-understood design principle that one should
change the meaning of familiar constructs, sometimes.

--
Lew
Dec 9 '07 #7
The main problem was that it for a
little while led me to believe a few types were classes instead of
structs.
I agree that this can be a pain, and it is *so* important to know if
you are talking about a class or a struct - I'm lazy, so I simply
change the IDE color of the various "User Types (...)" items; my
"Value Types" are in magenta, for example. Very helpful when you are
working with something unfamiliar.

(it obviously doesn't help with keyword aliases (int vs string), but
I'm pretty sure I know all of those ;-p)

Marc
Dec 9 '07 #8
Another change from C++ and Java: you can't fall through from one
switch case to another (although you can have multiple labels for the
same case). Is that a bad idea too? It removes a common source of
errors.
Ahh, but you *can* fall-through when you need to, using "goto case". The
best of both worlds -- protection against stupid errors, and the ability to
do what you need where you need it. Even a rather intuitive syntax for
letting the compiler know that the out-of-the-ordinary was intended.
Dec 11 '07 #9
On Dec 11, 4:09 pm, "Ben Voigt [C++ MVP]" <r...@nospam.no spamwrote:
Another change from C++ and Java: you can't fall through from one
switch case to another (although you can have multiple labels for the
same case). Is that a bad idea too? It removes a common source of
errors.

Ahh, but you *can* fall-through when you need to, using "goto case". The
best of both worlds -- protection against stupid errors, and the ability to
do what you need where you need it. Even a rather intuitive syntax for
letting the compiler know that the out-of-the-ordinary was intended.
True - but I wouldn't personally call "explicitly saying where to go
next" as fall-through :)

(One slight pity about the C# 3 spec is that in one place where it
shows how C# has removed a common source of C++ errors, it uses an if
without braces - demonstrating how a completely different source of C+
+ errors is still alive and kicking in C#!)

Jon
Dec 11 '07 #10

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

Similar topics

5
17712
by: johnsuth | last post by:
I want to produce a trivial demonstration of dynamic modification. I thought that pressing a button might change its color. I studied O'Reillys books and successfully created the button with a fancy style, but the onclick fails to do anything no matter what permutation of parameters I try. <input type=button style=background-color:yellow;color:blue;font-family:Arial;font-style:italic;font-weight:bold name="xyz" value="CHANGE COLOUR"...
388
21936
by: maniac | last post by:
Hey guys, I'm new here, just a simple question. I'm learning to Program in C, and I was recommended a book called, "Mastering C Pointers", just asking if any of you have read it, and if it's worth the $25USD. I'm just looking for a book on Pointers, because from what I've read it's one of the toughest topics to understand. thanks in advanced.
2
5665
by: IkBenHet | last post by:
Hello, I am uploading a file using this form in ASP.NET. I have also added a simpel textfield: <form runat="server" enctype="multipart/form-data"> <input type="file" id="oFile" Name="oFile" size="70" runat="Server"> <input type="text" SIZE="20" MAXLENGTH="20" id="Name" NAME="Name"> <input type="submit" id="Submit" runat="Server" value="Submit" OnServerClick="SubmitButton_Click"> </form>
10
21174
by: PB | last post by:
Hi ! I have the following code, which I am using in an Embedded systems, c-compiler.. However I see the same problem with GCC too.. I need the last 10 bits of an address pointer, which is completly know at link time, but will be symbols at the compile time. I used the following code for this initalization.. uint32 data_ptr_mask = ((uint32) data) & 0x3ff;
3
2489
by: IR | last post by:
Hi, I've been trying to do the following (which doesn't compile) : template<class T, class F = Example<T struct Example { F foo(); };
21
2420
by: Steven T. Hatton | last post by:
I'm trying to improve my formal understanding of C++. One significant part of that effort involves clarifying my understanding of the vocabulary used to describe the language. This is from the C++ Standard: "" "...sequence of operators and operands that specifies a computation...". That means to me that an expression can be "executed". I am purposely
5
2275
by: Diwa | last post by:
Does the "value" type (value as in key-value pair )of "std::map" require a default ctor even if it is not used ? If I comment out Line 1 in the code attached later, i.e remove the default ctor of "value" type of map, I get the following error: // -------------------------------------------- /usr/include/c++/3.2.3/bits/stl_map.h:225: no matching function for call to `FieldType::FieldType()'
7
2704
by: LoneHunter01 | last post by:
When I try to compile the below files, the complier says: the WordNode h in BST.cpp: "WordNode" does not name a type also in BST.cpp: "WordNode" undeclared, first use of this function So, any advice? Sorry for the huge post, just wanted to make sure you can understand the problem. As it is, I'm lost.... I've included the WordNode header file, so why can't I create/use WordNode objects/methods (in BST)? WordNode.h #ifndef RANDOM...
1
7084
by: mark | last post by:
Forgive me if this seems like a stupid question but I need help... I'm trying to do a simple online form that emails me the results from a few fields. Here is the code: <form action="http://cm1web1/WebSurveyComponents/script/ processform.asp" method="post">
0
9666
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
10200
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...
0
9021
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...
1
7530
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6769
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5422
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4094
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3707
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2909
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.