473,804 Members | 3,085 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
24 2082
Hi,
--
Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.
"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:e81c0e05-37c1-4cbb-9495-
(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#!)
I have never seem that error, have you?
Dec 11 '07 #11
<"Ignacio Machin \( .NET/ C# MVP \)" <machin TA laceupsolutions .com>>
wrote:
>(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#!)
I have never seem that error, have you?
Yes, I have. Basically code which ended up reading:

if (someExpression )
FirstStatement( );
SecondStatement ();

but should have read:

if (someExpression )
{
FirstStatement( );
SecondStatement ();
}

Not my code, of course, but I a bug I fixed :)

--
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 11 '07 #12

"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:MP******** *************@m snews.microsoft .com...
<"Ignacio Machin \( .NET/ C# MVP \)" <machin TA laceupsolutions .com>>
wrote:
>>(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#!)
>I have never seem that error, have you?

Yes, I have. Basically code which ended up reading:

if (someExpression )
FirstStatement( );
SecondStatement ();

but should have read:

if (someExpression )
{
FirstStatement( );
SecondStatement ();
}

Not my code, of course, but I a bug I fixed :)
python programmers...
>
--
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 13 '07 #13
Jon Skeet [C# MVP] wrote:
<"Ignacio Machin \( .NET/ C# MVP \)" <machin TA laceupsolutions .com>>
wrote:
>>(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#!)
>I have never seem that error, have you?

Yes, I have. Basically code which ended up reading:

if (someExpression )
FirstStatement( );
SecondStatement ();

but should have read:

if (someExpression )
{
FirstStatement( );
SecondStatement ();
}

Not my code, of course, but I a bug I fixed :)
It must have happen hundred of thousands of times.

The programmer writes:

if (someExpression )
SecondStatement ();

He figure out that he does not need the { } because it is only
one statement.

Then later another statement has to be added and because it is a bit
hectic at the time then it end up as:

if (someExpression )
FirstStatement( );
SecondStatement ();

And suddenly some things stop working.

Arne
Dec 24 '07 #14
Lew wrote:
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.
Fortran 90/95 ?

Not possible in 66 and 77.
(I know that C++ had such a problem before ANSI standardization )

Arne
Dec 24 '07 #15
Hi,
Interesting, it might be the case that I have not seen that much code that
was not mine :)

It would be overkill to force the use of { though.

"Arne Vajhøj" <ar**@vajhoej.d kwrote in message
news:47******** *************** @news.sunsite.d k...
Jon Skeet [C# MVP] wrote:
> <"Ignacio Machin \( .NET/ C# MVP \)" <machin TA laceupsolutions .com>>
wrote:
>>>(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#!)
>>I have never seem that error, have you?

Yes, I have. Basically code which ended up reading:

if (someExpression )
FirstStatement( );
SecondStatement ();

but should have read:

if (someExpression )
{
FirstStatement( );
SecondStatement ();
}

Not my code, of course, but I a bug I fixed :)

It must have happen hundred of thousands of times.

The programmer writes:

if (someExpression )
SecondStatement ();

He figure out that he does not need the { } because it is only
one statement.

Then later another statement has to be added and because it is a bit
hectic at the time then it end up as:

if (someExpression )
FirstStatement( );
SecondStatement ();

And suddenly some things stop working.

Arne
Dec 24 '07 #16
<"Ignacio Machin \( .NET/ C# MVP \)" <machin TA laceupsolutions .com>>
wrote:
Interesting, it might be the case that I have not seen that much code that
was not mine :)

It would be overkill to force the use of { though.
Would it really? I certainly find code much more readable that way, and
it *is* required for various constructs IIRC.

I use them everywhere, and haven't found any disadvantages to it.

--
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 24 '07 #17

"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:MP******** *************@m snews.microsoft .com...
<"Ignacio Machin \( .NET/ C# MVP \)" <machin TA laceupsolutions .com>>
wrote:
>Interesting, it might be the case that I have not seen that much code
that
was not mine :)

It would be overkill to force the use of { though.

Would it really? I certainly find code much more readable that way, and
it *is* required for various constructs IIRC.
do-while and switch require the braces
>
I use them everywhere, and haven't found any disadvantages to it.

--
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 24 '07 #18
Ben Voigt [C++ MVP] wrote:
"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:MP******** *************@m snews.microsoft .com...
<"Ignacio Machin \( .NET/ C# MVP \)" <machin TA laceupsolutions .com>>
wrote:
Interesting, it might be the case that I have not seen that much code
that
was not mine :)

It would be overkill to force the use of { though.
Would it really? I certainly find code much more readable that way, and
it *is* required for various constructs IIRC.

do-while and switch require the braces
---8<---
class App
{
static void Main()
{
do System.Console. WriteLine(); while(false);
}
}
--->8---

C# switch does require braces, although consider C/C++:

---8<---
int main(void)
{
switch (0);
return 0;
}
--->8---

(I in no way condone use of the above forms; though I'm ambivalent about
requiring {} on if-statement sub-blocks.)

-- Barry

--
http://barrkel.blogspot.com/
Dec 25 '07 #19
Lew
Arne Vajhøj wrote:
Lew wrote:
>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.

Fortran 90/95 ?

Not possible in 66 and 77.
(I know that C++ had such a problem before ANSI standardization )
I don't remember which version of FORTRAN scoped loop variables to the loop
for you, but it was in 1980. The reason was optimization - the loop variable
could be enregistered. The register would be colored by a different variable
immediately after loop exit.

--
Lew
Dec 26 '07 #20

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

Similar topics

5
17713
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
21948
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
2490
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
2425
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
2277
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
7087
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
9704
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
10558
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
10318
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
10302
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
10069
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...
1
7608
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
6844
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
5503
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...
3
2975
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.