📄 faqcat5e04.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3O//DTD W3 HTML 2.0//EN"><!-- This collection of hypertext pages is Copyright 1995-2005 by Steve Summit. --><!-- Content from the book "C Programming FAQs: Frequently Asked Questions" --><!-- (Addison-Wesley, 1995, ISBN 0-201-84519-9) is made available here by --><!-- permission of the author and the publisher as a service to the community. --><!-- It is intended to complement the use of the published text --><!-- and is protected by international copyright laws. --><!-- The on-line content may be accessed freely for personal use --><!-- but may not be published or retransmitted without explicit permission. --><!-- --><!-- this page built Sat Dec 24 21:47:46 2005 by faqproc version 2.7 --><!-- from source file strangeprob.sgml dated Tue Jun 29 21:44:40 2004 --><!-- corresponding to FAQ list version 4.0 --><html><!-- Mirrored from c-faq.com/~scs/cgi-bin/faqcat.cgi?sec=strangeprob by HTTrack Website Copier/3.x [XR&CO'2008], Sat, 14 Mar 2009 07:58:21 GMT --><head><base ><meta name=GENERATOR content="faqproc"><title>Strange Problems</title></head><body bgcolor="#ffffff"><H1>16. Strange Problems</H1><a name="emptyloop"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../strangeprob/emptyloop.html"><!-- qtag -->Question 16.1</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>Why is this loop always executing once?<pre> for(i = start; i < end; i++); { printf("%d\n", i); }</pre></p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>The accidental extra semicolonhiding at the end of the line containing the <TT>for</TT>constitutes a null statement which is,as far as the compiler is concerned,the loop body.The following brace-enclosed block,which you thought(and the indentation suggests)was a loop body,is actually the nextstatement,anditis traversedexactlyonce,regardless of the number of loop iterations.</p><p>See also question <a href="faqcat6b6b.html?sec=struct#retcrash">2.18</a>.</p><p>References:CT&P Sec. 2.3 pp. 20-21<hr><hr><hr><a name="unclosed"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../strangeprob/unclosed.html"><!-- qtag -->Question 16.1b</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>I'm getting baffling syntax errors which make no sense at all,and it seems like large chunks of my program aren't being compiled.</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>Check for unclosed comments,mismatched<TT>#if</TT>/<TT>#ifdef</TT>/<TT>#ifndef</TT>/<TT>#else</TT>/<TT>#endif</TT>directives,and perhaps unclosed quotes;remember to check header files, too.(See also questions<a href="faqcat6b6b.html?sec=struct#retcrash">2.18</a>,<a href="faqcat204f.html?sec=cpp#headerglom">10.9</a>,and<a href="faqcat7d4b.html?sec=ansi#preansi">11.29a</a>.)<hr><hr><hr><a name="procedure"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../strangeprob/procedure.html"><!-- qtag -->Question 16.1c</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>Why isn't my procedure call working?The compiler seems to skip right over it.</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>Does the code look like this?<pre> myprocedure;</pre>C has onlyfunctions,andfunctioncalls always require parenthesized argumentlists,even if empty.Use<pre> myprocedure();</pre>Without the parentheses,the reference to the function name simply generatesa pointer to the function,which is then discarded.<hr><hr><hr><a name="headerglom"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../strangeprob/headerglom.html"><!-- qtag -->Question 16.2</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>I'm getting strangesyntax errorson the very firstdeclaration ina file,but it looks fine.</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>See question <a href="faqcat204f.html?sec=cpp#headerglom">10.9</a>.<hr><hr><hr><a name="biglocal"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../strangeprob/biglocal.html"><!-- qtag -->Question 16.3</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>This program crashes before it even runs!(When single-stepping with a debugger, it dies before the firststatement in <TT>main</TT>.)</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>You probably have one or more very large(kilobyte or more)local arrays.Manysystems have fixed-size stacks,and even those which perform dynamic stack allocation automatically(e.g. Unix)can be confused when the stack tries to grow by a huge chunkall at once.It is often better to declare large arrays with <TT>static</TT> duration(unless of course you need a fresh set with each recursive call,in which case you could dynamically allocate them with <TT>malloc</TT>;see also question <a href="faqcatd3c2.html?sec=decl#autoaggrinit">1.31</a>).</p><p>Other possibilitiesarethat your program has been linked incorrectly(combining object modules compiled with differentcompilationoptions,or using improper dynamic libraries),orthat run-time dynamic library linkingis failing for some reason,or that you have somehowmisdeclared <TT>main</TT>.</p><p>See also questions<a href="faqcat7d4b.html?sec=ansi#voidmain">11.12b</a>,<a href="faqcat5e04.html?sec=strangeprob#crashatexit">16.4</a>,<a href="faqcat5e04.html?sec=strangeprob#funnybugs">16.5</a>,and<a href="faqcatccbd.html?sec=resources#lint1">18.4</a>.<hr><hr><hr><a name="crashatexit"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../strangeprob/crashatexit.html"><!-- qtag -->Question 16.4</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>I have a program that seems to run correctly,butitcrashes as it's exiting,<em>after</em>the last statement in <TT>main()</TT>.What could be causing this?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>There are at least three things to look for:<OL><li>If a semicolon in a previous declaration is missing,<TT>main</TT>might be inadvertently declared as returning a structure,conflicting with the run-time startup code'sexpectations.Seequestions <a href="faqcat6b6b.html?sec=struct#retcrash">2.18</a>,<a href="faqcat204f.html?sec=cpp#headerglom">10.9</a>,<a href="faqcat7d4b.html?sec=ansi#voidmain">11.12b</a>,and <a href="faqcat7d4b.html?sec=ansi#voidmain3">11.14a</a>.<li>If<TT>setbuf</TT> or <TT>setvbuf</TT>is called,and if the supplied bufferis an automatic, local variable of <TT>main</TT>(or any function),the buffer may not exist any moreby the time the stdiolibrary tries to performits finalcleanup.<li>Acleanup functionregistered by <TT>atexit</TT>may have an error.Perhaps <em>it</em> istrying to reference datalocal to <TT>main</TT>or to some other functionwhich no longer exists.</OL>(The second and third problemsare actually closely related to question <a href="faqcatbafd.html?sec=malloc#retaggr">7.5a</a>;seealso question<a href="faqcat7d4b.html?sec=ansi#exitvsreturn">11.16</a>.)</p><p>References:CT&P Sec. 5.3 pp. 72-3<hr><hr><hr><a name="funnybugs"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../strangeprob/funnybugs.html"><!-- qtag -->Question 16.5</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>This program runs perfectly onone machine,but I get weird results onanother.Stranger still,adding orremoving a debugging printoutchanges the symptoms...</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>Lots of things could be going wrong;here are a few of the more common things to check:<UL><li>uninitialized local variables<a href="../../strangeprob/stktrash.html" rel=subdocument>[footnote]</a>(see also question <a href="faqcatbafd.html?sec=malloc#malloc1">7.1</a>)<li>integer overflow,especially on 16-bit machines,especially of an intermediate result when doingthings like <TT>a * b / c</TT>(see also question <a href="faqcatee08.html?sec=expr#intoverflow1">3.14</a>)<li>undefined evaluation order(see questions <a href="faqcatee08.html?sec=expr#evalorder1">3.1</a> through<a href="faqcatee08.html?sec=expr#precvsooe">3.4</a>)<li>omitteddeclarationof external functions,especially those whichreturnsomethingother than <TT>int</TT>,or have ``narrow'' or variable arguments(see questions<a href="faqcatd3c2.html?sec=decl#implfdecl">1.25</a>,<a href="faqcat7d4b.html?sec=ansi#argpromos">11.3</a>,<a href="faqcat973e.html?sec=fp#fpdecl">14.2</a>,and <a href="faqcat744e.html?sec=varargs#protos">15.1</a>)<li>dereferencednull pointers(see section <a href="faqcat1f1a.html?sec=null#index">5</a>)<li>improper <TT>malloc</TT>/<TT>free</TT> use:assuming <TT>malloc</TT>'ed memory contains 0,assuming <TT>free</TT>d storage persists,<TT>free</TT>ing something twice,corrupting the <TT>malloc</TT> arena(see also questions<a href="faqcatbafd.html?sec=malloc#crash">7.19</a>and<a href="faqcatbafd.html?sec=malloc#useafterfree">7.20</a>)<li>pointer problems in general(see alsoquestions <a href="faqcat5e04.html?sec=strangeprob#ptralign">16.7</a> and <a href="faqcat5e04.html?sec=strangeprob#segv">16.8</a>)<li>mismatch between
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -