在gcc4.4.5中,我发出以下代码的警告.
char *f(void)
{
char c;
return &c;
}
Run Code Online (Sandbox Code Playgroud)
但是,当我使用临时指针时,不再有警告(即使行为是错误的).
char *f(void)
{
char c;
char *p = &c;
return p;
}
Run Code Online (Sandbox Code Playgroud)
我听说在C中指针分析很困难,但可以gcc警告这样的代码吗?
我想知道是否存在以下区别:
char s[32] = "";
Run Code Online (Sandbox Code Playgroud)
和:
char s[32] = {0};
Run Code Online (Sandbox Code Playgroud)
谢谢.
从C11草案:
C11(n1570),§K.3.5.1.1该
tmpfile_s功能
errno_t tmpfile_s(FILE * restrict * restrict streamptr);
restrict这里有资格赛的目的是什么?
因为没有其他参数,编译器能够知道streamptr没有别名restrict,不是吗?
引用C11标准:
阵列子装(第6.5.2.1节)
下标操作符的定义
[]是E1[E2]相同(*((E1)+(E2))).
我想知道为什么围绕E1必要的括号(它们在C89标准中缺失),即哪个表达式可以(*(E1+(E2)))不同(*((E1)+(E2)))?
从这篇文章.
对于变量声明为另一个用途
register,并const是抑制该变量的任何非本地的改变,甚至槽利用其地址,然后浇铸指针.即使你认为自己永远不会这样做,一旦你将一个指针(即使有一个const属性)传递给其他函数,你也永远无法确定这可能是恶意的并且改变你脚下的变量.
我不明白我们如何const通过指针修改变量的值.是不是未定义的行为?
const int a = 81;
int *p = (int *)&a;
*p = 42; /* not allowed */
Run Code Online (Sandbox Code Playgroud) 考虑以下代码:
struct s { /* ... */ };
void f(struct s x) { /* ... */) /* (1) */
/* or */
void f(const struct s *x) { /* ... */ } /* (2) */
Run Code Online (Sandbox Code Playgroud)
什么时候struct s有合适的尺寸,在这种情况下我们应该更喜欢第一种形式?
这是如何strtol根据§ 7.22.1.4C11(n1570)声明:
#include <stdlib.h>
long int strtol (const char *restrict nptr,
char **restrict endptr,
int base);
Run Code Online (Sandbox Code Playgroud)
据我所知,restrict关键字意味着左边界引用的对象*nptr只能使用它或直接从中获取的值.
但是,许多程序员,甚至是经验丰富的程序员,都strtol以下列方式使用:
#include <stdlib.h>
strtol (p, &p, 10);
Run Code Online (Sandbox Code Playgroud)
在那种情况下,**endptr == **&p == *p == *nptr行为是未定义的.这样对吗?
我正在寻找一些关于宏的代码,我找到了这样的代码,对于宏«va_start»:
#define __va_argsiz(t) \
(((sizeof(t) + sizeof(int) - 1) / sizeof(int)) * sizeof(int))
#define va_start(ap, pN) \
((ap) = ((va_list) (&pN) + __va_argsiz(pN)))
Run Code Online (Sandbox Code Playgroud)
我想知道«__va_argsiz»功能的目标究竟是什么.是对齐限制吗?
谢谢 !
我正在为Rabin-Karp算法寻找有效的哈希函数.这是我的实际代码(C编程语言).
static bool f2(char const *const s1, size_t const n1,
char const *const s2, size_t const n2)
{
uintmax_t hsub = hash(s2, n2);
uintmax_t hs = hash(s1, n1);
size_t nmax = n2 - n1;
for (size_t i = 0; i < nmax; ++i) {
if (hs == hsub) {
if (strncmp(&s1[i], s2, i + n2 - 1) == 0)
return true;
}
hs = hash(&s1[i + 1], i + n2);
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
我考虑了一些Rabin-Karp C实现,但所有代码之间存在差异.所以我的问题是:Rabin-Karp哈希函数应该具有哪些特征?
我正在测试一个带有gcc 4.4.5和-Wtype-limits选项的小代码片段。
#include <assert.h>
#include <limits.h>
#include <stdint.h>
int main(void)
{
/* With other values of v, the behavior of the compiler is the same. */
uint16_t v = 0;
assert((unsigned int)INT_MAX < (unsigned int)v); /* l. 7 */
return 0;
}
Run Code Online (Sandbox Code Playgroud)
然后,编译器向我抛出以下警告:
main.c:7: warning: comparison is always false due to limited range of data type
但是,据我所知,INT_MAX可能等于+32767(来自C11 (n1570), § 5.2.4.2.1 Sizes of integer types<limits.h>)。在这种情况下,变量v将能够保存 value INT_MAX+1,并且表达式 …
c ×10
c++ ×2
c11 ×2
gcc ×2
algorithm ×1
arrays ×1
const ×1
gcc-warning ×1
performance ×1
pointers ×1
pseudocode ×1
rabin-karp ×1
restrict ×1
strtol ×1
struct ×1
subscript ×1
types ×1