鉴于以下代码,我是否会有内存泄漏?
class B {
int x;
}
class A {
int x;
int y;
B* tab[10];
A(int x, int y){
this->x = x;
this->y = y;
for (int i = 0; i < 10; i++){
tab[i] = new B;
}
}
~A(){
for (int i = 0; i < 10; i++){
delete tab[i];
}
}
int main(){
A a(10, 10);
return 0;
}
根据我的理解,不会有来自B类指针的内存泄漏,但我担心通过覆盖默认的析构函数行为导致一些意外泄漏,不会有来自内存泄漏int x和int y因为我的析构函数完全省略了它们?
答案 0 :(得分:0)
根据我的理解,不会有来自B类指针的内存泄漏
你在这里是对的。
但我担心通过覆盖默认的析构函数行为会导致一些意外泄漏
你不应该害怕。默认析构函数不能很好地处理指针。你的实施很好。
不能有来自int x和int y的内存泄漏,因为我的析构函数完全省略了它们?
来自原语的内存泄漏?不,int
不能泄漏。 int*
int
可以,但不是一个简单的,好的,new
。对于其他非指针类型也是如此。事情不是malloc
,delete
等。不会泄漏,也不需要相应的free
或 <?php
include "Mail.php";
$email = mysqli_real_escape_string($connect, $data->reset_email);
$link = 'https://www.omadahq.com/dashboard/password.php?token='.$token;
$reply = "no-reply@omadahq.com";
$subject = "OmadaHQ Password Reset";
$from = "OmadaHQ < no-reply@omadahq.com >";
$to = $name." < ".$email." > ";
$mailer = 'PHP/' . phpversion();
$content = "<p>Hello! <br> You have requested a reset to your password, click the button below to reset your password</p><br><br><a href='".$link."' target='_blank'><button style='background: #2196F3; padding: 10px 50px 10px 50px; color: white; border:none'>Reset</button></a><br><br><small>Ignore this message if you did not request a password rest</small>";
$message = "
<html lang='en' style='font-size: 15px; font-family: Montserrat, sans-serif; line-height: 28px;'>
<center>
<body style='margin:0px; width: 100%'>
<table style='border-spacing: 0px; min-width: 502px'>
<thead>
<td style='background: #2196f3; width: 100%; height: 70px'>
<span style='margin: 25px 0px 25px 25px; color: #FDFFFC'>OmadaHQ</span><small style='font-size: 10px; color: white;'>BETA</small>
</td>
</thead>
<tbody>
<td style='background: #f1f1f1!important; width: 100%; min-height: 1000px; padding: 50px'>".$content."</td>
</tbody>
</table>
</body>
</center>
</html>";
$host = "ssl://omadahq.com";
$port = "465";
$username = "no-reply@omadahq.com";
//not actual password
$password = "mypass";
$headers = array (
'From' => $from,
'Reply-To' => $reply,
'MIME-Version' => "1.0",
'To' => $to,
'Subject' => $subject,
'Content-type' => 'text/html; charset=iso-8859-1',
);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password)
);
$mail = $smtp->send($email, $headers, $message);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo"success";
}
}
?>
s
答案 1 :(得分:0)
由于您没有使用会动态分配内存的new
运算符,因此不会出现内存泄漏。相反,它将使用堆栈,并且在main
函数的末尾它会破坏你的对象。