473,408 Members | 2,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Please help regarding new and delete

Here is the lines of code. Here new has been used bot not delete.
Is it correct or we need to do some code change. Please let me know.
Your help is highly appreciated. Thanks.

Expand|Select|Wrap|Line Numbers
  1. int  IsSwapClearable ( const sTRADE* pSummitTrade )
  2. {
  3. cout<<"INSIDE IsSwapClearable()"<<endl;
  4.    try
  5.    {
  6.       int retval = sFALSE;
  7.  
  8.       CheckSwapClear* mySwapClearChecks = new CheckSwapClear();
  9.  
  10.       // must be a mapped counterparty so do not call IsSwapClearCounterparty();
  11.  
  12.       if ( mySwapClearChecks->IsSwapClearMappedCounterparty ( pSummitTrade->Base.Env->Cust.Name ) == sTRUE )
  13.       {
  14.          switch ( pSummitTrade->Base.TradeType )
  15.          {
  16.             case sTT_SWAP :
  17.                  retval = mySwapClearChecks->IsSwapClearableSwap ( pSummitTrade );
  18.                  break;
  19.             default :
  20.                  break;
  21.          }
  22.       }
  23. cout<<"EXITING IsSwapClearable()"<<endl;
  24.       return retval;
  25.    }
  26.    catch( DmgException& aExcep )
  27.    {
  28.       mLogMessage ( "EXCEPTION caught in IsSwapClearable()" );
  29. cout<<"EXITING A IsSwapClearable()"<<endl;
  30.       throw;
  31.    }
  32.    catch ( ... )
  33.    {
  34.       mLogMessage ( "Unknown Exceptions caught in IsSwapClearable()" );
  35. cout<<"EXITING B IsSwapClearable()"<<endl;
  36.       throw;
  37.    }
  38. }
  39.  
Jul 25 '07 #1
5 1250
Banfa
9,065 Expert Mod 8TB
The new operates on a user defined type CheckSwapClear. It is probably that this type is a class and in that case it is possible that this class keeps track of all new'd/instantiated items via the use of static member variables in which case it may be OK for the item to be new'd here without being deleted.

ON the other hand the CheckSwapClear class may not keep track of new'd/instantiated items in which case the new without delete would be a memory leak.
Jul 25 '07 #2
ravenspoint
111 100+
To make that clearer: Yes, you need to use delete.

Easier, would be to make it an automatic.

Instead of

Expand|Select|Wrap|Line Numbers
  1. CheckSwapClear* mySwapClearChecks = new CheckSwapClear();
  2.  
  3. ...
  4.  
  5. mySwapClearChecks->IsSwapClearMappedCounterparty
  6.  
write

Expand|Select|Wrap|Line Numbers
  1. CheckSwapClear mySwapClearChecks ;
  2.  
  3. ...
  4.  
  5. mySwapClearChecks.IsSwapClearMappedCounterparty
  6.  
Now the compiler will look after garbage collection automatically.
Jul 25 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
CheckSwapClear* mySwapClearChecks = new CheckSwapClear();

...

mySwapClearChecks->IsSwapClearMappedCounterparty
This is the correct thing to do. However, mySwapClearChecks should be a handle. See the C/C++ Article on Handle Classes. Using the heap is preferred over the stack as stack memory is often limited plus the compiler has conrtrol over deleting and not you. With the heap, you have control over deleting but you have to know when to do it. That is where the handle(smart pointer) comes in.

One of the worst things you can do in a C++ program is pass pointers around.
Jul 25 '07 #4
ravenspoint
111 100+
It is best to let the compiler look after deletion, wherever possible, since it saves a great deal of strain on the old brain.

The point about using up stack space does have to be considered. If you have a monster class that uses large amounts of memory, then creating it on the stack with an automatic variable may be problematical. The solution is that they should allocate their own memory ( in the constructor ) and do their own garbage collection ( in the destructor ) - in this way they can be created on the stack without grabbing all the stack memory and still leave the compiler with the chore of deciding when to do garbage collection. The snag is you now have to be careful about copy constructors.

Trying to outhink the compiler is a mugs game!
Jul 25 '07 #5
weaknessforcats
9,208 Expert Mod 8TB
It is best to let the compiler look after deletion, wherever possible, since it saves a great deal of strain on the old brain.
Except this doesn't work. True, the compiler will look aftrer deletion and will delete the object when it goes out of scope. But it does not check to see if there are any pointers in the program that still point to the object. If there are, then when those pointers are used later, the program crashes.

The only way to get this to work is to make all of the objects global so they are deleted after main() completes. But this doesn't wwork either because there are limits on global space.

Further, stack objects such as arrays have to be sized at compile time. In actual practice, an array needs to be sized at run time.

All roads lead to the heap. Allocation and deletion of heap objects should involve create functions and handles.
Jul 25 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Dan | last post by:
Hello, I am trying to read and write to an Excel file via my Java applet. I have done so successfully on several simple Excel files that simply had data cells without many complicated equations...
2
by: Rune Lanton | last post by:
Hello! I am developing an application for PPC2003, and I want too put my SQL CE db on a flash card. But I'm a little concerned about flash lifetime! Most flash memory have a limit of 100.000...
14
by: HP | last post by:
Hi All i have confussion regarding given problem please help me out 4. What happens with the following program: void main(){ myclass* pmc = new myclass; pmc = 0; delete pmc;}
3
by: razheev | last post by:
Hi, I am doing a purge process and trying to delete rows .Let me know how efficiently I can handle the purge process because of the RI on the tables. TABLE A is a PARENT of TABLE B, TABLE C,...
12
by: ravinderthakur | last post by:
hi experts, i have few questions regarding the delete operator in c++. why does c++ have to operators for deleting memeory viz delete and delete. why cannnot delete be used insted of...
8
by: someone | last post by:
Hey All: I have two pointers A & B pointing towards data objects C & D resp. Now I want to achieve the following: 1. Free up memory that A points to 2. Have A point towards D 3. Have B point...
3
by: Pravesh | last post by:
Hello All, I had some query regarding virtual functions/destructors. If a class is having some/all of its methods that are virtual then is it recommended that it should also have virtual...
2
by: rohits123 | last post by:
To optimize the memory usage , I am using a huge block of memory for my system and then dividing the initial chunk in to 4 pools. I have overloaded new and delete such that memory from a...
16
by: PeterAPIIT | last post by:
Hello all C++ expert programmer, i have wrote partial general allocator for my container. After reading standard C++ library and code guru article, i have several questions. 1. Why...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.