# There is no need for reference counts nor smart pointers

This topic is 5430 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Quote:
Original post by iMalc
Quote:
 Original post by silvia_steven_2000this is what actuallay I meant to write. I did that by intention.when u chose a strange title u get your massage read by the maxnumber of people.
Ah yes, but many people will just assume you are a troll, and we all know not to feed the trolls.

Not all of us do. Take me, for example. I am an idiot(tm). I feed trolls.

Then again, sometimes the troll dies in the feeding, if my cooking is a bit off. So I suppose it's for the common good.

</random>

void simple_example_of_when_smart_pointers_would_be_nice ( void ){    int * this_should_have_been_a_smart_pointer = new int[100];    if(pie && foo)    {        if (lord_fnordicus_has_returned() && !mariners_win())        {            switch (ham_type)            {                case HAM_WITH_WHIPCREAM:                    std::cout << "EWWWW" << std::endl;                    return; //memory leak                default:                    cookie_monster += 12;                    break;            }            if (swiss_cheeze)            {                std::cout << "IT'S CHEESE YOU BUFFOON" << std::endl;                swiss_cheese = 1;                throw new pie(); //memory leak            }            else            {                if (elvis.alive()) return; //edit: I frogot to mark this as amemory leak on the first pass.            }        } else if (lord_poppeye_has_returned()) {            eat_hot_integer_array( this_should_have_been_a_smart_pointer );        }        swiss_cheese && (teh_yummeh = 1);        lolzorz || (we_are_safe = 1);    } else if (pie) {        throw std::execption(); //memory leak    } else if (foo) {        call_some_function_that_will_throw_without_us_realizing_it(); //memory leak    } else {        sexy_momma = 0;        return; //memory leak    }    delete[] this_should_have_been_a_smart_pointer; //cleanup}

##### Share on other sites
in fact, after developing program you theoretically can remove most refcounts. (in simpler programs you typically can remove all refcounts.)

How compiler can (theoretically can) remove refcounts: like any other known-at-compile-time thing.

Inline your
if(!refcount){allocate what you want;};
refcount++
and
refcount--;
if(!refcount){deallocate what you want;}

And then step along program flow and for every refcount++ and refcount-- operator check if refcount can be calculated at compile time, not at runtime .
It's quite hard but possible. And most of refcounts can be calculated at compile time.

BUT:
With complexs programs, some refcounts can't be calculated at compile time. Even in complex program it's probably small precent of 'em.

##### Share on other sites
Quote:
 Original post by silvia_steven_2000since the execution is done sequentially
I don't think anyone else has mentioned it yet, so I will. Execution does not have to be sequential. In a single-threaded program, you are correct, but reference counting can help out tremendously in multi-threaded programs. At least, that's my experience.

##### Share on other sites
A simple situation in which resource counting is useful in single-threaded situations is one where you need to keep memory use down (aka, to meet a certain "target" RAM use max, or a limit not breakable via virtual paging (embedded systems for example)).

Although you could simply load/decompress the resource for the program's lifetime, it does take up memory. Resource counting would let the application know when it could free that resource.

• 9
• 12
• 15
• 12
• 23