上传cppreference

This commit is contained in:
2025-08-01 12:57:54 +08:00
parent e29892f8cd
commit 196e706fe4
5485 changed files with 982007 additions and 3 deletions

View File

@@ -0,0 +1,172 @@
<!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>set_constraint_handler_s, constraint_handler_t</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_set_constraint_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">set_constraint_handler_s, constraint_handler_t</h1>
<div id="bodyContent">
<div id="contentSub"><span class="subpages">&lt; <a href="c.html">c</a>&lrm; | <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>&#23450;&#20041;&#20110;&#22836;&#25991;&#20214; <code>&lt;stdlib.h&gt;</code></div>
</td>
<td></td>
<td></td>
</tr>
<tr class="t-dcl t-since-c11">
<td>
<div><span class="mw-geshi c source-c">constraint_handler_t set_constraint_handler_s<span class="br0">(</span> constraint_handler_t handler <span class="br0">)</span><span class="sy4">;</span></span></div>
</td>
<td class="t-dcl-nopad"></td>
<td><span class="t-mark-rev t-since-c11">(C11 &#36215;)</span></td>
</tr>
<tr class="t-dcl-sep">
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
<p>&#37197;&#32622;&#25152;&#26377;<a href="c-error.html#.E8.BE.B9.E7.95.8C.E6.A3.80.E6.9F.A5">&#36793;&#30028;&#26816;&#26597;&#20989;&#25968;</a>&#22312;&#36816;&#34892;&#26102;&#21046;&#32422;&#36829;&#35268;&#26102;&#25152;&#35843;&#29992;&#30340;&#22788;&#29702;&#20989;&#25968;&#65292;&#25110;&#23558;&#20854;&#24674;&#22797;&#25104;&#40664;&#35748;&#65288;&#33509; <code>handler</code> &#26159;&#31354;&#25351;&#38024;&#65289;&#12290;</p>
<p>&#27492;&#22788;&#29702;&#20989;&#25968;&#24517;&#39035;&#26159; <span class="t-c"><span class="mw-geshi c source-c">constraint_handler_t</span></span> &#30340;&#20989;&#25968;&#25351;&#38024;&#65292;&#23450;&#20041;&#22914;&#19979;&#65306;</p>
<table class="t-dcl-begin">
<tbody>
<tr class="t-dsc-header">
<td>
<div>&#23450;&#20041;&#20110;&#22836;&#25991;&#20214; <code>&lt;stdlib.h&gt;</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="kw1">typedef</span> <span class="kw4">void</span> <span class="br0">(</span><span class="sy2">*</span>constraint_handler_t<span class="br0">)</span><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>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">void</span> <span class="sy2">*</span><span class="kw4">restrict</span> ptr,<br /></p>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; errno_t error<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 &#36215;)</span></td>
</tr>
<tr class="t-dcl-sep">
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
<p>&#36816;&#34892;&#26102;&#21046;&#32422;&#36829;&#35268;&#21457;&#29983;&#26102;&#65292;&#20250;&#25353;&#29031;&#20197;&#19979;&#21442;&#25968;&#35843;&#29992;&#23427;&#65306;</p>
<div class="t-li1"><span class="t-li">1)</span> &#25351;&#21521;&#25551;&#36848;&#38169;&#35823;&#30340;&#23383;&#31526;&#20018;&#30340;&#25351;&#38024;</div>
<div class="t-li1"><span class="t-li">2)</span> &#25351;&#21521;&#23454;&#29616;&#23450;&#20041;&#30340;&#23545;&#35937;&#30340;&#25351;&#38024;&#25110;&#31354;&#25351;&#38024;&#12290;&#23454;&#29616;&#23450;&#20041;&#23545;&#35937;&#30340;&#20363;&#23376;&#65292;&#26159;&#32473;&#20986;&#26816;&#27979;&#21040;&#36829;&#35268;&#30340;&#20989;&#25968;&#21517;&#21644;&#26816;&#27979;&#21040;&#36829;&#35268;&#26102;&#30340;&#34892;&#21495;&#30340;&#23545;&#35937;</div>
<div class="t-li1"><span class="t-li">3)</span> &#35201;&#30001;&#35843;&#29992;&#26041;&#20989;&#25968;&#36820;&#22238;&#30340;&#38169;&#35823;&#32534;&#21495;&#65292;&#33509;&#23427;&#27491;&#22909;&#26159;&#36820;&#22238; <code>errno_t</code> &#30340;&#20989;&#25968;&#20043;&#19968;</div>
<p>&#33509;&#20174;&#19981;&#35843;&#29992; <code>set_constraint_handler_s</code> &#65292;&#21017;&#40664;&#35748;&#22788;&#29702;&#26159;&#23454;&#29616;&#23450;&#20041;&#30340;&#65306;&#23427;&#21487;&#20197;&#26159; <span class="t-lc">abort_handler_s</span> &#12289; <span class="t-lc">ignore_handler_s</span> &#25110;&#21478;&#22806;&#30340;&#23454;&#29616;&#23450;&#20041;&#22788;&#29702;&#20989;&#25968;&#12290;</p>
<dl>
<dd></dd>
<dd>&#21516;&#25152;&#26377;&#36793;&#30028;&#26816;&#26597;&#20989;&#25968;&#65292; <code>set_constraint_handler_s, constraint_handler_t</code> &#20165;&#33509;&#23454;&#29616;&#23450;&#20041;&#20102; <span class="t-c"><span class="mw-geshi c source-c">__STDC_LIB_EXT1__</span></span> &#65292;&#19988;&#29992;&#25143;&#22312;&#21253;&#21547; <code>&lt;stdlib.h&gt;</code> &#21069;&#23450;&#20041; <span class="t-c"><span class="mw-geshi c source-c">__STDC_WANT_LIB_EXT1__</span></span> &#20026;&#25972;&#25968;&#24120;&#37327; <span class="t-c"><span class="mw-geshi c source-c"><span class="nu0">1</span></span></span> &#25165;&#20445;&#35777;&#21487;&#29992;&#12290;</dd>
</dl>
<h3><span class="mw-headline" id=".E5.8F.82.E6.95.B0">&#21442;&#25968;</span></h3>
<table class="t-par-begin">
<tr class="t-par">
<td>handler</td>
<td>-</td>
<td><span class="t-c"><span class="mw-geshi c source-c">constraint_handler_t</span></span> &#31867;&#22411;&#30340;&#20989;&#25968;&#25351;&#38024;&#25110;&#31354;&#25351;&#38024;</td>
</tr>
</table>
<h3><span class="mw-headline" id=".E8.BF.94.E5.9B.9E.E5.80.BC">&#36820;&#22238;&#20540;</span></h3>
<p>&#25351;&#21521;&#20808;&#21069;&#23433;&#35013;&#36816;&#34892;&#26102;&#21046;&#32422;&#22788;&#29702;&#20989;&#25968;&#30340;&#25351;&#38024;&#65288;&#27880;&#24847;&#65306;&#27492;&#25351;&#38024;&#20915;&#19981;&#20250;&#26159;&#31354;&#25351;&#38024;&#65292;&#22240;&#20026;&#35843;&#29992; <span class="t-c"><span class="mw-geshi c source-c">set_constraint_handler_s<span class="br0">(</span><a href="c-types-NULL.html"><span class="kw103">NULL</span></a><span class="br0">)</span></span></span> &#20250;&#35774;&#32622;&#31995;&#32479;&#40664;&#35748;&#22788;&#29702;&#20989;&#25968;&#65289;&#12290;</p>
<h3><span class="mw-headline" id=".E7.A4.BA.E4.BE.8B">&#31034;&#20363;</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 &lt;string.h&gt;</span>
<span class="co2">#include &lt;stdio.h&gt;</span>
<span class="co2">#include &lt;stdlib.h&gt;</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">&quot;Too long!&quot;</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">&quot;dst = <span class="es1">\&quot;</span>%s<span class="es1">\&quot;</span>, r = %d<span class="es1">\n</span>&quot;</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">&quot;Too long!&quot;</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">&quot;dst = <span class="es1">\&quot;</span>%s<span class="es1">\&quot;</span>, r = %d<span class="es1">\n</span>&quot;</span>, dst, r<span class="br0">)</span><span class="sy4">;</span>
<span class="co2">#endif</span>
<span class="br0">}</span></pre></div>
</div>
<p>&#21487;&#33021;&#30340;&#36755;&#20986;&#65306;</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;">
<div class="text source-text">
<pre class="de1">dst = &quot;&quot;, 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 &lt;= (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&#39;s vendor to get more help.
Aborted</pre></div>
</div>
</div>
<h3><span class="mw-headline" id=".E5.BC.95.E7.94.A8">&#24341;&#29992;</span></h3>
<div class="t-ref-std-11">
<ul>
<li>C11 &#26631;&#20934;&#65288;ISO/IEC 9899:2011&#65289;&#65306;</li>
</ul>
<dl>
<dd>
<ul>
<li>K.3.6/2 constraint_handler_t (p: 604)</li>
</ul>
</dd>
</dl>
<dl>
<dd>
<ul>
<li>K.3.6.1.1 The set_constraint_handler_s function (p: 604-605)</li>
</ul>
</dd>
</dl>
</div>
<h3><span class="mw-headline" id=".E5.8F.82.E9.98.85">&#21442;&#38405;</span></h3>
<table class="t-dsc-begin">
<tr class="t-dsc">
<td>
<div class="t-dsc-member-div">
<div><a href="c-error-abort_handler_s.html"><span class="t-lines"><span>abort_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>&#36793;&#30028;&#26816;&#26597;&#20989;&#25968;&#30340;&#24322;&#24120;&#20013;&#27490;&#22238;&#35843;<br />
<span class="t-mark">(&#20989;&#25968;)</span></td>
</tr>
<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>&#36793;&#30028;&#26816;&#26597;&#20989;&#25968;&#30340;&#24573;&#30053;&#22238;&#35843;<br />
<span class="t-mark">(&#20989;&#25968;)</span></td>
</tr>
</table>
</div>
<div class="visualClear"></div>
</div>
</div>
</div>
</body>
</html>