UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

#26 2009-06-17 12:01 AM

jfriesne
Administrator
From: California
Registered: 2005-07-06
Posts: 348
Website

Re: STL for Tree

Offline

#27 2009-06-17 12:04 AM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,847
Website

Re: STL for Tree

*sigh*  So, of course I had to go and demonstrate a bug in the tiny bit of C code I
typed, as if just to prove your point for you... ;-)

if (ref->cnt == 0) { close (ref->sock); free (ref); }
    return (ref->cnt);

Yeah, obviously you don't want to reference "ref" after freeing it... ;-/  Pretend I had a
"return (0);" inside the curly-braces, or something... ;-)

Offline

#28 2009-06-17 12:44 AM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,847
Website

Re: STL for Tree

Offline

#29 2009-06-17 12:51 AM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: STL for Tree

I don't have as much programming experience as either Rob or Jeremy, but
I started with C++ and ended up at C.

What I detest in both "solutions" is that they both make simple code more
complicated than needed.

Making code more complicated is never a good way of making code more
secure. And making things more complicated than needed is what C++ code
seems to do all the time.

In this example, a socket never comes alone, there's almost always some
other associated data. Then it's really easy to simplify resource handling
because the lifetime of both some state structure and the socket is the
same. Having a good code design with consistent rules is the right solution,
not some lame "let's wrap it and then forget about it". Bah.

Offline

#30 2009-06-17 01:36 AM

jfriesne
Administrator
From: California
Registered: 2005-07-06
Posts: 348
Website

Re: STL for Tree

Offline

#31 2009-06-17 01:39 AM

jfriesne
Administrator
From: California
Registered: 2005-07-06
Posts: 348
Website

Re: STL for Tree

Offline

#32 2009-06-17 08:46 AM

yurec
Member
From: Singapore
Registered: 2006-11-16
Posts: 134

Re: STL for Tree

Impressive discussion, guys.
However that is c++ thread.

Regarding oop and not oop. After one of my early program : Java script chess, i found that funcional coding is a big mess in comparance to oop mindset.

Progress of languages which are easy to learn, fast to code and produce many times cheaper applications that satisfy user needs prooves that OOP is a really good idea.

But there are still will be cars which are hand made (exlusive, of highest quality and incredibly expensive).

Offline

#33 2009-06-17 10:47 AM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: STL for Tree

Offline

#34 2009-06-17 01:33 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,847
Website

Re: STL for Tree

int some_func (...)
{
    int ret = 0;

    /* ... */

    if (some_bad_thing)
        ret = ERR_SOME_BAD_THING;

    if (!ret) {
        /* ... do more real work ... */
        if (some_other_bad_thing)
            ret = ERR_SOME_OTHER_BAD_THING;
    }

    if (!ret) {
        /* ... do more real work ... */
    }
    /* ... */

    /* ... now do whatever cleanup is needed ... */
    return (ret);
}

Offline

#35 2009-06-17 02:19 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: STL for Tree

Offline

#36 2009-06-17 05:52 PM

jfriesne
Administrator
From: California
Registered: 2005-07-06
Posts: 348
Website

Re: STL for Tree

Offline

#37 2009-06-17 08:42 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,847
Website

Re: STL for Tree

Offline

#38 2009-06-17 10:34 PM

jfriesne
Administrator
From: California
Registered: 2005-07-06
Posts: 348
Website

Re: STL for Tree

Offline

#39 2009-06-17 11:09 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,847
Website

Re: STL for Tree

Offline

#40 2009-06-17 11:27 PM

jfriesne
Administrator
From: California
Registered: 2005-07-06
Posts: 348
Website

Re: STL for Tree

Offline

#41 2009-06-18 11:38 AM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: STL for Tree

Offline

#42 2009-06-18 02:14 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,847
Website

Re: STL for Tree

Offline

#43 2009-06-18 06:51 PM

jfriesne
Administrator
From: California
Registered: 2005-07-06
Posts: 348
Website

Re: STL for Tree

Offline

#44 2009-06-18 09:03 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,847
Website

Re: STL for Tree

Offline

#45 2009-06-19 12:12 AM

Uzume
Administrator
Registered: 2002-08-30
Posts: 186

Re: STL for Tree

OK where is the Objective-C section/discussion? Are there no Mac programmers out there (I know that is not the only use but that is the common usage today).

This is great discussion by the way...though I am not sure it should be attached to this ancient thread--but who cares?

Offline

#46 2009-06-19 04:18 AM

jfriesne
Administrator
From: California
Registered: 2005-07-06
Posts: 348
Website

Re: STL for Tree

Offline

#47 2009-06-19 10:44 AM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: STL for Tree

Okay, although I don't care much about C++ forcing people to cast, I'll
bother to reply anyway...

The reason it's totally useless and hence only pollutes the code is because
the coder writing that code is very aware that it's a void*. And a void* isn't
typeless, it's still a pointer, so most of the type info is preserved.

The main issue is this: Dealing with void* has two sides, the one where putting
something of a certain type in it, and the other side which extracts it again. A
bug only happens when there is a miscommunication and both sides use two
incompatible types. Both code snippets are separate, and in both it's very clear
what type they expect. That is why forcing a cast is totally useless, because it
doesn't prevent the miscommunication from happening, looking at the code
separately it's totally clear what type it is and should be cast to. It results in
code like:

type_a x = (type_a)y;

Useless, isn't it? If y's type changes, there's still a bug. If x's type changes
the programmer also changed the type cast, or does so after the compiler
whined. Either way, a bug goes unnoticed.

Even funnier, casting things makes things worse. As seen above it doesn't
solve a damn thing, but what it does do is add a potential for missing a class
of bugs: When y's type is changed from void* to type_b, you won't hear the
compiler complaining anymore.

Forced casting from void* is like a railing besides a mountain track that
collapes when you try to actually lean on it.

Offline

#48 2009-06-19 02:57 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,847
Website

Re: STL for Tree

Offline

#49 2009-06-19 04:24 PM

jfriesne
Administrator
From: California
Registered: 2005-07-06
Posts: 348
Website

Re: STL for Tree

Offline

#50 2009-06-19 05:07 PM

jfriesne
Administrator
From: California
Registered: 2005-07-06
Posts: 348
Website

Re: STL for Tree

(void) arg;

Offline

Board footer

Powered by FluxBB