小编Cac*_*ito的帖子

如何检查文件是否是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进行比较,但它似乎无法通过不同的系统进行移植.

c c++ filesystems dirent.h

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

中断的易失性与内存屏障

xy成为在主代码和中断代码之间共享的变量。

我的想法volatile是,它只是并且始终需要用于也在主代码中使用的硬件变量和中断变量。

通过禁用中断,主代码中的x和 的每次使用y都保证是原子的。

xy真正需要的是volatile,还是足够使用它们来强制重装从RAM中的变量之前把内存屏障?

一种)

volatile bool x;
volatile int y[100];

int main(void)
{

        while (true) {
                disable_interrupts();
                if (x)
                        work(y);
                x = false;
                enable_interrupts();
        }
}
Run Code Online (Sandbox Code Playgroud)

乙)

bool x;
int y[100];

int main(void)
{

        while (true) {
                memory_barrier();
                disable_interrupts();
                if (x)
                        work(y);
                x = false;
                enable_interrupts();
        }
}
Run Code Online (Sandbox Code Playgroud)

目标是:

  • 让编译器优化work()

  • 能够使用标准库函数,例如memcpy()(那些不能与volatile变量一起使用)。

编辑:添加中断示例

interrupts.c

extern volatile? …
Run Code Online (Sandbox Code Playgroud)

c atomic interrupt volatile memory-barriers

5
推荐指数
1
解决办法
575
查看次数

通过`void *`和C ++`extern“ C”`函数调用在C中初始化C ++类

目的是在C中使用OpenCV3。OpenCV拥有C API,但很久以前就已弃用。

因此,我所做的是C ++中的一种抽象,它将所有指针都class cv::Something转换成void *我无法在C中取消引用的指针,但是可以在extern "C"完成工作的C ++ 函数之间传递。

为了使用这种抽象,我做了一些C函数,该函数应该从文件中读取图像并将其保存到新文件中:

#include "foo.h"

#include "libalx/extra/cv.h"


int foo(const char *restrict src, const char *restrict dest)
{
    void    *img;
    int     status;

    status  = -1;
    if (alx_cv_alloc_img(&img))
        return  -1;
    if (alx_cv_init_img(img, 1, 1))     // Segfault is here
        goto out_free;
    if (alx_cv_imread(img, src))
        goto out_free;
    if (alx_cv_imwrite(img, dest))
        goto out_free;
    status  = 0;
out_free:
    alx_cv_free_img(img);
    return  status;
}
Run Code Online (Sandbox Code Playgroud)

创建此抽象所需的文件如下:


cv.h

#pragma once    /* libalx/extra/cv.h */


#include <stddef.h>


__attribute__((nonnull)) …
Run Code Online (Sandbox Code Playgroud)

c c++ opencv pointers class

5
推荐指数
1
解决办法
76
查看次数

docker 清单创建注释并推入脚本

我有 DockerHub 为我的项目构建镜像x86_64。图像的命名如下:myname/project:version_architecture; 例如foo/bar:1.0.0_x86_64

然后我aarch64在 RPi4: 上构建映像foo/bar:1.0.0_aarch64,然后将其推送到 DockerHub。

现在我想要一个包含它们的清单,以便我可以foo/bar:1.0.0在任何地方使用它并且它可以工作。

AFAIK,以下命令应该有效(在 RPi 上运行):

docker build -t foo/bar:1.0.0_aarch64 .
docker push foo/bar:1.0.0_aarch64

docker manifest create foo/bar:1.0.0 foo/bar:1.0.0_aarch64
docker manifest annotate foo/bar:1.0.0 foo/bar:1.0.0_x86_64
docker manifest push foo/bar:1.0.0
Run Code Online (Sandbox Code Playgroud)

但由于某种原因,该annotate步骤有时会失败:

manifest for image foo/bar:1.0.0_x86_64 does not exist in foo/bar:1.0.0
Run Code Online (Sandbox Code Playgroud)

以下解决方法(通常)可以代替:

docker build -t foo/bar:1.0.0_aarch64 .
docker push foo/bar:1.0.0_aarch64

docker manifest create foo/bar:1.0.0 foo/bar:1.0.0_aarch64
docker manifest annotate foo/bar:1.0.0 foo/bar:1.0.0_x86_64
docker manifest create …
Run Code Online (Sandbox Code Playgroud)

docker docker-registry multiarch

4
推荐指数
1
解决办法
6379
查看次数

演员布尔:`!!`vs`(bool)`

使用!!xvs 有什么区别(bool)x吗?

假设__STDC_VERSION__ >= 199901L#include <stdbool.h>

他们两个都保证结果是0或者1,并且没有溢出发生,无论大小和价值是x多少?

c casting boolean

3
推荐指数
1
解决办法
79
查看次数

使用union在整数和数组之间键入修剪

在整数和整数数组之间进行类型校正是否合法?

具体代码:

#include <nmmintrin.h>
#include <stdint.h>

union   Uint128 {
    __uint128_t uu128;
    uint64_t    uu64[2];
};

static inline   uint_fast8_t    popcnt_u128 (__uint128_t n)
{
    const union Uint128 n_u     = {.uu128 = n};
    const uint_fast8_t  cnt_a   = _mm_popcnt_u64(n_u.uu64[0]);
    const uint_fast8_t  cnt_b   = _mm_popcnt_u64(n_u.uu64[1]);
    const uint_fast8_t  cnt     = cnt_a + cnt_b;

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

c arrays unions type-punning

3
推荐指数
1
解决办法
88
查看次数

将sizeof()的结果分配给ssize_t

我碰巧需要将的结果与sizeof(x)进行比较ssize_t

当然,GCC给出了一个错误(很幸运,我(我曾经用过-Wall -Wextra -Werror)),所以我决定做一个宏来拥有的签名版本sizeof()

#define ssizeof (ssize_t)sizeof
Run Code Online (Sandbox Code Playgroud)

然后我可以像这样使用它:

for (ssize_t i = 0; i < ssizeof(x); i++)
Run Code Online (Sandbox Code Playgroud)

问题是,我有什么保证SSIZE_MAX >= SIZE_MAX吗?我以为可悲的是,这永远不会成为现实。

或者至少是that sizeof(ssize_t) == sizeof(size_t),它将减少一半的值,但仍然足够接近。

我没有找到任何关系ssize_tsize_tPOSIX的文档。

相关问题:

应使用哪种类型遍历数组?

c posix sizeof

3
推荐指数
1
解决办法
130
查看次数

extern“ C”:有什么用和不需要什么?

extern "C"关于函数的问题已经存在,但是这个问题试图将其扩展到其他事物,例如变量等。

如果我有一个foo.hpp标头来使用foo.cC ++中的文件,则可以制作此模板,并用这个问题的简单示例进行填充:

/******************************************************************************
 ******* include guard { ******************************************************
 ******************************************************************************/
#ifndef FOO_HPP
#define FOO_HPP


/******************************************************************************
 ******* headers **************************************************************
 ******************************************************************************/
#include <cstdbool>
#include <cstddef>
#include <cstdint>


/******************************************************************************
 ******* typedefs *************************************************************
 ******************************************************************************/
/* `#if !defined(UINT128_MAX)` is to test if uint128_t already exists */
#if !defined(UINT128_MAX)
    typedef __uint128_t uint128_t;
    typedef __int128_t  int128_t;
#endif


/******************************************************************************
 ******* macros ***************************************************************
 ******************************************************************************/
#if !defined(UINT128_MAX)
    #define UINT128_MAX (~((uint128_t)0))
#endif
#if !defined(INT128_MAX)
    #define INT128_MAX  ((int128_t)(UINT128_MAX >> 1))
#endif
#if !defined(INT128_MIN)
    #define INT128_MIN  ((int128_t)(-INT128_MAX …
Run Code Online (Sandbox Code Playgroud)

c c++ header-files

2
推荐指数
1
解决办法
120
查看次数

有256位整数类型吗?

操作系统:Linux(Debian 10)

CC:GCC 8.3

CPU:i7-5775C

在GCC中有一个unsigned __int128/ __int128,但是有什么办法在GCC中有一个uint256_t/ int256_t

我读过一篇__m256i似乎来自英特尔的文章。我可以包含任何标头来获取它吗?

它像假设一样有用unsigned __int256吗?我的意思是,如果您可以为其分配/比较,比较,按位运算等。

它的等效符号是什么(如果有)?


编辑1:

我做到了:

#include <immintrin.h>
typedef __m256i uint256_t;
Run Code Online (Sandbox Code Playgroud)

并编译。如果可以进行一些操作,请在此处进行更新。


编辑2:

发现问题:

uint256_t   m;
ptrdiff_t   l = 5;

m = ~((uint256_t)1 << l);
Run Code Online (Sandbox Code Playgroud)

输出:

error: can’t convert a value of type ‘int’ to vector type ‘__vector(4) long long int’ which has different size
  m = ~((uint256_t)1 << l);
Run Code Online (Sandbox Code Playgroud)

c gcc types x86-64

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

声明中的 __attribute__((packed)) 和 [[gnu::packed]] 位置

packed属性应该放在声明中的什么位置?C2x(C++ 风格)和旧 GNU 属性的使用有什么区别吗?

c attributes gcc struct

2
推荐指数
1
解决办法
1363
查看次数

将正有符号整数输入为无符号整数(反之亦然)

union   Positive_Small {
    int8_t  s;
    uint8_t u;
};

union Positive_Small    x = {.s = 3};
union Positive_Small    y = {.u = 4};

assert(x.u == 3);
assert(y.s == 4);
Run Code Online (Sandbox Code Playgroud)

这是定义的行为吗?标准是否保证有符号整数类型的正范围与其无符号等效项具有相同的表示?

我想没有足够疯狂的实现(可能是 DS9K?)不这样做,但它是否已定义?

c unsigned signed language-lawyer type-punning

1
推荐指数
1
解决办法
84
查看次数

为什么“错误:仅在某些时候跳入语句表达”?

我有两个使用相同技巧和功能的程序,只有其中一个可以编译。

A)这一个可以编译,并且可以按预期工作:

#include <errno.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>


/*
 * int  mallocs(T **restrict p, ptrdiff_t nmemb);
 */
#define mallocs(ptr, nmemb) (                                       \
{                                                                   \
        ptrdiff_t   nmemb_  = (nmemb);                              \
        __auto_type ptr_    = (ptr);                                \
        int         err_;                                           \
                                                                    \
        err_    = 0;                                                \
        if (ptr_ == NULL) {                                         \
                errno   = EINVAL;                                   \
                err_    = EINVAL;                                   \
                goto ret_;                                          \
        }                                                           \
        if (nmemb_ < 0) {                                           \
                *ptr_   = NULL;                                     \
                errno   = EOVERFLOW;                                \
                err_ …
Run Code Online (Sandbox Code Playgroud)

c gcc goto gcc-statement-expression

0
推荐指数
1
解决办法
71
查看次数

getaddrinfo() 返回 0(成功)但将 errno 设置为 EINVAL (22)

我在使用以下代码时遇到问题:

#include <errno.h>
#include <stdlib.h>

#include <netdb.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>


[...]
    struct protoent *tcp;
    struct addrinfo hint = {0};
    struct addrinfo *addrs;
    int             status;

    tcp = getprotobyname("tcp");
    if (!tcp)
        return  -EINVAL;
    hint.ai_family      = AF_UNSPEC;
    hint.ai_socktype    = SOCK_STREAM;
    hint.ai_protocol    = tcp->p_proto;
// errno is 0
    status  = getaddrinfo("localhost", "30002", &hint, &addrs);
// errno is 22
    if (status)
        return  -labs(status);
[...]
Run Code Online (Sandbox Code Playgroud)

问题是,虽然它有效(我将它用于确实有效的 TCP 通信),但它不一致:

man getaddrinfo什么也没说errno;相反,它返回一个错误代码。为什么它可能在内部设置errnoEINVAL(22, Invalid argument)?

c sockets linux getaddrinfo

0
推荐指数
1
解决办法
395
查看次数