如果文件是常规文件(并且不是目录,管道等),我如何签入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进行比较,但它似乎无法通过不同的系统进行移植.
让x和y成为在主代码和中断代码之间共享的变量。
我的想法volatile是,它只是并且始终需要用于也在主代码中使用的硬件变量和中断变量。
通过禁用中断,主代码中的x和 的每次使用y都保证是原子的。
做x和y真正需要的是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中使用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) 我有 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) 使用!!xvs 有什么区别(bool)x吗?
假设__STDC_VERSION__ >= 199901L和#include <stdbool.h>
他们两个都保证结果是0或者1,并且没有溢出发生,无论大小和价值是x多少?
在整数和整数数组之间进行类型校正是否合法?
具体代码:
#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) 我碰巧需要将的结果与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_t和size_tPOSIX的文档。
相关问题:
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) 操作系统: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) 该packed属性应该放在声明中的什么位置?C2x(C++ 风格)和旧 GNU 属性的使用有什么区别吗?
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?)不这样做,但它是否已定义?
我有两个使用相同技巧和功能的程序,只有其中一个可以编译。
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) 我在使用以下代码时遇到问题:
#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;相反,它返回一个错误代码。为什么它可能在内部设置errno为EINVAL(22, Invalid argument)?
c ×12
c++ ×3
gcc ×3
type-punning ×2
arrays ×1
atomic ×1
attributes ×1
boolean ×1
casting ×1
class ×1
dirent.h ×1
docker ×1
filesystems ×1
getaddrinfo ×1
goto ×1
header-files ×1
interrupt ×1
linux ×1
multiarch ×1
opencv ×1
pointers ×1
posix ×1
signed ×1
sizeof ×1
sockets ×1
struct ×1
types ×1
unions ×1
unsigned ×1
volatile ×1
x86-64 ×1