小编Sou*_*osh的帖子

(为什么)使用未初始化的变量未定义行为?

如果我有:

unsigned int x;
x -= x;
Run Code Online (Sandbox Code Playgroud)

很明显,x 应该这样表达后是零,但到处看,他们说的行为,这种代码是不确定的,而不是仅仅值x(直到减法之前).

两个问题:

  • 这段代码的行为确实未定义吗?
    (例如,代码在兼容系统上崩溃[或更糟]?)

  • 如果是这样,为什么 C表示行为是未定义的,当非常清楚x这里应该为零时?

    即不在此定义行为给出的优势是什么?

很明显,编译器可以简单地使用它在变量中认为"方便"的任何垃圾值,并且它可以按预期工作......这种方法有什么问题?

c initialization undefined-behavior

77
推荐指数
6
解决办法
1万
查看次数

C11中_Generic的语法和示例用法

我听说C11添加了泛型.我已经google了一下,看了一些文章,了解到有一个新的关键字(_Generic)和所有.但我似乎无法把握住这一切.

它是类似C#中的泛型还是C++中的模板?任何人都可以简单解释一下C11的泛型定义,它的语法和一个简单的示例用法示例吗?

c generics c11

66
推荐指数
3
解决办法
3万
查看次数

这是一个释放记忆的好方法吗?

释放实例的函数struct Foo如下:

void DestroyFoo(Foo* foo)
{
    if (foo) free(foo);
}
Run Code Online (Sandbox Code Playgroud)

我的一位同事建议如下:

void DestroyFoo(Foo** foo)
{
    if (!(*foo)) return;
    Foo *tmpFoo = *foo;
    *foo = NULL; // prevents future concurrency problems
    memset(tmpFoo, 0, sizeof(Foo));  // problems show up immediately if referred to free memory
    free(tmpFoo);
}
Run Code Online (Sandbox Code Playgroud)

我看到NULL在释放后将指针设置为更好,但我不确定以下内容:

  1. 我们真的需要将指针分配给临时指针吗?它在并发性和共享内存方面有帮助吗?

  2. 将整个块设置为0以强制程序崩溃或至少输出具有显着差异的结果真的是一个好主意吗?

c memory free

55
推荐指数
2
解决办法
3848
查看次数

在C中打印指针

我试图用指针理解一些东西,所以我写了这段代码:

#include <stdio.h>

int main(void)
{
    char s[] = "asd";
    char **p = &s;

    printf("The value of s is: %p\n", s);
    printf("The direction of s is: %p\n", &s);

    printf("The value of p is: %p\n", p);
    printf("The direction of p is: %p\n", &p);

    printf("The direction of s[0] is: %p\n", &s[0]);
    printf("The direction of s[1] is: %p\n", &s[1]);
    printf("The direction of s[2] is: %p\n", &s[2]);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

用gcc编译时我得到以下警告:

$ gcc main.c -o main-bin -ansi -pedantic -Wall -lm
main.c: In function ‘main’: …
Run Code Online (Sandbox Code Playgroud)

c pointers

41
推荐指数
3
解决办法
15万
查看次数

memcpy或memmove可以返回与dest不同的指针吗?

该函数memmove定义如下:

void *memmove(void *dest, const void *src, size_t n);
Run Code Online (Sandbox Code Playgroud)

在Linux手册页中,它说:

返回值
memmove()函数返回指向dest的指针.

为什么函数只是定义为void memmove(…)总是返回其中一个输入参数?返回值可以与dest?不同?

或者返回值是否总是dest如此,只是能够以某种创造性的方式组合功能?

c return-value function-call memmove

38
推荐指数
3
解决办法
2584
查看次数

像C中的库一样增强

你能推荐我可以在C环境中使用的同行评审库(比如Boost for C++)吗?提供散列,线程,进程间通信,列表,智能内存管理的东西......

环境是嵌入式系统,不是一个非常小的系统,也不是PC!

c boost

31
推荐指数
4
解决办法
9236
查看次数

分配结构字段时,"错误:赋值给表达式,数组类型错误"(C)

我是初学C程序员,昨天我学习了C结构的使用以及这些结构可能应用于特定问题的解决方案.然而,当我尝试使用我的C IDE(Codeblocks 16.01)来学习C编程的这个方面时,我遇到了一个奇怪的问题.代码如下:

#include <stdio.h>

#define N 30

typedef struct{
     char name[N];
     char surname[N];
     int age;
} data;

int main() {
     data s1;
     s1.name="Paolo";
     s1.surname = "Rossi";
     s1.age = 19;
     getchar();
     return 0;
}
Run Code Online (Sandbox Code Playgroud)

在编译期间,编译器(Windows下的GCC 4.9.3-1)向我报告了一个错误

"错误:赋值给表达式,数组类型错误"

在指导上

s1.name="Paolo" 
s1.surname="Rossi" 
Run Code Online (Sandbox Code Playgroud)

如果我这样做的话

data s1 = {"Paolo", "Rossi", 19};
Run Code Online (Sandbox Code Playgroud)

有用.我究竟做错了什么?

c arrays string struct initialization

31
推荐指数
3
解决办法
15万
查看次数

printf(s)和printf("%s",s)之间的根本区别是什么?

问题很简单,s是一个字符串,我突然想到试图用printf(s)它来查看它是否可行,我在一个案例中得到警告而在另一个案例中没有警告.

char* s = "abcdefghij\n";
printf(s);

// Warning raised with gcc -std=c11: 
// format not a string literal and no format arguments [-Wformat-security]

// On the other hand, if I use 

char* s = "abc %d efg\n";
printf(s, 99);

// I get no warning whatsoever, why is that?

// Update, I've tested this:
char* s = "random %d string\n";
printf(s, 99, 50);

// Results: no warning, output "random 99 string".
Run Code Online (Sandbox Code Playgroud)

那么,什么是之间的基本差异printf(s),并printf("%s", s)和为什么会在短短的一个案例一个警告?

c string printf compiler-warnings format-specifiers

27
推荐指数
3
解决办法
2791
查看次数

intptr_t有什么用?

我知道它是一个整数类型,可以在不丢失数据的情况下强制转换为指针,但为什么我要这样做呢?使用整数类型有什么优势void*可以保存指针和THE_REAL_TYPE*指针算术?

编辑
标记为"已被询问"的问题没有回答这个问题.问题是如果使用intptr_t作为一般的替换void*是一个好主意,那里的答案似乎是"不要使用intptr_t",所以我的问题仍然有效:什么是一个很好的用例intptr_t?

c c++ pointers

25
推荐指数
5
解决办法
2万
查看次数

realloc和memcpy如何工作?

我有两个问题.

  1. 不要realloc()和memcpy()阵列中的复制项目到另一个的方式更快的不仅仅是遍历每个元素O(N)?如果答案是肯定的,那么您认为它的复杂性是什么?

  2. 如果分配的大小小于原始大小,是否realloc()将条目复制到其他位置,或者只是将它们保留,因为它们会减小数组的大小?

c memory-management realloc

24
推荐指数
4
解决办法
2万
查看次数