"Shit
! Also crazy. I have been working on this platform for a month. The number of can be crash is much more than I am in this month. why? Not the time of this dead platform, why is the memory of the handheld system and the girl who is willing to talk to me?
"
SolmyR complains.
"Brothers, what happened." Zero asked.
"Zero is the main presequence of our group, he knows a lot, the people are also handsome, and they are very happy. Even the Lili of the US Workgroup (longer than Sun Yanzi is still good enough)." SolmyR thought, "Boss, you see, and crazy. Why do I use dynamic memory more than 10 times, what is CRASH?"
"I know that you are Java programmers, maybe now, you still have to remember the GC (garbage collection mechanism, I said it is better than calling the God and cry", you have to know, you are now using C programming, Important is efficiency. "
"Efficiency, I ..." Suddenly, SolmyR felt that there was a liquid flowing at the end of the mouth. That is Solmy's saliva. Each time I mention the efficiency, Solmyr always wants to fluid, just like seeing beautiful MM, SolmyR wants to nose blood.
"C , about dynamic memory is new and delete."
"I know," Solmyr is eager to express himself, I want to prove that I don't know if I don't know if I have C . "In C , New operator is C built-in behavior. No one (perhaps) In addition to bjarne stroustrup, it cannot be changed. New Operator first calls a function named Operator New. Standard mode is like this:
void * operator new; Size_t size
The behavior is then configured on the back VOID * pointer. Delete Operator sends the object, then calls the function named Operator Delete, the standard form is like this:
Void Operator Delete (void * ptodealloc)
// GotW is still a standard form even after the pointer parameters are added.
// size_t size is to check if the memory you want to remove is the desired size. If the class is defined in the class level, as long as the base class is Virtual Destructor, then size can // ensure that it is correct.
Placement New ...... "
"Hey, it is good, make progress. About Placement New / Delete, Prophet Meyer [1] has detailed discussion. The original Placement New is just this:
Void * operator new (size_t, void * pmem) {return PMEM;}
'Over time, any Operator new version of any' requires an extra quota 'has gradually adopted the terminology of Placement New. In fact, this term has been remembered in the C standard specification. Therefore, when the C programmer talks about the so-called Placement New function, they may be that the above 'requires an additional VOID * parameter to point out where the item is placed, but may also refer to those' The parameter is required to be a single_t quota version of the SIZE_T quotient, including the above functions, including the above function, including other 'parameters more' 'Operator New functions. '- from [1]. Now let's consider a question. If the constructor throws an exception after the end of the Operator New ends, then the memory has been applied, and the answer is of course a compiler. Because the entire New Operator has not yet ended. So you can't get control. If you have overloaded Operator New / Delete, then the compiler will call the Operator DELETE? Since different Operator New may get memory through different methods, let the Operator Delete that allocated to release memory is obviously irresponsible. So the compiler assumes which Operator Delete has the same parameters as the Operator New (except size_t size and void * pdealloc), then the Operator Delete knows how the operator new gets memory. After the constructor throws an exception, the Operator Delete that is the same as the Operator New is called to release the memory. "
"So how should I solve the problem now?"
"Don't be so urgent, it has been five o'clock in the afternoon. Don't let people think that the programmer is the night tour. Go home to see the Effective C second part and gotw9, gotw10. Tomorrow."
Early the next morning, SolmyR walked into the office. I saw that Guru Zero had already geared. It was even in front of the computer to send and received email. I couldn't help but put it down the big cake in my hand. I ran and Zero said: "Boss yesterday In the evening, I pick up the night reading, summarize two points:
1. If you write an Operator New, write a Operator Delete.
2. Don't delete is not your own New's memory.
"Well, yes, how do I look, is like the terms in Effective C ? Have you ever thought about Placement DELETE?"
"Is there any problem with Placement delete?" Solmy is asked in a face.
"Have you tried to put a piece of memory to apply for a Placement New with placement delete? You can try it now."
I saw that SolmyR ran to a computer, and the two hands were flying on the keyboard, but we could hear the wow of his ignorance and the compiler. SolmyR is really no way, you can only ask Zero. ZERO has drank a coffee and said: "We usually write Operator Delete that is required for additional parameters will only be automatically called when the constructor throws an exception, and we are impossible to manually call to any extra parameters DELETE, why no built-in 'Placement Delete' is the reason for the 'Placement New' is that there is no way to guarantee it correctly, in the C type system, where to get a pointer to get the memory it points to It may be pointing to Heap, or STACK. So ... "" So when we do know how it gets a pointer, we can do this:
Template
{
IF (p) {
P-> ~ t (); // Explicit Destructor Call
A.deallocate (P);
}
}
In this way, there will be no resource leaks. "[2]
"But how do I solve my rare memory?"
"Allocator and boost in the standard library can solve the problem of memory management, do not have to call Operator New every time, there are fewer overhead, but I just received the customer's mail, they said that they are ready to be on their devices. Add 12MB of memory. "
"..." SolmyR depressed.
[1] Object Counting In C Cuj 1998.4 Chinese translation jJHOU.COM/ (programmer magazine: 2001.8 magazine less Placement New and Placement Delete)
[2] BJARNE STROUSTRUP's C Style and Technique FAQ IS TheRE A "Placement DELETE"?