You are here:

C++/C++ syntax question


Hi Ralph. I have a generally question. Logically, whats the difference between these two statements and whats the difference in their outcome/output.

Culs::logCreator logdata (a, b);


Creator::logdata = new Culs::logCreator(a, b);  

The first statement declares and defines an object of type Culs::logCreator called logdata initialised by passing the items named a and b (or references to them) to an appropriate constructor. If no such constructor exists or there is more than one equally plausible candidate then the program will be in error. An exception may be thrown by the constructor.

The object created is either a local automatic object allocated on the stack frame of a function call or a global object allocated in the program static data area, depending on where it appears in the code. Where ever the object is allocated it is allocated in total, i.e. its whole value is allocated not just a reference to it with the object allocated on the heap (i.e. dynamic memory area).

In either case the compiler ensures that the object is allocated correctly, and ensures the object is destroyed when it goes out of scope, or when closing down the program if the object is global. That is, it is the compiler that generates the code to arrange for these things to happen when the code is executed.

As an aside I should note that the compiler can also arrange for them not to happen if it can determine that not doing them causes no observable difference to the execution of the program. This is less likely for objects that have constructors and destructors as these tend to perform operations which might affect the behaviour of the program.

The second statement also creates an object of type Culs::logCreator, initialised as for the first example. In this case the object is allocated at runtime (i.e. dynamically) on the heap and the new operator returns a reference to this object in the form of a pointer to Culs::logCreator (i.e. Culs::logCreator *). This pointer is assigned to some named item called Creator::logdata, which is either a member of some class Creator called logdata or some global data member of some namespace Creator. Note that it is the programmer's responsibility to ensure that dynamically allocated objects are explicitly deleted (releasing their resources) when they are no longer required by passing a pointer to the object to the delete operator exactly once:

   delete Creator::logdata;

From this it can be seen that if Creator::logdata previously held a pointer to some other dynamically allocated object then re-assigning it to the new object does _not_ cause the original object to be destroyed, and the resources of any such object will not therefore be released. If this were the only pointer to the previous pointed to object then it can no longer be accessed. Its memory and quite possibly any additional resources it holds will remain in use but inaccessible for the duration of the program execution (a resource leak).

The second statement may also be in error for the same reasons as the first - the arguments a and b do not resolve the choice of constructor to a single item. It can also fail at runtime, in addition to the constructor throwing an exception, if there is insufficient dynamic memory available for the object. In this case new will throw a std::bad_alloc exception.

Hope this helps.  


All Answers

Answers by Expert:

Ask Experts


Ralph McArdell


I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.


My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at


©2017 All rights reserved.

[an error occurred while processing this directive]