Click here to Skip to main content
12,894,249 members (80,302 online)
Click here to Skip to main content

Tagged as


26 bookmarked
Posted 29 Sep 2012

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

, 25 Oct 2012 CPOL
reduce bugs and memory leaks by using new(local)
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include "Scope.h"   

TScope global; // this one is threadsafe and deallocates all associated objects on program exit no matter 
               // what exception or error in program happens

struct A : Scope::Obj{  // to support autocleanup functionality just derive from Scope::Obj
       A(){ printf("\n  A %x",this); }  // so Scope knows which destructor to call
      ~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"); //associate with global scope
    A* a=new(global) A();  // this will get destructor called on progam exit

void main() {

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


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...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170424.1 | Last Updated 25 Oct 2012
Article Copyright 2012 by Ladislav Nevery
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid