http://c.biancheng.net/c/assert/ good 用断言来校验 参数 所有语言都有的进阶思想
在函数中使用断言来检查参数的合法性
在防错性程序设计中使用断言来进行错误报警
在c语言上尤其有用。 因为c语言 如果内存指针 有点错。或null 或越界。 直接 Segmentation fault 段错误
用assert 自己报错。good。 报错是一个技术点。。因为程序中的bug位置知道。已经解决了90%了。
字符串本质上是在内存地址上顺序存放的一组字节 。
https://zhuanlan.zhihu.com/p/409044316
char *
STRCAT (char *dest, const char *src)
{
strcpy (dest + strlen (dest), src);
return dest;
}
libc_hidden_builtin_def (strcat)
char *
STPCPY (char *dest, const char *src)
{
size_t len = strlen (src);
return memcpy (dest, src, len + 1) + len;
}
weak_alias (__stpcpy, stpcpy)
libc_hidden_def (__stpcpy)
libc_hidden_builtin_def (stpcpy)
#include <sysdeps/generic/memcopy.h>
#undef OP_T_THRES
#define OP_T_THRES 8
#undef BYTE_COPY_FWD
#define BYTE_COPY_FWD(dst_bp, src_bp, nbytes) \
do { \
int __d0; \
asm volatile(/* Clear the direction flag, so copying goes forward. */ \
"cld\n" \
/* Copy bytes. */ \
"rep\n" \
"movsb" : \
"=D" (dst_bp), "=S" (src_bp), "=c" (__d0) : \
"0" (dst_bp), "1" (src_bp), "2" (nbytes) : \
"memory"); \
} while (0)
#undef BYTE_COPY_BWD
#define BYTE_COPY_BWD(dst_ep, src_ep, nbytes) \
do \
{ \
int __d0; \
asm volatile(/* Set the direction flag, so copying goes backwards. */ \
"std\n" \
/* Copy bytes. */ \
"rep\n" \
"movsb\n" \
/* Clear the dir flag. Convention says it should be 0. */ \
"cld" : \
"=D" (dst_ep), "=S" (src_ep), "=c" (__d0) : \
"0" (dst_ep - 1), "1" (src_ep - 1), "2" (nbytes) : \
"memory"); \
dst_ep += 1; \
src_ep += 1; \
} while (0)
#undef WORD_COPY_FWD
#define WORD_COPY_FWD(dst_bp, src_bp, nbytes_left, nbytes) \
do \
{ \
int __d0; \
asm volatile(/* Clear the direction flag, so copying goes forward. */ \
"cld\n" \
/* Copy longwords. */ \
"rep\n" \
"movsl" : \
"=D" (dst_bp), "=S" (src_bp), "=c" (__d0) : \
"0" (dst_bp), "1" (src_bp), "2" ((nbytes) / 4) : \
"memory"); \
(nbytes_left) = (nbytes) % 4; \
} while (0)
#undef WORD_COPY_BWD
#define WORD_COPY_BWD(dst_ep, src_ep, nbytes_left, nbytes) \
do \
{ \
int __d0; \
asm volatile(/* Set the direction flag, so copying goes backwards. */ \
"std\n" \
/* Copy longwords. */ \
"rep\n" \
"movsl\n" \
/* Clear the dir flag. Convention says it should be 0. */ \
"cld" : \
"=D" (dst_ep), "=S" (src_ep), "=c" (__d0) : \
"0" (dst_ep - 4), "1" (src_ep - 4), "2" ((nbytes) / 4) : \
"memory"); \
dst_ep += 4; \
src_ep += 4; \
(nbytes_left) = (nbytes) % 4; \
} while (0)
网友评论