我正在阅读 C 标准中的以下内容:
(6.5.6 加法运算符)
9 当两个指针相减时,两个指针都指向同一个数组对象的元素,或者指向数组对象最后一个元素之后的一个;结果是两个数组元素的下标之差。
现在我想知道什么被认为是“数组对象”。更具体地说,我想知道下面这个愚蠢的例子是否合法?该分配的内存块是否被视为一个“数组对象”?
uint8_t *data = malloc(255);
uint8_t *end = data + 255;
ptrdiff_t size = end - data;
Run Code Online (Sandbox Code Playgroud) 看起来该typeof
运算符很可能会被下一个 C 标准所接受,我正在寻找是否有一种方法可以利用它来创建一个使用可移植 ISO-C 的宏,该宏可以获取传递给它的数组的长度或者如果将指针传递给它,则无法编译。通常,在使用不需要的类型时,可以使用泛型选择来强制编译器错误,方法是将其排除在泛型关联列表之外,但在这种情况下,我们需要一个默认关联来处理任何长度的数组,因此我尝试强制对我们不想要的类型的泛型关联产生编译器错误。下面是宏的示例:
#define ARRAY_SIZE(X) _Generic(&(X), \
typeof(&X[0]) *: sizeof(struct{_Static_assert(0, "Trying to get the array length of a pointer"); int _a;}), \
default: (sizeof(X) / sizeof(X[0])) \
)
Run Code Online (Sandbox Code Playgroud)
问题是,_Static_assert
即使选择的通用关联是默认关联,也会发生故障。为了简单起见,由于当前的问题与 C23 中引入的任何内容无关,我们将制作一个测试程序,该程序显式地拒绝指向 int 的指针:
#include <stdio.h>
#include <stdlib.h>
#define ARRAY_SIZE(X) _Generic(&(X), \
int **: sizeof(struct{_Static_assert(0, "Trying to get the array length of a pointer"); int _a;}), \
default: (sizeof(X) / sizeof(X[0])) \
)
int main(void) {
int x[100] = {0};
int *y = x;
int …
Run Code Online (Sandbox Code Playgroud) 我试图让 GDB 在变量更改时打印该变量的值。
给定一个示例程序,我想在它更改时获取x
in的值func
,但要让程序在没有提示的情况下继续:
#include <stdio.h>
#include <stdlib.h>
int func(int x, int y);
int main(void) {
int x = 5;
int y;
y = func(x, 4);
printf("%d\n", x);
printf("%d\n", y);
return EXIT_SUCCESS;
}
int func(int x, int y) {
y *= 2;
x += y;
return x;
}
Run Code Online (Sandbox Code Playgroud)
我尝试过的:
break func
commands
silent
watch x
commands
continue
end
continue
end
Run Code Online (Sandbox Code Playgroud)
虽然这将成功获取更改时的值x
,但问题是当离开 的范围时x
,gdb 将停止通知我它正在离开 的范围x
并且正在删除观察点。有没有什么方法可以设置 GDB 在自动删除观察点时继续执行而无需用户提示?
我遇到了这个问题:gdb:当局部变量上的观察点超出范围时不要中断 但是它从未收到解决方案。
我正在尝试使用_Generic
创建一个线程安全的strerror
函数,该函数与使用strerror_r
. XSI 变体返回一个int
并修改传递给它的缓冲区的内容,而 GNU 版本返回一个char *
.
strerror.c
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include "strerror.h"
static inline char *strerror_r_gnu(int errnum, char *buf, size_t buflen);
static inline char *strerror_r_xsi(int errnum, char *buf, size_t buflen);
static inline char *strerror_r_gnu(int errnum, char *buf, size_t buflen) {
// FUNKY CASTING TO SILENCE COMPILER WARNING BELOW
return (char *)(intptr_t)strerror_r(errnum, buf, buflen);
}
static inline char *strerror_r_xsi(int errnum, char *buf, size_t buflen) {
// Cast result …
Run Code Online (Sandbox Code Playgroud) 我需要找到一种方法来用随机数填充数组而没有重复,所以我写了这段代码并且它有效。我的问题是,这段代码有效吗,它真的没有重复吗?提前致谢!
#include <iostream>
#include <time.h>
#include <stdlib.h>
int main(void) {
srand(time(NULL));
std::size_t array_size=100;
int array[array_size];
for(int i=0;i<array_size;i++) {
array[i]=rand()%105+1;
for(int k=0;k<array_size;k++) { // Checks if there is a duplicate in the array //
if(i!=k) { // Don't check for the same array position //
if(array[i]==array[k]) { // If a duplicate is found,repeat the check process//
array[i]=rand()%105+1;
k=-1; // -1 so the for loop starts from zero //
}
}
}
}
return 0;
}
Run Code Online (Sandbox Code Playgroud) 假设我有一堆定义一些变量的宏 - 它们需要在代码的不同部分中以两种方式定义(是的,这是一个坏主意,但重构将需要很长的路要走).
是否有可能让下面的代码段工作,即打印4然后1?
#include <iostream>
#define ENABLE
#ifdef ENABLE
#define B 4
#define C 5
//imagine a bunch more here
#else
#define B 1
#define C 2
//imagine a bunch more here
#endif
int main()
{
std::cout << B << std::endl;
#pragma push_macro("ENABLE")
#undef ENABLE
std::cout << B << std::endl;
#pragma pop_macro("ENABLE")
return 0;
}
Run Code Online (Sandbox Code Playgroud)
通过具体定义B当然可以达到相同的效果,但如果我有一大块宏,那么这不是100%实用:
#include <iostream>
#define B 4
int main()
{
#pragma push_macro("B")
#undef B
#define B 1
std::cout << B << std::endl;
#pragma pop_macro("B")
std::cout …
Run Code Online (Sandbox Code Playgroud) 在 C++ 中是否可以从类成员函数返回类的元素?
namespace translation2d
{
class translation2d
{
public:
double x;
double y;
public:
translation2d()
{
x=0;
y=0;
}
public:
translation2d translateBy(translation2d other);
//being interpreted as a constructor?
}
translation2d translation2d::translateBy(translation2d other)
{//I need this member function to return an object of type translation2d
x+=other.x;
y+=other.y;
}
};
Run Code Online (Sandbox Code Playgroud)