Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: Memory Management scares me

Reply
Thread Tools

Re: Memory Management scares me

 
 
hondacivic@whoever.com
Guest
Posts: n/a
 
      04-01-2004
Thore Karlsen <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>. ..

> Memory management is typically very simple in C++. You just need to know
> how to do it right, but once you see the right way to do it you'll
> understand how easy it is.


Although I don't disagree, and have written tons of clean software in
C++ in the past, I now write almost exclusively in Java, because in
Java it is even easier.

> The trick is never to use raw pointers.
> Always use smart pointers or classes like vector.


I think that would make C++ actually slower than Java.

> There is very rarely a need for raw pointers. I almost never need them.


But other people in the development team will, and the project risks
getting canned by the company.

> In the product I'm responsible for I have had a couple of memory leaks.
> Almost all were caused by buggy Microsoft components/APIs that I had to
> work around, and one was caused by another third-party component.


I remember BoundsChecker, oh no, it's all coming back now.

> I haven't had a single memory leak caused by my code in any of the
> applications I'm responsible for, not even in beta code. If you avoid
> raw pointers, which you can in almost all circumstances, you just never
> have to worry about memory leaks.


Perhaps. I can write clean code too. But I think these days, one
needs too many excuses to stick with C++. Stroustrup is way passee,
although I used to, and still do to some degree, admire his thinking.
His Java dismissal was too much of a turn off. I bet me against him
writing an app from scratch, he wouldn't have a hope in hell
competing.

> I also use smart pointers and wrapper classes for other resources, so
> there's no need to manually clean up those either. In fact, it is harder
> to do this correctly in Java for non-memory resources since Java doesn't
> have deterministic destruction.


And then you spend a lot of time inventing a method of organizing,
hard for other people to get on board with. Each 'good' software
architect tends to build a proprietary foundation in C++. Bad for
companies and teams. In java you write libraries too, but you spend
less cycles at the *really* low level.

> Even initialization and cleanup of libraries is much easier in C++.
> Example using a scope guard:
>
> JPEG_CORE_PROPERTIES jprops;
> if (ijlInit(&jprops) != IJL_OK)
> return E_FAIL;
>
> ON_BLOCK_EXIT(ijlFree, &jprops);
>
> No matter what happens below this, ijlFree() will always be called. You
> can return anywhere, throw an exception, whatever -- jprops will be
> freed automatically.


With Java's generational garbage collector, you can expect that as
soon as an object or group of objects become isolated, they get
cleaned up.
As for closing resource and files, there's the finally thing, not
object's finalize, which has been taboed, but the try{}finally{}.

> C++ has an undeserved reputation for being tricky in this area, when
> it's really one of the easiest languages to do the right thing in.
> Sadly, judging by the bugs I see and the code I've looked through, few
> people seem to know how to use C++ properly.


Which is why C++ performs poorly in teams.

Check out these GC related pages:
http://www-106.ibm.com/developerwork...-jtp01274.html
http://java.sun.com/docs/hotspot/gc1.4.2/faq.html
 
Reply With Quote
 
 
 
 
Steven T. Hatton
Guest
Posts: n/a
 
      04-01-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

Let me guess. You work here:

http://www.can.ibm.com/torontolab/


> Although I don't disagree, and have written tons of clean software in
> C++ in the past, I now write almost exclusively in Java, because in
> Java it is even easier.


I suspect GC will be a fairly standard /option/ for C++ in the not too
distant future. So this may turn out to be moot.

> I think that would make C++ actually slower than Java.


I'd like to see the numbers. And the source used to produce them.


> But other people in the development team will, and the project risks
> getting canned by the company.


There are coding standards, and there may even be ways to lint code for such
things. I'v proposed creating a 'safe' mode subset of C++ which would
confine code within a carefully chosen syntactic limit. Other's haven't
been too kind to the idea, but don't count it out.

>> In the product I'm responsible for I have had a couple of memory leaks.
>> Almost all were caused by buggy Microsoft components/APIs that I had to
>> work around, and one was caused by another third-party component.

>
> I remember BoundsChecker, oh no, it's all coming back now.


I remember Microsoft...It's all coming back to me now...

> Perhaps. I can write clean code too. But I think these days, one
> needs too many excuses to stick with C++. Stroustrup is way passee,
> although I used to, and still do to some degree, admire his thinking.
> His Java dismissal was too much of a turn off. I bet me against him
> writing an app from scratch, he wouldn't have a hope in hell
> competing.


Do you start from scratch with the core language as defined in the Java
Language Specification, or are you throwing in all the other stuff such as
Swing, javax.net.*, etc.? Can he then use freely available counterparts
from open source? I'm not going to place bets. A language designer may
not be the fastest coder. I'll bet Stroustrup could show you up in terms
of core contributions to computer languages.

For me, I am much faster with Java. But I'm still trying to find my feet in
C++. There is a lack of Linux tools for C++ comperable to JBuilder, and I
also believe there are some infrastructure issues involved with C++.


> And then you spend a lot of time inventing a method of organizing,
> hard for other people to get on board with. Each 'good' software
> architect tends to build a proprietary foundation in C++. Bad for
> companies and teams. In java you write libraries too, but you spend
> less cycles at the *really* low level.


The C++ community has been slow to address the need for common resources,
but they're comming around. For example http://www.boost.org

> With Java's generational garbage collector, you can expect that as
> soon as an object or group of objects become isolated, they get
> cleaned up.
> As for closing resource and files, there's the finally thing, not
> object's finalize, which has been taboed, but the try{}finally{}.


Got any benchmarks we can contest, uh, I mean examine?


> Which is why C++ performs poorly in teams.


Not in all cases. http://www.kde.org

One thing to consider with C++ is that there are a _lot_ of experienced
people who /can/ contribute to the kinds of infrastructure that gives Java
some advantages. I hope C# is a wakeup call to them. Microsoft has a new
baby, all their own. Remember OS/2 and the original plans for NT?

This is actually what inspired me to start this thread. Friends of your's?

http://xml.apache.org/xerces-c/apiDo...ssXMemory.html



--
p->m == (*p).m == p[0].m
http://www.kdevelop.org
http://www.suse.com
http://www.mozilla.org
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: more food scares Old Codger Computer Support 1 02-15-2008 09:28 PM
Canon S1 scares the hell out of me nesredep egrob Digital Photography 0 02-20-2005 08:59 AM
Perl scares me ... Richard Gration Perl Misc 5 04-28-2004 08:00 PM
CatOS web management or CiscoView management ? Martin Bilgrav Cisco 1 12-20-2003 01:49 PM
perl memory management - does @array = () free the memory? Matt Oefinger Perl Misc 0 06-25-2003 09:11 PM



Advertisments