Click here to Skip to main content
13,353,322 members (58,525 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


26 bookmarked
Posted 29 Sep 2012

new(local) for safer and simpler code with no need for smart-pointer template wrappers

, 3 Oct 2012
Rate this:
Please Sign up or sign in to vote.
reduce bugs and memory leaks by using new(local)
This is an old version of the currently published article.


Pointers always envied automatic cleanup that is in c++ provided only for big fat slow Static Object or their arrays stored by value. In this article we will introduce concept of scopes also for dynamic objects and scoped version of new/malloc to take advantage of those scopes. Ie associated objects/memory will get destroyed with the scope. Plus your code will now be smaller and simpler. I got this idea about pointers deserving automatic cleanup love too in my other article about new && operator
struct Object : Scope::Obj {

Scope global;
// entering scope
void proc() { 
    Scope local;
    char*  text = strdup(local,"text"): // malloc based memory have automatic cleanup too
    Object* obj = new(global) Object(); // obj will get destructor called on program exit 

    Object* array[1000]={0};            //sorting pointers is fast compared to array of objects
    for(int i=0;i<1000;i++) {
        array[i] = new(local) Object(); // creates object associated with "local" scope 
// we left scope so "Scope local" and all associated object /memory 
// gets destructors called/freed in proper order    

Why manual cleanup is source of bugs.     

Automatic cleanup provided for static objects saves time lines of code and a lot and I mean really lot of bugs. Bugs by forgetting to match all allocations with deallocations. Bugs by memory being leaked when exception or error handler exits something somewhere prematurely and not all control paths contain correct number of release statements.   
Bugs by releasing objects twice or more and corrupting memory by confused condition statements.

Performance point of view    

So we definitely want automatic cleanup that is provided for static objects. But pretty much any program serious about performance will store objects by pointers and not by value because price for reallocation insert or sort is horrendous as you can see in benchmark here

So can't we have both? Performance and efficiency of pointers and safety and simplicity of static objects by automatic cleanup when leaving scope? Yes we can if we implement concept of scopes also for dynamic objects.

Simpler alternative to smart pointers   

Big advantage of scopes is that you don't need kinda heavy dependency on stl/boost just for simple thing like pointer. Code simplicity and clarity keeps bugs at bay. Why ?

in the bigger struct A sample code bellow it's clear what is going on and you are not limited in array storage either. 

               // by keeping simple pointers it's always obvious what is going on
               // and how efficiently.
 ptr1 =  ptr2; // just pointers are assigned ie MOVE semantics
*ptr1 = *ptr2; // objects are copied ie COPY semantics
               // ie you decide  when you copy or move by clear syntax.

With smart pointers the fact when they actually copy or move is one big jungle.

Most of them simply don't work in containers. some require c++11 compiller or big template libs resulting to big executables and all make code unusable by others since libs/dlls  with stl/boost are incompatible on binary level not having any concept of stable interface .  

auto_ptr is very bad choice. It's pointer with copy and with move semantics and ownership (=auto-delete) can't be stored in containers

scoped_ptr is a auto_ptr without copy and without move semanticscan be stored in containers.and requires boost It adds a level of safety. memory created will exist only for that scope and no more, thus you get compile-time protection against attempting to move it or transfer it.  

 is a auto_ptr without copy but with  move semantics.can be stored in containers requires c++ 11 compillerOwnership is movable,  

shared_ptr. requires c++ 11 compillerIf you want to create something and share that pointer and cleanup when all referencers are gone,  

weak_ptr ,intrusive_ptr ,... whatever new ones they will attempt to create  in future just making the whole mess and confusion even deeper
With smart pointers you need to wrap every pointer reference ie change all the code to alien lenghty syntax always worying about state machine you just created. Take for example this guy having trouble sorting simple array of pointers which he normally wouldnt have but now obviously having noidea what state machine he actually created he is sitting in forums getting responses like   

"...The IS specifies that a predicate should not assume that the dereferenced iterator yields a non-const  object. Arguments should therefore be taken by reference-to-const or by value.  The IS also specifies that

algorithms that take function objects as arguments are permitted to copy those function objects. So in most cases, the overloaded function call operator would be a const member function ..."                                                                                                                                            

It is unbelivable what complexity we vere able introduce to ehm... "simplify our life" 

The real solution is to have smart pointers build in c++ language and not create zillion of weird templates supporting this and not that. For example let Object *~ be declaration of smart pointer. Will have destructor called going out of scope and all of the copy move is already part of low level pointer arithmetics. no zillion of template wrapers/libs/operators. period. I am right now attempting to implement it in fantastic CLang compiller as an interesting experiment. Thank's god for whole open source movement.

But until then with scopes you just derive your objects from Scope::Obj in one line and that's all. Keeping simple easy to debug code and libs/dlls usable by others. Just insert scope to your class and use scoped de/allocators. All your data will be properly deallocated exactly like with smart pointers even when constructor fails in the middle during exception etc. 

struct A {
    Scope local; 
    B* ptr1;
    B* ptr2;
    A() {
        ptr1=new(local) B();
        ptr2=new(local) B();// will not allocate and will leave prematurely due to memory 
                            // exhaustion exception. But no memory leak will happen since scope
                            // deallocates ptr1 on scope destruction as part of object
                            // destruction that follows this constructor exception

So if you are like me preferring things small clear simple and fast. If you don't  like obfuscation for simple things and like to know by fast look at source what is going on.
Let's try simpler but I hope equally functional alternative.  

Let's Scopes for dynamic heap objects be born  

    So how does the automatic cleanup of static objects actually work? Well. It works by simply adding pointer of every static object within scope to internal invisible linked list and when leaving calling destructors for each of them in reverse order.

Can we make something similar for dynamic objects?
Why not. We can have array of void pointers and store pointers to various object types in one single array.
The only issue are calling proper object specific destructors. Unfortunately as far as I know c++ doesnt allow to get destructor adresses. So the only sollution I come up so far is to use virtual destructor in all stored objects so polymorphysm selects proper one for us.  

Implementation of Scope.h                                                   

This code snippet bellow is kept simple with compressed formating just for purpose of article so it is clear what is going on. Complete implementation along with scoped versions of new delete strdup malloc calloc free and threadsafe version of scope called TScope (usefull for using for example global scope from multiple threads etc) is in Scope.h that is attached in zip file with Example.cpp on top of the article. I did not attached project files since most of you will not be able to open vs2012 anyway. 

struct Scope { // This is Just simple linked list
       struct Obj { virtual ~Obj() {} };
       Scope*    prev;  Obj* ptr;
       Scope() : prev(0) ,   ptr(0) {}	
      ~Scope() { ptr->~Obj(); free(ptr); delete prev; } // deleting all childs in reverse order on exit

inline void * __cdecl operator new(unsigned int size,Scope& scope)
    void *ptr = (void *)malloc(size); // we add all new objects to this linked list
    if(scope.ptr) { Scope* prev=0; prev=(Scope*)calloc(sizeof(Scope),1); *prev=scope;
    scope.prev=prev; }

Limitations of this solution 

Unfortunatelly c++ doesn't support getting address of destructor. So as slight inconvenience.
To support automatic cleanup your objects need to be derived from Scope::Obj or to have virtual destructor as first virtual method.
That being said adding one derive statement in class declaration is one changed line of code ie less work and changes than going about replacing every ocurence of pointer of that class with long alien syntax as you need with smart-pointers .  

Example code  

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include "Scope.h"   

Scope global; // Deallocates/Destroys all associated objects on program exit no matter 
              // what exception or error in program happens

struct A : Scope:Obj {// As for supported objects. They must be derived
              // from Scope::Obj or have virtual destructor as the first vitual method 
              // so it is first in vtable and correct call is constructed on cleanup
              // In a sense we are keeping pointers to destructors as known this way
       A(){ printf("\n  A %x",this); }
      ~A(){ printf("\n ~A %x",this); } 

void Test() {
    Scope local; // all objects associated with scope will be deallocated by leaving procedure
    A* array[3]={0};
    for(int i=0;i<3;i++) {
        array[i]=new(local) A();
    char* text=strdup(global,"this will get deallocated on program exit");
    A* a=new(global) A();  // this will get destructor called on progam exit

void main() {


   A 689718   A 68b110   A 68b198   A 68b220 ~A 68b198 ~A 68b110 ~A 689718 ~A 68b220

Points of Interest 

The fact that derive from Scope::Obj or with virtual destructor as first virtual method kinda is slight inconvenience considering what you will gain.  

Also if you want to autodeallocate memory from malloc/calloc/strdup just use scoped versions from scope.h in zip file. Off coarse for explicit dealocation you must use  scoped versions of free/delete too so they are removed from scope and no another attempt is made to free them when leaving scope 


 Where this leads and future work

As I said before right now I am playing with the clang c++ compiller sources and implementing smart pointers as build in to c++. Feature that should had been there long time ago (calling smartpounter destructor when not null and going out of scope) so low level os c/c++ developers can produce smaller and safer code too without big template monsters like boost. I am open to interesting new ideas or suggestions 

I have no idea how to mark them yet. But so far I am testing with variable*~ syntax


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Ladislav Nevery
Software Developer (Senior)
Slovakia Slovakia
Past Projects:
[]Mobile network software: HLR-Inovation for (Corba)
Medical software: CorRea module for CT scanner
[]Computer Games:XboxLive/net code for Conan, Knights of the temple II, GeneTroopers, CivilWar, Soldier of fortune II
[]Computer Games:XboxLive/net code for Elveon game based on Unreal Engine 3
ESET Reasearch.
Looking for job

You may also be interested in...


Comments and Discussions

Discussions on this specific version of this article. Add your comments on how to improve this article here. These comments will not be visible on the final published version of this article.
QuestionDidn't know there is a new operator in C Pin
Stan Mihai18-Jul-13 5:41
memberStan Mihai18-Jul-13 5:41 
QuestionWhy not std::unique_ptr<T>? Pin
C++ Pirate Programmer18-Oct-12 6:46
memberC++ Pirate Programmer18-Oct-12 6:46 
AnswerRe: Why not std::unique_ptr? Pin
Ladislav Nevery18-Oct-12 17:02
memberLadislav Nevery18-Oct-12 17:02 
QuestionThanks Pin
YannAchard18-Oct-12 2:29
memberYannAchard18-Oct-12 2:29 
AnswerRe: Thanks Pin
Ladislav Nevery18-Oct-12 14:00
memberLadislav Nevery18-Oct-12 14:00 
GeneralMy vote of 2 Pin
SeattleC++15-Oct-12 10:53
memberSeattleC++15-Oct-12 10:53 
GeneralRe: My vote of 2 Pin
Ladislav Nevery16-Oct-12 10:56
memberLadislav Nevery16-Oct-12 10:56 
GeneralRe: My vote of 2 Pin
SeattleC++16-Oct-12 12:24
memberSeattleC++16-Oct-12 12:24 
GeneralRe: My vote of 2 Pin
Ladislav Nevery17-Oct-12 6:44
memberLadislav Nevery17-Oct-12 6:44 
GeneralRe: My vote of 2 Pin
SeattleC++17-Oct-12 11:29
memberSeattleC++17-Oct-12 11:29 
GeneralRe: My vote of 2 Pin
Ladislav Nevery18-Oct-12 0:06
memberLadislav Nevery18-Oct-12 0:06 
QuestionWhat's about an exception handling? Pin
konik1015-Oct-12 7:05
memberkonik1015-Oct-12 7:05 
AnswerRe: What's about an exception handling? Pin
Ladislav Nevery16-Oct-12 2:47
memberLadislav Nevery16-Oct-12 2:47 
GeneralRe: What's about an exception handling? Pin
konik1023-Oct-12 22:24
memberkonik1023-Oct-12 22:24 
GeneralRe: What's about an exception handling? Pin
Ladislav Nevery24-Oct-12 23:58
memberLadislav Nevery24-Oct-12 23:58 
GeneralRe: What's about an exception handling? Pin
konik1025-Oct-12 2:20
memberkonik1025-Oct-12 2:20 
GeneralRe: What's about an exception handling? Pin
Ladislav Nevery25-Oct-12 5:41
memberLadislav Nevery25-Oct-12 5:41 
GeneralRe: What's about an exception handling? Pin
konik1025-Oct-12 6:43
memberkonik1025-Oct-12 6:43 
GeneralRe: What's about an exception handling? Pin
Ladislav Nevery25-Oct-12 13:50
memberLadislav Nevery25-Oct-12 13:50 
QuestionException / Thread safety? Pin
Petro Protsyk8-Oct-12 2:20
memberPetro Protsyk8-Oct-12 2:20 
AnswerRe: Exception / Thread safety? Pin
Ladislav Nevery8-Oct-12 8:54
memberLadislav Nevery8-Oct-12 8:54 
GeneralMy vote of 2 Pin
Patrick Niedzielski7-Oct-12 6:38
memberPatrick Niedzielski7-Oct-12 6:38 
GeneralRe: My vote of 2 Pin
Ladislav Nevery7-Oct-12 11:26
memberLadislav Nevery7-Oct-12 11:26 
GeneralRe: My vote of 2 Pin
Patrick Niedzielski7-Oct-12 11:30
memberPatrick Niedzielski7-Oct-12 11:30 
GeneralMy vote of 5 Pin
nv33-Oct-12 23:03
membernv33-Oct-12 23:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180111.1 | Last Updated 3 Oct 2012
Article Copyright 2012 by Ladislav Nevery
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid