【游戏编程扯淡精粹】《C语言现代方法》汇总

【游戏编程扯淡精粹】《C语言现代方法》汇总,第1张

【游戏编程扯淡精粹】《C语言现代方法》汇总 【游戏编程扯淡精粹】《C语言现代方法》汇总 PROGRAM-1-使用offset
  • 成员是数组,可以访问某个索引的偏移
  • 使用offset的意义在于,编译器对于成员会自己重排保证对齐
    • 所以offset不一定等于你自己手算的
// @zolo
// $ ./01.exe
// 0
// 4
// 8
// 12
#include 
#include 
struct s {
    char a;
    int b[2];
    float c;
};
int main(void)
{
    printf("%ldn", offsetof(struct s, a));
    printf("%ldn", offsetof(struct s, b[0]));
    printf("%ldn", offsetof(struct s, b[1]));
    printf("%ldn", offsetof(struct s, c));
    
}
EX-1-系统头文件位置;非标准头,以及用途 系统头文件位置

搜了stdio.h

  • windows sdk
  • visual studio
  • mingw
非标准头,以及用途 aio.hDeals with asynchronous Input and Output.aliases.hFunctions to access the aliases database that contains mail aliases.alloca.hContains function that allocates memory and automatically frees it.argp.hHandles parsing unix-style program arguments.argz.hVector handling an array of strings (program arguments) and a length.ar.hDescribes the structure of files produced by the ‘ar’ program.autosprintf.hFormatted output to an outputstream.byteswap.hMacros to swap the order of bytes in integer values.cpio.hHandles cpio archive format.crypt.hPassword and data encryption.dialog.hCommon declarations for all dialog modules.dirent.hHandles directory structure/traversal.dlfcn.hHandles run-time dynamic loading.dlg_colors.hColor attribute definitions.dlg_config.hSet of definitions generated by configure.dlg_keys.hHandles run-time binding support for dialog.elf.hDefines the format of executable and linking format (ELF) files.endian.hHandles conversion of integer values to/from Big/Little endian byte ordering.envz.hArgz vectors with additional constraints.err.hUtility functions for error messages.error.hDeclaration for error-reporting function.execinfo.hHandles whether one error is printed per line.fcntl.hManipulate file descriptor.fmtmsg.hMessage display handling.fnmatch.hFile/pathname match handling.fstab.hDefines fstab struct handling internal representation of files.fts.hHandles traversal of a file hierarchy.ftw.hHandles walk through a directory tree._G_config.hDefines various configuration parameters.gconv.hAssists in writing additional gconv modules.getopt.hParsing command line arguments.gettext-po.hProcessing of PO files.glob.hDefines structures and symbolic constants used by the glob() function.gnu-versions.h Defines GNU version macros.grp.hDeclares the group database access structure.gshadow.hDeclares types and functions for shadow group suite.iconv.hFacilitates character encoding conversion for certain applications.ifaddrs.hHandles retrieval of networking interface addresses.langinfo.hContains language information constants.lastlog.hDefines the struct lastlog in 4.3BSDlibgen.hDefinitions for pattern matching functions.libintl.hMessage catalogs for internationalization.link.hData structure for communication from the run-time dynamic linker for loaded ELF shared objects.malloc.hPrototypes and definition for malloc implementation.mcheck.hDefine debugging hooks for malloc.memory.hManagement of dynamic memory.mntent.hUtilities for reading/writing fstab, mtab, etc.monetary.hMonetary value formatting functions.mqueue.hDefines type for message queue descriptors.netdb.hDefinitions for network database operations.nl_types.hDefines various data types.nss.hHandles NSS lookups.obstack.hDefines object stack macros.paths.hDefines PATH macros.poll.hDefinitions for the poll function.printf.hPrintf handler extension.pthread.hHandles threads.pty.hDeclares the functions openpty and forkpty.pwd.hDefines a password structure.re_comp.hDefines regular expression matching functions.regex.hDefinitions for data structures and routines for the regular expressions library.regexp.hRegular expression declarations.resolv.hDefines types for queries.sched.hExecution scheduling.search.hDeclarations for System V style searcing functions.semaphore.hDefines type for performed semaphore operations.shadow.hDeclarations of types and functions for shadow password suite.spawn.hDefinitions for POSIX spawn interface.stab.hIndicate the GNU stab.h is in usestdc-predef.hDefines macros for the entirety of the translation unit.stdio_ext.hDefines functions that access FILE structureinternals.strings.hDefines non-standard string functions.stropts.hStreams interface.sudo_plugin.hSudo Plugin interface.syscall.hLinux syscall interface.sysexits.hDefines exit codes for programs.syslog.hDefinitions for system error logging.tar.hExtended tar format from POSIX.1termios.hDefines values for terminal I/O interfaces.thread_db.hInterface for debugging.ttyent.hDefines tty terminal types.ucontext.hDefines user context types.ulimit.hDefines constants for ulimit function.unistd.hDefines miscellaneous symbolic constants and types.utime.hSet file access and modification times.utmp.hInformation about user currently using system.utmpx.hUser accounting database definitions.values.hOld compatibility names for limits.h and float.h constants.wordexp.hDefines word expansion types.xlocale.hDefinition of locale datatype. EX-2-宏隐藏函数

宏隐藏函数指的是,头提供的api函数是宏函数assert,宏assert内部调用了__assert(你可以调用,但是不应该调用)

使用宏隐藏时,函数当然必须在宏前声明

  • 因为宏调用了函数
  • 如果反过来,assert会展开__assert
# define assert(expr)							
  ((expr)								
   ? __ASSERT_VOID_CAST (0)						
   : __assert_fail (__STRING(expr), __FILE__, __LINE__, __ASSERT_FUNCTION))



extern void __assert (const char *__assertion, const char *__file, int __line)
     __THROW __attribute__ ((__noreturn__));

EX-4-C99标准中为未来库预留的标识符(“future library directions”)

大致意思就是,这些标识符你不要用作自己的标识符

complex.h

    Function names:
    cerf
    cerfc
    cexp2
    cexpm1
    clog10
    clog1p
    clog2
    clgamma
    ctgamma

    And the same names suffixed with f or l.

ctype.h

    Function names:
    Any names that begin with is or to, and a lowercase letter may be added
    to the declarations in the ctype.h header.

errno.h
    Macro names:
    Names that begin with E and a digit, or E and an uppercase letter.

inttypes.h
    Macro names:
    Names that begin with PRI or SCN followed by any lowercase letter.
    X may be added to any macros defined in the inttypes.h header.

locale.h
    Macro names:
    Names that begin with LC_ and an uppercase letter.

signal.h
    Macro names:
    Names that begin with either SIG and an uppercase letter or SIG_ and an
    uppercase letter.

stdint.h
    Typedef names:
    Names beginning with int or unit and ending with _t.

    Macro names:
    Names begging with INT or UINT and ending with _MAX, _MIN, or _C.

stdio.h
    Lowercase letters may be added to the conversion specifiers and length
    modifiers in fprintf and fscanf. Other characters may be used in extensions.

stdlib.h
    Function names:
    Names that begin with str and a lowercase letter.

string.h
    Function names:
    Names that begin with str, mem, or wcs and a lowercase letter.

wchar.h
    Function names:
    Names that begin with wcs and a lowecase letter.

    Lowercase letters may be added to the conversion specifiers and length
    modifiers in fwprintf and fwscanf. Other characters may be used in
    extensions.

wctype.h
    Function names:
    Names that begin with is or to and a lowercase letter.
EX-5-下面的islower宏为什么不符合标准?

#define islower(c) ((c) >= 'a' && (c) <= 'z')

标准

  • 谨慎全面地使用括号保护好
  • 每个参数仅被求值一次

这种写法,参数c出现了两次
因此如果c是一个有副作用的表达式,表达式会引起两次副作用

比如islower(c++)

EX-6-中的宏技巧
  • C标准声明:一个文件include了标准头后,不允许重用里声明的宏
    • 因为你会修改掉这个宏
    • 你自己的项目,宏要带项目前缀,避免冲突
    • 下划线打头的,然后是大写字母的宏,被用作标准库内部保留用
    • 标准库使用下划线来避免冲突,这样你就可以定义无下划线的宏自己用
  • 包含一个静态数组_ctype,有什么用?
    • _ctype是把ascii码映射到它的一些flag(用bit-field)
    • 比如是否是数字(isdigit),是否是字母(isalphabetic)等等
  • 为什么要用数组实现这些宏?
    • 如前所说,一般的UPPER实现会对c求值两次
    • 使用查表法不需要
    • 而且更加快
  • 我看了一下源码
    • 这些bool信息被手工写入表格
    • 访问的方法就是索引字符,然后用掩码取出字段
EX-7-常见功能使用哪个标准函数?
  • 日期:time
  • 字符是否是数字:ctype
  • int上限:kunuts
  • 向上取整浮点数:math
  • 一个字符有多少位?:limits
  • 一个浮点数的有效位个数:float
  • 在字符串中查找特定字符:string
  • 打开文件:stdio

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5691988.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存