151 lines
8.8 KiB
HTML
151 lines
8.8 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="zh" dir="ltr" class="client-nojs" xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh">
|
|
<head><meta http-equiv="x-ua-compatible" content="ie=edge">
|
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
|
<title>abort_handler_s</title>
|
|
<link rel="stylesheet" href="ext.css" />
|
|
<link rel="stylesheet" href="site_modules.css" />
|
|
</head>
|
|
<body class="mediawiki ltr sitedir-ltr ns-0 ns-subject page-c_error_abort_handler_s skin-cppreference2 action-view cpp-navbar">
|
|
<div id="cpp-content-base">
|
|
<div id="content"><a id="top"></a>
|
|
<h1 id="firstHeading" class="firstHeading">abort_handler_s</h1>
|
|
<div id="bodyContent">
|
|
<div id="contentSub"><span class="subpages">< <a href="c.html">c</a>‎ | <a href="c-error.html">error</a></span></div>
|
|
<div id="mw-content-text" lang="zh" dir="ltr" class="mw-content-ltr" xml:lang="zh">
|
|
<table class="t-dcl-begin">
|
|
<tbody>
|
|
<tr class="t-dsc-header">
|
|
<td>
|
|
<div>定义于头文件 <code><stdlib.h></code></div>
|
|
</td>
|
|
<td></td>
|
|
<td></td>
|
|
</tr>
|
|
<tr class="t-dcl t-since-c11">
|
|
<td>
|
|
<div><span class="mw-geshi c source-c"><span class="kw4">void</span> abort_handler_s<span class="br0">(</span> <span class="kw4">const</span> <span class="kw4">char</span> <span class="sy2">*</span> <span class="kw4">restrict</span> msg,<br /></span>
|
|
<p> <span class="kw4">void</span> <span class="sy2">*</span> <span class="kw4">restrict</span> ptr,<br />
|
|
errno_t error<br /></p>
|
|
<span class="br0">)</span><span class="sy4">;</span></div>
|
|
</td>
|
|
<td class="t-dcl-nopad"></td>
|
|
<td><span class="t-mark-rev t-since-c11">(C11 起)</span></td>
|
|
</tr>
|
|
<tr class="t-dcl-sep">
|
|
<td></td>
|
|
<td></td>
|
|
<td></td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
<p>写入必须包含 <code>msg</code> 所指向字符串的实现定义消息到 <span class="t-lc"><a href="c-io.html">stderr</a></span> ,然后调用 <span class="t-lc"><a href="c-program-abort.html">abort()</a></span> 。</p>
|
|
<p>指向此函数的指针可以传递给 <a href="c-error-set_constraint_handler_s.html">set_constraint_handler_s</a> 以建立运行时制约违规处理。</p>
|
|
<dl>
|
|
<dd></dd>
|
|
<dd>同所有边界检查函数, <code>abort_handler_s</code> 仅若实现定义了 <span class="t-c"><span class="mw-geshi c source-c">__STDC_LIB_EXT1__</span></span> ,且用户在包含 <code><stdlib.h></code> 前定义 <span class="t-c"><span class="mw-geshi c source-c">__STDC_WANT_LIB_EXT1__</span></span> 为整数常量 <span class="t-c"><span class="mw-geshi c source-c"><span class="nu0">1</span></span></span> 才保证可用。</dd>
|
|
</dl>
|
|
<h3><span class="mw-headline" id=".E5.8F.82.E6.95.B0">参数</span></h3>
|
|
<table class="t-par-begin">
|
|
<tr class="t-par">
|
|
<td>msg</td>
|
|
<td>-</td>
|
|
<td>指向要写到标准错误输出的消息的指针</td>
|
|
</tr>
|
|
<tr class="t-par">
|
|
<td>ptr</td>
|
|
<td>-</td>
|
|
<td>指向实现定义的对象的指针或空指针。实现定义对象的例子,是给出检测到违规的函数名和检测到违规时的行号的对象</td>
|
|
</tr>
|
|
<tr class="t-par">
|
|
<td>error</td>
|
|
<td>-</td>
|
|
<td>errno_t 类型的正值</td>
|
|
</tr>
|
|
</table>
|
|
<h3><span class="mw-headline" id=".E8.BF.94.E5.9B.9E.E5.80.BC">返回值</span></h3>
|
|
<p>无;此函数不对其调用者返回</p>
|
|
<h3><span class="mw-headline" id=".E6.B3.A8.E6.84.8F">注意</span></h3>
|
|
<p>若从未调用 <code>set_constraint_handler_s</code> ,则默认处理函数是实现定义的:可以为 <span class="t-lc">abort_handler_s</span> 、 <span class="t-lc">ignore_handler_s</span> 或另外的实现定义处理函数。</p>
|
|
<h3><span class="mw-headline" id=".E7.A4.BA.E4.BE.8B">示例</span></h3>
|
|
<div class="t-example">
|
|
<div class="t-example-live-link"></div>
|
|
<div dir="ltr" class="mw-geshi" style="text-align: left;">
|
|
<div class="c source-c">
|
|
<pre class="de1"><span class="co2">#define __STDC_WANT_LIB_EXT1__ 1</span>
|
|
<span class="co2">#include <string.h></span>
|
|
<span class="co2">#include <stdio.h></span>
|
|
<span class="co2">#include <stdlib.h></span>
|
|
|
|
<span class="kw4">int</span> main<span class="br0">(</span><span class="kw4">void</span><span class="br0">)</span>
|
|
<span class="br0">{</span>
|
|
<span class="co2">#ifdef __STDC_LIB_EXT1__</span>
|
|
<span class="kw4">char</span> dst<span class="br0">[</span><span class="nu0">2</span><span class="br0">]</span><span class="sy4">;</span>
|
|
set_constraint_handler_s<span class="br0">(</span>ignore_handler_s<span class="br0">)</span><span class="sy4">;</span>
|
|
<span class="kw4">int</span> r <span class="sy1">=</span> strcpy_s<span class="br0">(</span>dst, <span class="kw4">sizeof</span> dst, <span class="st0">"Too long!"</span><span class="br0">)</span><span class="sy4">;</span>
|
|
<a href="c-io-fprintf.html"><span class="kw851">printf</span></a><span class="br0">(</span><span class="st0">"dst = <span class="es1">\"</span>%s<span class="es1">\"</span>, r = %d<span class="es1">\n</span>"</span>, dst, r<span class="br0">)</span><span class="sy4">;</span>
|
|
set_constraint_handler_s<span class="br0">(</span>abort_handler_s<span class="br0">)</span><span class="sy4">;</span>
|
|
r <span class="sy1">=</span> strcpy_s<span class="br0">(</span>dst, <span class="kw4">sizeof</span> dst, <span class="st0">"Too long!"</span><span class="br0">)</span><span class="sy4">;</span>
|
|
<a href="c-io-fprintf.html"><span class="kw851">printf</span></a><span class="br0">(</span><span class="st0">"dst = <span class="es1">\"</span>%s<span class="es1">\"</span>, r = %d<span class="es1">\n</span>"</span>, dst, r<span class="br0">)</span><span class="sy4">;</span>
|
|
<span class="co2">#endif</span>
|
|
<span class="br0">}</span></pre></div>
|
|
</div>
|
|
<p>可能的输出:</p>
|
|
<div dir="ltr" class="mw-geshi" style="text-align: left;">
|
|
<div class="text source-text">
|
|
<pre class="de1">dst = "", r = 22
|
|
abort_handler_s was called in response to a runtime-constraint violation.
|
|
|
|
The runtime-constraint violation was caused by the following expression in strcpy_s:
|
|
(s1max <= (s2_len=strnlen_s(s2, s1max)) ) (in string_s.c:62)
|
|
|
|
Note to end users: This program was terminated as a result
|
|
of a bug present in the software. Please reach out to your
|
|
software's vendor to get more help.
|
|
Aborted</pre></div>
|
|
</div>
|
|
</div>
|
|
<h3><span class="mw-headline" id=".E5.BC.95.E7.94.A8">引用</span></h3>
|
|
<div class="t-ref-std-11">
|
|
<ul>
|
|
<li>C11 标准(ISO/IEC 9899:2011):</li>
|
|
</ul>
|
|
<dl>
|
|
<dd>
|
|
<ul>
|
|
<li>K.3.6.1.2 The abort_handler_s function (p: 605)</li>
|
|
</ul>
|
|
</dd>
|
|
</dl>
|
|
</div>
|
|
<h3><span class="mw-headline" id=".E5.8F.82.E9.98.85">参阅</span></h3>
|
|
<table class="t-dsc-begin">
|
|
<tr class="t-dsc">
|
|
<td>
|
|
<div class="t-dsc-member-div">
|
|
<div><a href="c-error-ignore_handler_s.html"><span class="t-lines"><span>ignore_handler_s</span></span></a></div>
|
|
<div><span class="t-lines"><span><span class="t-mark-rev t-since-c11">(C11)</span></span></span></div>
|
|
</div>
|
|
</td>
|
|
<td>边界检查函数的忽略回调<br />
|
|
<span class="t-mark">(函数)</span></td>
|
|
</tr>
|
|
<tr class="t-dsc">
|
|
<td>
|
|
<div class="t-dsc-member-div">
|
|
<div><a href="c-error-set_constraint_handler_s.html"><span class="t-lines"><span>set_constraint_handler_s</span></span></a></div>
|
|
<div><span class="t-lines"><span><span class="t-mark-rev t-since-c11">(C11)</span></span></span></div>
|
|
</div>
|
|
</td>
|
|
<td>设置边界检查函数的出错回调<br />
|
|
<span class="t-mark">(函数)</span></td>
|
|
</tr>
|
|
</table>
|
|
</div>
|
|
<div class="visualClear"></div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</body>
|
|
</html>
|