GIDForums  

Go Back   GIDForums > Computer Programming Forums > MS Visual C++ / MFC Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 11-Jan-2006, 23:35
Janakiraman Janakiraman is offline
Junior Member
 
Join Date: Apr 2005
Location: Chennai - India
Posts: 47
Janakiraman is on a distinguished road

In which case static object - dynamic object can be used


Hi 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  
Old 14-Jan-2006, 02:30
aie0 aie0 is offline
Member
 
Join Date: Dec 2004
Posts: 246
aie0 is a jewel in the roughaie0 is a jewel in the rough

Re: In which case static object - dynamic object can be used


Dynamic - anywhere you want.
Static - in static functions.
  #3  
Old 16-Jan-2006, 01:37
Dominic Dominic is offline
Member
 
Join Date: Aug 2005
Posts: 123
Dominic will become famous soon enough

Re: In which case static object - dynamic object can be used


Static 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  
Old 16-Jan-2006, 04:47
aie0 aie0 is offline
Member
 
Join Date: Dec 2004
Posts: 246
aie0 is a jewel in the roughaie0 is a jewel in the rough

Re: In which case static object - dynamic object can be used


Quote:
Static objects are deleted when they go out of scope
When does static object exits its scope?
  #5  
Old 16-Jan-2006, 06:10
Dominic Dominic is offline
Member
 
Join Date: Aug 2005
Posts: 123
Dominic will become famous soon enough

Re: In which case static object - dynamic object can be used


I could be wrong, but I believe a object declared as (CAbc abc would go out of scope in exactly the same way a variable would. So if it was declared in the function, then it would be deleted when the function exits. The compiler would have allocated some space for it at compile time. The information within that object being unavailable after the function has exited.
Try looking at this:
CPP / C++ / C Code:
class CAbc	
{
	public:

		CAbc::CAbc();
		CAbc::CAbc(char a); 
		CAbc::~CAbc();
	
		void CAbc::WriteStuff(void);

	protected:


	private:
		char Space[20];


};

CAbc::CAbc() 
{
	printf("Within Class CAbc\n");
	for(int i=0; i<20; i++){
		Space[i] = 'A';
	}

}
CAbc::CAbc(char a) 
{
	printf("Within Class CAbc 2nd contruction\n");
	for(int i=0; i<20; i++){
		Space[i] = a;
	}


}

CAbc::~CAbc()
{

}

void CAbc::WriteStuff(void)
{
	for(int i=0; i<20; i++){
		printf("%c",Space[i]);
	}
	printf("\n");

}



CAbc *LocalFunction(void)
{
	CAbc Abc1;
	CAbc Abc2('B');
	CAbc *p  = new CAbc('X');

	//
	//
	//

	//
	// delete p now would cause a program failure
	return p;
}

int OtherFunction(CAbc *q)
{
	q->WriteStuff();

	return 0;
}


int main(int argc, char* argv[])
{
	CAbc *q;

	printf("Hello World!\n");
	q = LocalFunction();
	return OtherFunction(q);

	// Note that the object pointed to by p has not been 
	// deleted, thus posible memory leaks.


}

Try looking in debuf to see when the objects are no longer in scope.


  #6  
Old 16-Jan-2006, 09:37
aie0 aie0 is offline
Member
 
Join Date: Dec 2004
Posts: 246
aie0 is a jewel in the roughaie0 is a jewel in the rough

Re: In which case static object - dynamic object can be used


Well 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  
Old 16-Jan-2006, 20:43
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 927
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: In which case static object - dynamic object can be used


Quote:
Originally Posted by Janakiraman
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(); )...?
I am slightly confused.

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  
Old 17-Jan-2006, 00:05
Dominic Dominic is offline
Member
 
Join Date: Aug 2005
Posts: 123
Dominic will become famous soon enough

Re: In which case static object - dynamic object can be used


For 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  
Old 17-Jan-2006, 14:08
aie0 aie0 is offline
Member
 
Join Date: Dec 2004
Posts: 246
aie0 is a jewel in the roughaie0 is a jewel in the rough

Re: In which case static object - dynamic object can be used


Quote:
Perhapes someone can enlighten me as to the correct terms to use.
If you declare a class like this
CPP / C++ / C Code:
 CAbc abc; 
Then it is auto-destructed in the end of the braces where it was declared and is called local variable.
If you declare a class like this
CPP / C++ / C Code:
 static CAbc abc; 

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:
 CAbc* abc = new CAbc(); 

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:
 static class CAbc {} 
Then it can be used without declaring an in stance of it, meaning that you can call its methods without declaring for the class. Often used for math classes.
 
 

Recent GIDBlogPython ebook by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 18:21.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.