GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ 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 06-Dec-2007, 07:08
tufan tufan is offline
Junior Member
 
Join Date: Aug 2006
Posts: 32
tufan is on a distinguished road

File size optimization.


CPP / C++ / C Code:
void stack<T>::clear()
{
	free(p);
	p=NULL;
	size=0;
}

stack<T>::stack()
{
	p=NULL;
	size=0;
}

template<class T>
stack<T>::~stack()
{
	free(p);
}
hi.i want to save from size.so i want to make the clear function like this:
CPP / C++ / C Code:
void stack<T>::clear()
{
     this->~stack();
     stack();

}
but it calls the destructor after the constructor again.
also i want to ask why i can't call the destructor with only:
CPP / C++ / C Code:
~stack();
and the constructor with:
CPP / C++ / C Code:
this->stack();
thx..
  #2  
Old 06-Dec-2007, 07:55
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 479
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: file size optimization..


What is your question?

I would not make a habit out of calling destructors like that. What exactly are you trying to accomplish?

Also, in your destructor, you may want to check to make sure that p is not NULL before freeing it.
  #3  
Old 06-Dec-2007, 09:00
tufan tufan is offline
Junior Member
 
Join Date: Aug 2006
Posts: 32
tufan is on a distinguished road

Re: file size optimization..


why don't u use constructors and destructors like that?what is wrong with it?i said in the post,it's for saving from file size.three lines means,a place for one more function and i don't want it.also it could be more lines than three.you may be right about freeing p.
  #4  
Old 06-Dec-2007, 09:11
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 479
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: file size optimization..


Why are you worried about 3 lines? That is less than 100 bytes. You should be able to find a hard drive that has millions of bytes (some of them have billions). Saving the file size of your source code should not be a high priority in comparison with writing cleaner code.
  #5  
Old 06-Dec-2007, 09:15
tufan tufan is offline
Junior Member
 
Join Date: Aug 2006
Posts: 32
tufan is on a distinguished road

Re: file size optimization..


cleaner code.i was talking about that too.according to you which seems more clear,this one:
CPP / C++ / C Code:
void stack<T>::clear()
{
     this->~stack();
     stack();

}
or this one:
CPP / C++ / C Code:
void stack<T>::clear()
{
	free(p);
	p=NULL;
	size=0;
}

which is the one that appropriates to less code reuse?
if there's another function that does the same function why do i write it again?
  #6  
Old 06-Dec-2007, 09:22
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 479
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: file size optimization..


I would probably have done it the opposite way but if that way works for you, more power to you.

The opposite way would be like:
CPP / C++ / C Code:
void stack<T>::clear()
{
	if(p != NULL) free(p);
	p=NULL;
	size=0;
}

void stack<T>::~stack()
{ clear();
}

The thing to worry about is if you ever put something in the destructor that you would not want on a regular clear(). I suppose it would work either way.
  #7  
Old 06-Dec-2007, 09:32
tufan tufan is offline
Junior Member
 
Join Date: Aug 2006
Posts: 32
tufan is on a distinguished road

Re: file size optimization..


but it's unnecessary to make p NULL and size=0 for a stack variable that goes out of scope.so it makes the program run slower.i'm wondering one thing.so if the destructor is this:
CPP / C++ / C Code:
~stack()
{
	free(p);
        cout<<"p deleted<<"\n";
}
this function:
CPP / C++ / C Code:
clear()
{
     this->~stack();
     stack();
}

prints:
CPP / C++ / C Code:
p deleted
p deleted

in here:

s.clear();

tell me this if u know?
  #8  
Old 06-Dec-2007, 09:45
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 479
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: file size optimization..


That is correct. If you call the clear() method it will run the destructor, but then it runs the destructor automatically (that's the purpose of the destructor) when it leaves the scope or is deleted.

CPP / C++ / C Code:
int main()
{ stack<int> s;
  s.clear(); // will print out "p deleted"
  return 0;
  // will print out "p deleted" as we leave the scope
}
  #9  
Old 06-Dec-2007, 10:27
Kimmo Kimmo is offline
Member
 
Join Date: Mar 2007
Location: Finland
Posts: 229
Kimmo has a spectacular aura aboutKimmo has a spectacular aura about

Re: file size optimization..


Quote:
Originally Posted by tufan
also i want to ask why i can't call the destructor with only:
CPP / C++ / C Code:
~stack();
Because a destructor is not a global function.

Quote:
and the constructor with:
CPP / C++ / C Code:
this->stack();
So you would need an object to call the constructor? And to get an object you would need to call the constructor. But you couldn't call the constructor until you had an object. So you could never call the constructor.

I myself kind of missed what was the question in this thread. It was not very clear.

Quote:
but it's unnecessary to make p NULL and size=0 for a stack variable that goes out of scope.so it makes the program run slower.i'm wondering one thing.so if the destructor is this:
If it's a stack variable, why were you freeing it in the first place?

This thread REALLY confuses me. Could you take some time to state your question clearly if it wasn't already answered.
__________________
Music, programming, endless learning..
  #10  
Old 06-Dec-2007, 10:45
tufan tufan is offline
Junior Member
 
Join Date: Aug 2006
Posts: 32
tufan is on a distinguished road

Re: file size optimization..


fakepoo it's not like you think it's not because of the stack variable that goes out of scope,i stopped the program before return 0; with getchar() and look output,there were two "p deleted" lines.it's because,in the clear function.when i call stack() function,immediately after it jumps to destructor again,before the function finishes.i'm trying to understand this.is there a temporary object there?if there is,which object do the p variable that it makes NULL and the size variable that it makes 0 belongs to?if i declare a variable like u do:
stack<int> s;
do they belong to s?or the temporary variable?
can we make calls like this in main?
s=stack();
if we can do;then should i change the stack(); line in the clear function like this:
*this=stack();
 
 

Recent GIDBlogFlickr uploads of IA pictures 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
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 07:44
Hard drive/CPU Diagnoses Issues binarybug Computer Hardware Forum 1 22-Jan-2007 19:23
Determing size of a binary file Dream86 C++ Forum 7 01-Jun-2005 10:10
Having a problem Chuckles Computer Hardware Forum 19 13-Sep-2004 12:17
Yet another CD burner problem: Lite-On LSC-24082K Erwin Computer Hardware Forum 1 22-May-2004 11:28

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

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


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