473,854 Members | 1,664 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

learn C++ or C#

If I haven't made substantial investment in either C++ or C#, which language
would the experts recommend I become well acquainted with?

Daniel
Jul 17 '08
151 4555
>Yes... the big difference is that with C#, errors manifest themselves
>as incorrect output or recognizable error messages rather than random
crashes or memory leaks.

At least you KNOW there's something wrong when you get a random crash ...
Assuming it does crash. C++ can not only suffer from the same "incorrect
output" as C# (and to a potentially much greater degree), but it can also
result in very serious (and potentially "random") problems that may not
manifest themselves until long after the problem has occurred (in ways that
C# just isn't susceptible to). And you may never even notice it. You may be
filling your DB with garbage, generating invalid reports, displaying
incorrect information for users, etc., but it may not come to anyone's
attention depending on the nature of the problem. And if (when) it does, it
may be incredibly difficult if not (practically) impossible to track it
down.
that incorrect output you get from your C# program is really much more
dangerous.

I think you and Larry and others here blowing the C# trumpet are vastly
over-stating the case about C++ being hard to use and hard to get right.
The fact is that bad programmers will write bad code in any language, and
if you want good code you have to employ good programmers. Once you have
those your problems will start to go away -- whatever language you are
using.

Of course, you need good management, too. Bad management and irrational
budgetary constraints can ruin a project just as easily as bad
programming.
Lol. You must have just graduated.
Jul 25 '08 #121
>I also have some serious pet peeves. Why didn't they permit local
>classes to be passed as template arguments. I once asked Stroustrup
about this at a conference but I wasn't satisfied with his terse
response.

I think the answer to that is "it looked too hard at the time" ... and
IIRC
that constraint is to be removed an some future version of the standard.
Too hard? Maybe too little time to work out the details at the time but it
had something to do with the binding rules according to Stroustrup. I don't
recall the limited details he provided off-hand.
>Even very experienced developers have to continuously bend their
minds out of shape and remain on guard years after learning the
language. This is not the hallmark of a successful language in
spite of C++'s "stellar" reputation and the millions who
continue struggling with it.

I think it's /only/ the very experienced developers (and those who aren't
but think they are -- which is more worrying) who "continuous ly bend their
minds out of shape" looking for new ways to use the expressive power of
the
language. These are the library builders, the Boost developers, the
Alexandrescus of the world ... most C++ programmers just /use/ their work
and are grateful for the way that it makes their lives easier and more
pleasant.
Most code in the real world is mediocre at best but usually much worse. It's
usually indicative of people who don't have their heads wrapped around the
language and how to program in general (opposed to pushing the limits of the
language). Even very experienced (skilled) programmers still have to
exercise a lot of caution and deal with complex issues not common in other
languages (not always but frequently enough). Most C++ programmers may be
"grateful" but the majority are causing serious problems for their
companies, their colleagues, and their users. Most of them function in a
state of ignorant bliss and management usually doesn't understand the scope
of the problem.
Jul 25 '08 #122
Peter Duniho wrote:
On Thu, 24 Jul 2008 22:43:52 -0700, Andre Kaufmann
<an************ *********@t-online.dewrote:
[...]
>I can include reference counting, but I can't use smart pointers.

Why not?
Not in that way as they are used in C++.

E.g. if I assign one smart pointer to another in C++ the reference is
incremented. In C# I would have to use a method for the assignment.
But I can't prevent one smart pointer accidentally be assigned to
another one in C#.
So I don't see that much value in smart pointers in C#, I then rather
would prefer the AddRef method directly and not using a smart pointer.

>I prefer GC memory handling, but for resources GC can't be used
effectively, because often resources have to be released directly.
I do a lot of multi threaded programming, there I permanently have the
need of some kind of reference counted resource handling.

For what it's worth, I've done a fair amount of multi-threaded
programming in C# without needing ref-counting.
For example in the following architecture:

A simple shared buffer holding memory and resources: [b]

[b] -Thread #1
[b] ---[ReceiverThread] - passes --[b] -Thread #2
[b] -Thread #3

The buffer is passed to multiple receivers which "do something" with the
buffer. After the last thread has finished it's operations it should
release the buffer and store it into a central pool for reusing and
should additionally release eventually used resources immediately.
How do I know if the last thread has finished ?
Not that I agree that ref-counting can't be done in C#, but I've found
that in many cases, what's really needed is a different way of looking
at the problem.
Yes, reference counting can surely be done in C#. But I don't see how I
could use reference counting with smart pointers in C# ---- outside of a
control block.
>>[...]
But there's nothing about C# that precludes using that
technique if you find yourself i

If you could add methods to value structures in C# I would agree.

I don't know what you mean here. What methods do you want to add? To
what structure? How would that help?
A structure is a value type in C# and therefore copied on assignment and
immediately "freed" if the control block is left.
If I could add an destructor and overload the assignment operator I
would have a possibility to implement smart pointers like in C++.

>But currently in C# you have to either use a temporary object in
combination with using or call the AddRef / Release functions by hand.

How is a smart pointer not a temporary object?
Yes, that's the point. I know how to handle it temporarily in a control
block, but when the smart pointer is added to a list of smart pointers
and removed from the list I have no possibility to "force" the automatic
release of the reference.
I have to remove the smart pointer from the list and then manually call
release. What's the point of a smart pointer in this case ? I could
rather use the reference counted object directly
[...]
Why not just create a smart-pointer-like class or struct that does that
with the "using" statement? Adds a ref when the object is created,
decrements the ref on disposal, zero ref-count causes disposal of the
wrapped object.
Maybe I'm just being obtuse, but I don't see what the big problem is.
I only have the feeling that just creating a new object to automatically
call methods of another object in the Dispose method is somewhat
overkill. The GC is fast, but why should I add pressure to it, by
permanently allocating temporary objects ?

I agree It's no big deal if the temporary object is created rarely or
held for a long time.

But additionally a smart pointer should behave like another ordinary
pointer, with additional functionality that is under my control.
Pete
Andre
Jul 25 '08 #123
Ben Voigt [C++ MVP] wrote:
>A simple example:

How do I ensure in C++ that I have successfully overridden a base
class virtual function and that the compiler throws an error if the
base class implementation has changed ?

You can't do that in any language I'm aware of, so it's a non-issue. Did
you mean "error if the signature of the function in the base class has
changed"?
Sorry yes - the signature - function declaration - not the definition or
code inside the function itself.
>
>Andre
Andre
Jul 25 '08 #124
On Fri, 25 Jul 2008 09:35:20 -0700, Andre Kaufmann
<an************ *********@t-online.dewrote:
Peter Duniho wrote:
>On Thu, 24 Jul 2008 22:43:52 -0700, Andre Kaufmann
<an*********** **********@t-online.dewrote:
[...]
>>I can include reference counting, but I can't use smart pointers.
Why not?

Not in that way as they are used in C++.

E.g. if I assign one smart pointer to another in C++ the reference is
incremented. In C# I would have to use a method for the assignment.
But I can't prevent one smart pointer accidentally be assigned to
another one in C#.
So I don't see that much value in smart pointers in C#, I then rather
would prefer the AddRef method directly and not using a smart pointer.
Ah, I see. So it's not that you can't do it. You just don't want to do
it the way you'd need to in C#.

You're right, assignment operator overload isn't available in C#. But
such an overloaded operator's really just another method. There's no
functional difference between simply implementing it as an explicit
method. In fact, some will argue that there's a good reason to not allow
overloading of the assignment operator and that making that sort of thing
explicit is a _good_ "don't blow your leg off" sort of thing. :)
For example in the following architecture:

A simple shared buffer holding memory and resources: [b]

[b] -Thread #1
[b] ---[ReceiverThread] - passes --[b] -Thread #2
[b] -Thread #3

The buffer is passed to multiple receivers which "do something" with the
buffer. After the last thread has finished it's operations it should
release the buffer and store it into a central pool for reusing and
should additionally release eventually used resources immediately.
How do I know if the last thread has finished ?
There are any number of approaches you can take. But if you really like
ref-counting, then the solution is to just ref-count the object. I don't
know what the phrase "eventually used resources" means, but if you're
pooling the object I don't see why you'd release its resources.
Conversely, if you're releasing its resources, then just get rid of the
object and be done with it.

In either case, you may still want _some_ form of ref-counting, either
managed by the pool manager itself, or as part of the object. But either
way, it's much ado about nothing.
[...]
A structure is a value type in C# and therefore copied on assignment and
immediately "freed" if the control block is left.
If I could add an destructor and overload the assignment operator I
would have a possibility to implement smart pointers like in C++.
IDisposable can replace the functionality of the destructor (in
conjunction with the "using" statement), and yes you'd have to have an
explicit method instead of an overloaded assignment operator. I don't
find those differences to be material.
>>But currently in C# you have to either use a temporary object in
combination with using or call the AddRef / Release functions by hand.
How is a smart pointer not a temporary object?

Yes, that's the point. I know how to handle it temporarily in a control
block, but when the smart pointer is added to a list of smart pointers
and removed from the list I have no possibility to "force" the automatic
release of the reference.
I have to remove the smart pointer from the list and then manually call
release. What's the point of a smart pointer in this case ? I could
rather use the reference counted object directly
When the smart pointer is not stack-allocated, don't you have to manually
delete the smart pointer in C++? What's the difference?
[...]
I only have the feeling that just creating a new object to automatically
call methods of another object in the Dispose method is somewhat
overkill.
Well, certainly there is the question of just how important smart pointers
are. Personally, I find them superfluous. You can still get ref-counting
bugs even if you're using smart pointers, if you use them incorrectly.
But inasmuch as there's some benefit to wrapping ref-counting in a
clearer, more automatic API, that benefit is available in C#, just using
slightly different syntax.
The GC is fast, but why should I add pressure to it, by permanently
allocating temporary objects ?
The phrase "permanentl y allocating temporary objects" doesn't make sense
to me. But regardless, the last thing you really need to worry about is
pressuring the GC. Assuming you're using a reference type for your smart
pointer (which may or may not be true anyway), the GC can handle the load
just fine.
I agree It's no big deal if the temporary object is created rarely or
held for a long time.
Au contraire. One of the GC's strengths is its ability to deal with large
numbers of frequently allocated, short-lived objects.
But additionally a smart pointer should behave like another ordinary
pointer, with additional functionality that is under my control.
To some extent, the real issue here is that there's a fundamental paradigm
shift that needs to be made. If you don't want to make that shift, that's
fine. But that's not an indictment of C#. The whole concept of "smart
pointer" is related to the needs of unmanaged code (and especially COM).
An application written to the strengths of C# and the .NET Framework will
most often never run into ref-counting issues at all, and if and when it
does happen, there are viable work-arounds.

Again: much ado about nothing.

Pete
Jul 25 '08 #125
Peter Duniho wrote:
You save a whole line of code. Wow!
Sorry, you just completely missing the point of RAII...

In C++ I can write a class Thingie that employs RAII for resource
management and then anybody can use it without caring about how it does
it's stuff.

In C# you can write the same Thingie but it's user is required to only
use it in the context of 'using' statement (or it will leak resources).
Jul 25 '08 #126
Peter Duniho wrote:
On Fri, 25 Jul 2008 09:35:20 -0700, Andre Kaufmann
<an************ *********@t-online.dewrote:

[...]
You're right, assignment operator overload isn't available in C#. But
such an overloaded operator's really just another method. There's no
functional difference between simply implementing it as an explicit
method. In fact, some will argue that there's a good reason to not
allow overloading of the assignment operator and that making that sort
of thing explicit is a _good_ "don't blow your leg off" sort of thing. :)
May be. But I have to protect the smart pointer somehow, so preventing
an assignment would be sufficient.
[...]
There are any number of approaches you can take. But if you really like
ref-counting, then the solution is to just ref-count the object. I
I don't like it that much, but I don't see a better solution for
efficient resource handling.
don't know what the phrase "eventually used resources" means, but if
you're pooling the object I don't see why you'd release its resources.
Conversely, if you're releasing its resources, then just get rid of the
object and be done with it.
To release the resources I too have to know when the last thread has
finished. A resource could be for example a large memory buffer. IMHO it
wouldn't be wise to frequently allocate - dispose large objects in C#.
[...]
When the smart pointer is not stack-allocated, don't you have to
manually delete the smart pointer in C++? What's the difference?
Smart pointers aren't allocated (normally) on the heap in C++.
Everywhere you are using them, they are either stack allocated or
embedded in an object. It's up to the compiler to generate the
appropriate code to call the destructor.
[...]

The phrase "permanentl y allocating temporary objects" doesn't make sense
to me. But regardless, the last thing you really need to worry about is
pressuring the GC. Assuming you're using a reference type for your
smart pointer (which may or may not be true anyway), the GC can handle
the load just fine.
Yes, GC is quite fast - under Windows. I think under Mono it's somewhat
different. But anyways I just want to call 2 methods of another object,
it's IMHO just somewhat overkill to create a temporary object just for
that.

IIRC lock(..) in C# simply calls 2 methods. Would be fine if I could use
the same functionality for other objects and methods.

>
To some extent, the real issue here is that there's a fundamental
paradigm shift that needs to be made. If you don't want to make that
I'm fine so far with C# - it has many strengths and I like it. But I
only miss the control C++ offered me - sometimes.
shift, that's fine. But that's not an indictment of C#. The whole
concept of "smart pointer" is related to the needs of unmanaged code
(and especially COM). An application written to the strengths of C# and
the .NET Framework will most often never run into ref-counting issues at
all, and if and when it does happen, there are viable work-arounds.
The D language for example combines both - garbage collection and RAII.
Additionally I can use C++/CLI which also supports RAII for managed
programming.
But the downside is, C++/CLI is damn slow in compilation compared to C#,
because it's a full fledged C++ compiler. And regarding managed
application development it's IMHO only a second class citizen.

So I prefer C# anyways for this task.
Again: much ado about nothing.
Yes, it's not a vital feature. But the discussion was about RAII, which
isn't used for smart pointers only.
It would only be a feature, that would make C# more attractive to C++
developers ;-)
Pete
Andre
Jul 25 '08 #127
On Jul 25, 5:45 am, Daniel James <wastebas...@no spam.aaisp.orgw rote:
In article <news:OP******* *******@TK2MSFT NGP04.phx.gbl>, Larry Smith wrote:
I also have some serious pet peeves. Why didn't they permit local
classes to be passed as template arguments. I once askedStroustrup
about this at a conference but I wasn't satisfied with his terse
response.

I think the answer to that is "it looked too hard at the time" ... and IIRC
that constraint is to be removed an some future version of the standard.
C++0x will support local types as template arguments. The implementors
who had problems have found a way.
Jul 25 '08 #128
In article news:<OA******* *******@TK2MSFT NGP04.phx.gbl>, Larry Smith wrote:
Assuming it does crash. C++ can not only suffer from the same
"incorrect output" as C# ... but it can also ..
Certainly -- Point taken -- I didn't mean to suggest that it couldn't.

I was just pointing out that while bad C# code may give you a "nice warm
feeling" by not crashing you should not be lulled into a false sense of
security that it is actually doing the right thing.

C# code needs just as much testing for correctness (and just as much
careful design work to achieve correctness in the first place) as code in
any other language -- it would be wrong to suggest that it did not, or that
doing these things for C# was significantly quicker, cheaper, or easier
than doing them for C++.
Lol. You must have just graduated.
Flattery will get you nowhere -- it took me years to develop this much
cynicism!

Cheers,
Daniel.
Jul 26 '08 #129
C# code needs just as much testing for correctness (and just as much
careful design work to achieve correctness in the first place) as code in
any other language -- it would be wrong to suggest that it did not, or
that
doing these things for C# was significantly quicker, cheaper, or easier
than doing them for C++.
Far from it. While nobody would dispute that program correctness is
demanding in any language, C++ is significantly more demanding to get right
from a purely mechanical perpsective alone. It's also filled with more
trip-wires and gotchas than any other mainstream language. It's easier to
achieve programa correctness when you're strolling down a paved sidewalk
instead of hiking through the jungle.
>Lol. You must have just graduated.

Flattery will get you nowhere -- it took me years to develop this much
cynicism!
It's a strange brand of cynicism you're practicing if you really believe
that "good programmers" and "good management" exist in the real world (in
sufficient numbers to make an appreciable difference). Those who practice
the more conservative brand would simply tell you this is reality. The
panacea you're waiting for may exist somewhere, but it won't save the
majority of us.
Jul 26 '08 #130

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

Similar topics

17
3439
by: Rob | last post by:
i know javascript, vbscript, asp css and alot more and im only 14 i was wondering which is easier to learn php or cgi. any help?
42
3737
by: Bicho Verde | last post by:
I have now free time and money to do what I want :-) I have some basic skills in programming (C, Pascal, Macromedia Actionscript) but don't know exactly what to do in the world of programming. And also I don't know exactly why would I learn Python rather than C#, C++ or Perl. Basicaly I don't know where to start, if there is much to do or if it is has it seems and there is software to everything nowadays and so doesn't make sense to spend...
55
46035
by: Elijah | last post by:
I have read many of the topics on learning C++ or Java first. It seems like everyone says something different. I would like to know if I should learn C++ or Java. First a little about myself. I know PHP, BASIC, and of course HTML. I'll be 15 years old in September. I am interested in programming GUI applications. I am also interested in programming games. I know that I should learn C++ to program games, but would learning Java make the...
30
9444
by: Rhino | last post by:
I am giving some thought to applying for some jobs that want people with Java and C++ experience. I have been writing Java for several years and am fluent enough that I don't have to get help with Java very often. I have no real C++ experience and not much C experience for that matter. However, the core Java statements are "borrowed" from C and C++ has often been called "C with classes". It seems to me that it shouldn't take very long to...
8
4053
by: Hermawih | last post by:
Hello , I want your opinion about this . In order to say it clearly , I think I have to describe it in long sentences . I could consider myself as Intermediate/Advance Access Developer ; Intermediate/Advanced Database designer . Because of the requirements , I must create Web Application . Access Pages is not suitable for that so I think about learning VB Net / ASP Net . I am
21
2874
by: TAM | last post by:
Hi, I read that ASP.NET uses VB.NET instead of VBScript. I also read that ASP.NET is a subset of VB.NET. So if I learn VB.NET first then do I have the knowledge for programming ASP.NET applications or do I need to learn both VB.NET and ASP.NET. Thank you. TAM
85
4189
by: abhi | last post by:
hi everybody am new to this group and help me to learn C
31
2642
by: anand devarajan | last post by:
hi friends, im anand im just a beginner in c learning for the past two weeksnow i can write simple prgs can anyone help me to get well known to c lang so that i should able to write even tough prgs in c
34
3169
by: pandit | last post by:
hai all, i want to become a good programmer. one of my friends ( named "arnuld", he posts here infrequently), taught me Lisp. so i am not a programming beginner. i have heard these 2 points. i want to know how : 1. C gives you a strong base of Procedural style of programming which forms the basis of learning other paradigms e.g OOP
65
5319
by: Chris Carlen | last post by:
Hi: From what I've read of OOP, I don't get it. I have also found some articles profoundly critical of OOP. I tend to relate to these articles. However, those articles were no more objective than the descriptions of OOP I've read in making a case. Ie., what objective data/studies/research indicates that a particular problem can be solved more quickly by the programmer, or that the solution is more efficient in execution time/memory...
0
9901
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
11031
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
10684
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
10763
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
9517
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
7918
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
5744
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...
0
5942
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4563
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

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.