操作系统:Debian 9 (Linux 4.9)
编译器:GCC 8.2
目前我包括<stddef.h>(size_t定义在哪里)和<stdint.h>(定义了大多数整数类型),但我仍然没有ssize_t.
它在哪里定义?
根据 ISO C(任何版本),指定零大小的数组参数是否有效?
\n该标准似乎含糊不清。虽然很明显零大小的数组是无效的,但数组函数参数很特殊:
\nC23::6.7.6.3/6:
\n\n\n将参数声明为“类型数组”应调整为“指向类型的限定指针”,其中类型限定符(如果有)是在数组类型派生的 [ 和 ] 内指定的限定符。如果\n关键字 static 也出现在数组类型派生的 [ 和 ] 中,则对于每次调用该函数,\n相应实际参数的值应提供对数组的第一个\n元素的访问,\n至少为由 size 表达式指定的许多元素。\n
\n
只要您不使用static,之间指定的大小[]就会被有效忽略。据我了解引用的段落,编译器根本不允许对指针做出任何假设。
那么,下面的代码应该是符合要求的,对吧?
\nvoid h(char *start, char past_end[0]);\n\n#define size 100\nvoid j(void)\n{\n char dst[size];\n h(dst, dst+size);\n}\nRun Code Online (Sandbox Code Playgroud)\n我使用past_end[0]作为指向末尾一位的哨兵指针(而不是大小;在某些情况下它更舒服)。清楚[0]地表明这是一个过去的结束,而不是实际的结束,作为一个指针,读者可能会感到困惑。结束将被标记为end[1]为清楚起见,
GCC 认为它不符合:
\n$ gcc -Wall -Wextra -Wpedantic -pedantic-errors -std=c17 -S ap.c \nap.c:1:26: error: ISO C forbids zero-size array \xe2\x80\x98past_end\xe2\x80\x99 [-Wpedantic]\n 1 | void h(char *start, char …Run Code Online (Sandbox Code Playgroud) 字符串可以传递多长时间system()?
我知道POSIX的最小值是4096,但我想知道可以使用的实际尺寸。是否在任何标头中定义了任何宏,类似于FILENAME_MAX?
char cmd[SOME_MACRO];
...
system(cmd);
Run Code Online (Sandbox Code Playgroud) 我有一个用于UART的缓冲区,它以这种方式声明:
union Eusart_Buff {
uint8_t b8[16];
uint16_t b9[16];
};
struct Eusart_Msg {
uint8_t msg_posn;
uint8_t msg_len;
union Eusart_Buff buff;
};
struct Eusart {
struct Eusart_Msg tx;
struct Eusart_Msg rx;
};
extern volatile struct Eusart eusart;
Run Code Online (Sandbox Code Playgroud)
这里是填充缓冲区的函数(将使用中断发送):
void eusart_msg_transmit (uint8_t n, void *msg)
{
if (!n)
return;
/*
* The end of the previous transmission will reset
* eusart.tx.msg_len (i.e. ISR is off)
*/
while (eusart.tx.msg_len)
;
if (data_9b) {
memcpy((void *)eusart.tx.buff.b9, msg,
sizeof(eusart.tx.buff.b9[0]) * n);
} else {
memcpy((void *)eusart.tx.buff.b8, …Run Code Online (Sandbox Code Playgroud) 如果我在属于某个分支的特定提交上有一个标签(没有其他分支有此提交),并且我删除了该分支,那么该标签是否会永远保留(只要我不删除该标签)?
*-*-*---* (master)
\--x---* (foo)
Run Code Online (Sandbox Code Playgroud)
标签foobar指向由x.
我决定foo不再支持/需要该分支,并将其删除。
但有人想查看该特定标签,因为他依赖于我项目的特定版本。
这个标签会为他而存在吗git clone --branch foobar?
我今天开始在PIC16f88上编程,发现其寄存器的标题包含一个union只包含一个struct:
extern volatile unsigned char ANSEL __at(0x09B);
typedef union {
struct {
unsigned ANS0 :1;
unsigned ANS1 :1;
unsigned ANS2 :1;
unsigned ANS3 :1;
unsigned ANS4 :1;
unsigned ANS5 :1;
unsigned ANS6 :1;
};
} ANSELbits_t;
extern volatile ANSELbits_t ANSELbits __at(0x09B);
Run Code Online (Sandbox Code Playgroud)
它是否提供任何好处,以封闭struct内union即只包含struct?
它的访问量我想是完全相同的,因为它是一个简单的struct(因为它struct是匿名的):
ANSELbits.ANS4 = 0;
我想做这样的事情:
PHONY += bar
bar:
$(MAKE) foo_$@
PHONY += foo_%
foo_%:
cp somedir/%.a someotherdir/
.PHONY: $(PHONY)
Run Code Online (Sandbox Code Playgroud)
但%在配方(cp 部分)中没有对其进行评估。
我的解决方法是创建一个与我要使用的文件同名的虚假目标,但我更喜欢更好的解决方案:
PHONY += bar
bar:
$(MAKE) foo_$@
PHONY += foo_%
foo_%: %.a
@:
PHONY += %.a
%.a:
cp somedir/$@ someotherdir/
.PHONY: $(PHONY)
Run Code Online (Sandbox Code Playgroud)
有什么方法可以将 的扩展传递%给 shell 命令吗?
我需要以最有效(最快)的方式来弹出大小为128位的无符号变量。
尽管解决方案更便携,甚至更好。
首先,GCC中有两种类型,分别是__uint128_t和unsigned __int128。我猜他们最终还是一样,看不出有什么理由写丑陋的unsigned __int128东西,因此尽管它应该是新类型,但我更喜欢第一个,它与标准更加相似uint64_t。另外,英特尔拥有__uint128_t使用它的另一个原因(可移植性)。
我写了以下代码:
#include <nmmintrin.h>
#include <stdint.h>
static inline uint_fast8_t popcnt_u128 (__uint128_t n)
{
const uint64_t n_hi = n >> 64;
const uint64_t n_lo = n;
const uint_fast8_t cnt_hi = _mm_popcnt_u64(n_hi);
const uint_fast8_t cnt_lo = _mm_popcnt_u64(n_lo);
const uint_fast8_t cnt = cnt_hi + cnt_lo;
return cnt;
}
Run Code Online (Sandbox Code Playgroud)
这是绝对最快的选择吗?
编辑:
我想到了另一个选择,它可能会(或不会)更快:
#include <nmmintrin.h>
#include <stdint.h>
union Uint128 {
__uint128_t …Run Code Online (Sandbox Code Playgroud) 在 Debian 9 上,使用 GCC 8.2、2.27 libc6-dev:amd64:
strchr(以及许多其他函数)的手册页<string.h>具有以下原型:
char *strchr(const char *s, int c);
Run Code Online (Sandbox Code Playgroud)
const char *当它的源是 a 时,它如何返回非, const char *?
原型不应该是吗const char *strchr(const char *str, int c);?
如果文件是常规文件(并且不是目录,管道等),我如何签入C++?我需要一个函数isFile().
DIR *dp;
struct dirent *dirp;
while ((dirp = readdir(dp)) != NULL) {
if ( isFile(dirp)) {
cout << "IS A FILE!" << endl;
i++;
}
Run Code Online (Sandbox Code Playgroud)
我已经尝试将dirp-> d_type与(unsigned char)0x8进行比较,但它似乎无法通过不同的系统进行移植.