![]() |
|
|||||||
|
|
Thread Tools | Search this Thread | Rate Thread |
|
#1
|
|||
|
|||
In which case static object - dynamic object can be usedHi All,
Could any one give an example in which case we can use static object (CAbc abc; ) and in which case we use dynamic object(CAbc *abc = new CAbc(); )...? Best Regards, Janakiraman |
|||
|
#2
|
|||
|
|||
Re: In which case static object - dynamic object can be usedDynamic - anywhere you want.
Static - in static functions. |
|
#3
|
|||
|
|||
Re: In which case static object - dynamic object can be usedStatic objects are deleted when they go out of scope, but you must remember to delete a dynamic object. Often this is not done, and memory leaks ensue.
A developement technique that I use to help prevent this is to run the software in debug mode in Visual C++ Dev studio, with no break points. When it completes, you can see if it has memory leaks. Do this one quick simple check everytime you add the posiblity of using an object and you should end up with no memory leaks. |
|
#4
|
|||
|
|||
Re: In which case static object - dynamic object can be usedQuote:
|
|
#5
|
|||
|
|||
Re: In which case static object - dynamic object can be usedI could be wrong, but I believe a object declared as (CAbc abc
Try looking at this: CPP / C++ / C Code:
|
|
#6
|
|||
|
|||
Re: In which case static object - dynamic object can be usedWell naturally if you declare on static object in the function then it won't be seen in other functions, these kind of objects are usually used for counting in recursion, but it is not deleted in the end of the function - this is its purpose to save info after the end of the function.
|
|
#7
|
||||
|
||||
Re: In which case static object - dynamic object can be usedQuote:
Are you talking only about memory allocation? Or about static keyword, etc? __________________
Don't walk in front of me, I may not follow. Don't walk behind me, I may not lead. Just walk beside me and be my friend. |
|
#8
|
|||
|
|||
Re: In which case static object - dynamic object can be usedFor me, I was only talking about a class object declared as CAbc Abc; not a static variable which would exit after the function has exited, but still only be available within the scope of the function... since it was declared there.
A quick point about reply from Aie0. A good point you have made. However, since the variable is out of scope when the function exits, doesnt the variable space become usable again for the program. In this sense, isnt the object deleted. The class objects were not declared using the keyword "static" so I think they would be deleted. If the keyword "static" had been used, then the values in the object would remain and could be used if the function was ever re-entered. In this case I would expect to have had a memory leak when the program ended. Do you think this would be the case? Also, try as I did, I could not get a memory leak with the tiny bit of knocked up code I put forward. I wonder if this is something to do with it being run in a consol window, rather than a windows window. I supose in conclusion, I must confess to not knowing how to describe a object which is made by (CAbc Abc) and (CAbc *p = new CAbc()). Most of the time i use objects declared like (CAbc Abc) is within another class header file, so in that case they are static for the duration that that class object eixts. Perhapes someone can enlighten me as to the correct terms to use. |
|
#9
|
|||
|
|||
Re: In which case static object - dynamic object can be usedQuote:
CPP / C++ / C Code:
If you declare a class like this CPP / C++ / C Code:
Then it is not destructed in the end of the function and can be reused in the next call of the function and it is called static variable. If the class holds any pointers they must be destructed implicitly when it is known for sure the class won't be used again. If you declare a class like this CPP / C++ / C Code:
Then it also not destructed but if you don't transfer the pointer to somewhere at the end of the function then it will be lost and memory leak is what you will get, called dynamically created class. If you define class like this CPP / C++ / C Code:
|
Recent GIDBlog
Problems with the Navy (Enlisted) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to listens to two different ports at the same time | jaro | C Programming Language | 5 | 06-Jan-2006 06:36 |
| Problem with one variable | bretter | C++ Forum | 1 | 16-May-2005 07:20 |
| Compiling Errors | ToddSAFM | C++ Forum | 22 | 18-Dec-2004 11:42 |
| there has to be a better way | dabigmooish | C++ Forum | 8 | 17-May-2004 10:24 |
| C++ file I/O | CronoX | C++ Forum | 36 | 09-Mar-2004 17:28 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The