⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 c-iaq.html

📁 稀疏矩阵、链表、图、队列、二叉树、多叉树、排序、遗传算法等的实现
💻 HTML
📖 第 1 页 / 共 5 页
字号:
system(&quot;echo Please open a new shell now.&quot;);sprintf(cmdstring, &quot;echo Enter the command '%s' in it.&quot;, cmd);system(cmdstring);</PRE>		<P>This will not work if you haven't declared cmdstring properly.		</P>		<H4><A NAME="question-18.13"></A>18.13: How can I ensure objects of my class are always created		via ``new'' rather than as locals or global/static objects? </H4>		<P>Read the C++ FAQ. </P>		<H3><A NAME="section-19"></A>Section 19: Miscellaneous</H3>		<H4><A NAME="question-19.1"></A>19.1: What can I safely assume about the initial values of variables		which are not explicitly initialized? If global variables start		out as ``zero,'' is that good enough for null pointers and floating-point		zeroes? </H4>		<P>They're always zero. [<a href="c-iaq-a.html#question-19.1">a</a>]</P>		<H4><A NAME="question-19.2"></A>19.2: How can I write data files which can be read on other machines		with different word size, byte order, or floating point formats?		</H4>		<P>The traditional solution, pioneered by Microsoft, is to sell enough		copies of your proprietary, slow, and limited software that everyone		else supports your formats. </P>		<H4><A NAME="question-19.3"></A>19.3: How can I insert or delete a line (or record) in the middle		of a file? </H4>		<P>Using fcntl(), lock the line or record in the file exclusively.		Now, using another thread, read the file, at each byte, trying		to write that byte back. Whenever you succeed, write that byte		into another file. Then copy the new file over the old file, releasing		the lock first. </P>		<H4><A NAME="question-19.4"></A>19.4: How can I return several values from a function? </H4>		<P>Code like this ought to work. </P>		<PRE>long int foo() {	return 2L +3; /* returns both values */}</PRE>		<H4><A NAME="question-19.5"></A>19.5: If I have a char * variable pointing to the name of a function		as a string, how can I call that function? </H4>		<P>Try the following: </P>		<PRE>eval(s);</PRE>		<P>Now all you need to do is write eval(). </P>		<H4><A NAME="question-19.6"></A>19.6: I seem to be missing the system header file &lt;math.h&gt;. Can		someone send me a copy? </H4>		<P>A lot of people claim that it is useless to send people headers		from other machines. Not so! It can be informative, and can show		you a lot about how blatantly stupid your request was, although		it can't show you anything you wouldn't have known in an instant		had you thought before posting. </P>		<P>Of course, we'd be happy to send you the header files... </P>		<P>----cut here---- </P>		<PRE>/* math.h rev 7.0b (3/7/95) *//* RCS log: #log% - can anyone tell me why this doesn't work? * - joe, 2/12/93 *//* * Copyright 1995 Berserkley Software Systems &amp;&amp; Analytic Overdrive *//* Parts of this header, including in particular the second and * third clauses of the first sentance of the fourth comment, were * based on copyright agreements from other sources, including * Xerox corporation. *//* * math.h - math related macros and headers */#ifndef _MATH_H#define _MATH_H/* * global data and definitions */#ifdef __LITERAL_BIBLICAL_FUNDEMENTALISM#define PI 3.0				/* 1 Kings 7:23 */#endif/* * common (portable) structures and functions *//* * machine specific data */#include &lt;machine/math.h&gt;#endif /* _MATH_H // prevent multiple inclusion by using C++ comments*/----cut here----</PRE>		<P>(Morons.) [<a href="c-iaq-a.html#question-19.6">a</a>]</P>		<H4><A NAME="question-19.7"></A>19.7: How can I call FORTRAN (C++, BASIC, Pascal, Ada, LISP, perl)		functions from C? (And vice versa?) </H4>		<P>You can do things like this: </P>		<PRE>DO CALL FORTRAN;		fortran();__LINE__ BASIC;			basic();sub pascal;			pascal();(((((lisp)))))			lithp(); [*]&amp;perl_c;			perl():</PRE>		<P>(You can't call Ada from C; it's unsafe.) </P>		<P>[*] C is pass by value, of course. </P>		<H4><A NAME="question-19.8"></A>19.8: Does anyone know of a program for converting Pascal or FORTRAN		(or LISP, Ada, awk, ``Old'' C, ...) to C? </H4>		<P>Nope. However, the psychic friends network may have a lead. And		they're not just a psychic, they're also a friend. </P>		<H4><A NAME="question-19.9"></A>19.9: Is C++ a superset of C? Can I use a C++ compiler to compile		C code? </H4>		<P>C++ is a superset of something, we're not sure what. You can use		a C++ compiler to compile C code, but the results may surprise		you. </P>		<H4><A NAME="question-19.10"></A>19.10: Where can I get copies of all these public-domain programs?		</H4>		<P>From <A HREF="ftp://ftp.microsoft.com/">ftp://ftp.microsoft.com/</A> . Some of the code may look copyrighted; don't worry! The small		companies that wrote it in the first place are not available for		comment. </P>		<H4><A NAME="question-19.11"></A>19.11: When will the next International Obfuscated C Code Contest		(IOCCC) be held? How can I get a copy of the current and previous		winning entries? </H4>		<P>Next week. You missed the deadline. Tough, sucker. </P>		<H4><A NAME="question-19.12"></A>19.12: Why don't C comments nest? How am I supposed to comment		out code containing comments? Are comments legal inside quoted		strings? </H4>		<P>We believe it has something to do with captivity; C comments in		the wild mate and nest normally. The San Diego Zoo believes it		has managed to convince some C comments to nest, but it's hard		to tell how much of that is really in the preprocessor, and how		much of it is just bovine fecal matter. </P>		<H4><A NAME="question-19.13"></A>19.13: How can I get the ASCII value corresponding to a character,		or vice versa? </H4>		<P>chr$(foo); You would have known this if you had an integer basic		in ROM. </P>		<H4><A NAME="question-19.14"></A>19.14: How can I implement sets and/or arrays of bits? </H4>		<P>With linked lists of bitfields. You may also wish to simply use		a large set of constants and some clever use of the switch statement,		i.e.: </P>		<PRE>enum { zero, one, two, three };</PRE>		<PRE>int bitwise_or(int n, int m) {	switch (n) {	case three:		return three;		break;	case two:		switch (m) {		case one: case three: return three; break;		default: return two; break;		}		break;	case one:		switch (m) {		case two: case three: return three; break;		default: return one; break;		}		break;	default: case zero:		switch (m) {		case one: return one; break;		case two: return two; break;		case three: return three; break;		case zero: default: return zero; break;		}		break;	}}</PRE>		<P>Obviously, you'll need to increase this slightly to deal with		more than two bits. This is much more readable than the alleged		``C'' solution: </P>		<PRE>int bitwise_or(int n,int m){return n|m;}</PRE>		<P>Note how the lack of whitespace around operators obscures the		functionality of the code. A clear argument for explicit statement		of program logic over arcane operators, if I ever saw one. </P>		<P>The enum at the top isn't declared ``const int'', because the		resulting ``const poisoning'' would require casts during all of		the switch statements. </P>		<H4><A NAME="question-19.15"></A>19.15: What is the most efficient way to count the number of bits		which are set in a value? </H4>		<P>Start a counter at zero and add one to it for each bit set. Some		operating systems may provide a call to do this. For values over		INT_MAX/2, start the counter at CHAR_BIT * sizeof(int) and subtract		one for each bit not set. </P>		<H4><A NAME="question-19.16"></A>19.16: How can I make this code more efficient? </H4>		<P>Remove the comments; the no-op instructions generated by comments		can slow your code down signifigantly. Similarly, shorten variable		names. Most compilers, to implement pass by value, actually pass		the names of variables in the stack; shorter variable names will		reduce stack usage, and consequently execution time. If your compiler		has good loop optimization, replace </P>		<PRE>foo();</PRE>		<P>with </P>		<PRE>do {	foo();} while (1 != 1);</PRE>		<P>which will likely receive more optimization. </P>		<H4><A NAME="question-19.17"></A>19.17: Are pointers really faster than arrays? How much do function		calls slow things down? Is <CODE>++i</CODE> faster than <CODE>i = i + 1</CODE>? </H4>		<P>Yes. About 10 ms per call. Only on machines which feature preincrement		addressing. </P>		<H4><A NAME="question-19.18"></A>19.18: This program crashes before it even runs! (When single-stepping		with a debugger, it dies before the first statement in main.)		</H4>		<P>You probably declared main as ``void main(void)''. It's also possible		that the first statement in main is abort(); - by the as if rule,		the compiler can abort at any time before then, too. Some compilers		have bugs, and will produce buggy code for any module which includes		the letters ``a'', ``b'', ``o'', ``r'', and ``t'' in that order		before the first function declaration. </P>		<H4><A NAME="question-19.19"></A>19.19: What do ``Segmentation violation'' and ``Bus error'' mean?		</H4>		<P>C programs are very territorial, and divide their code into segments.		Violating these segments can trigger riots; similarly, pointers		and integral constants are at the front of the bus, wheras arrays,		strings, and other second-class data types are required to be		at the rear of the bus. When they start forgetting their places,		you can get a bus error. This is what the whole ``integral'' type		thing is about - integrated bussing. </P>		<H4><A NAME="question-19.20"></A>19.20: My program is crashing, apparently somewhere down inside		malloc, but I can't see anything wrong with it. </H4>		<P>Your vendor's library is buggy; complain loudly. Don't send them		any example code; they just ask for that so they can steal your		trade secrets. </P>		<H4><A NAME="question-19.21"></A>19.21: Does anyone have a C compiler test suite I can use? </H4>		<P>Yes. Unfortunately, it's probably broken. It's hard to tell. </P>		<H4><A NAME="question-19.22"></A>19.22: Where can I get a YACC grammar for C? </H4>		<P>You can't; YACC is written in C. </P>		<H4><A NAME="question-19.23"></A>19.23: I need code to parse and evaluate expressions. </H4>		<P>Ask any first year CS student. You may also wish to use your C		compiler. </P>		<H4><A NAME="question-19.24"></A>19.24: I need a sort of an ``approximate'' strcmp routine, for		comparing two strings for close, but not necessarily exact, equality.		

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -