我一直认为这const char **x是一个正确的类型,用于动态分配的const字符串数组,如下所示:
#include <stdlib.h>
int main()
{
const char **arr = malloc(10 * sizeof(const char *));
const char *str = "Hello!";
arr[0] = str;
free(arr);
}
Run Code Online (Sandbox Code Playgroud)
但是,在使用VS2017编译此代码时,我会收到此警告free:
warning C4090: 'function': different 'const' qualifiers
Run Code Online (Sandbox Code Playgroud)
我的代码有问题吗?FWIW,当我和GCC一起编译时,即使有了,也没有任何警告-Wall -Wextra -pedantic.
假设我有一个函数,它接受一个结构数组,定义如下:
void Foo(struct MyStruct *s, int count) {
for (int i = 0; i < count; ++i) {
// Do something with s[i]
}
}
Run Code Online (Sandbox Code Playgroud)
以下两个片段是否保证以相同的方式运行?
struct MyStruct s;
s.x = 0;
s.y = 0;
Foo(&s, 1);
Run Code Online (Sandbox Code Playgroud)
与
struct MyStruct s[1]; // stack-allocated array of size 1
s[0].x = 0;
s[0].y = 0;
Foo(s, 1);
Run Code Online (Sandbox Code Playgroud) 我有一个C++类,它会在构造函数失败时抛出异常.如何保持此类的本地实例(不使用new)并处理任何可能的异常,同时保持try块范围尽可能小?
基本上,我正在寻找以下Java习惯用语的C++等价物:
boolean foo() {
Bar x;
try {
x = new Bar();
} catch (Exception e) {
return false;
}
x.doSomething();
return true;
}
Run Code Online (Sandbox Code Playgroud)
我不想捕获异常x.doSomething(),只有构造函数.
我想我正在寻找的是一种分离声明和初始化的方法x.
是否可以在不使用堆分配和指针的情况下实现此目的?
说我有以下C代码:
int32_t foo(int32_t x) {
return x + 1;
}
Run Code Online (Sandbox Code Playgroud)
这是未定义的行为时x == INT_MAX.现在说我用内联汇编代替了:
int32_t foo(int32_t x) {
asm("incl %0" : "+g"(x));
return x;
}
Run Code Online (Sandbox Code Playgroud)
问题:内联汇编版本何时仍会调用未定义的行为x == INT_MAX?或者未定义的行为仅适用于C代码?
出于好奇,我今天尝试运行此代码(使用 编译gcc -m32 1.c):
int main(void)
{
// EB is the opcode for jmp rel/8
// FE is hex for -2
// So this is essentially an infinite loop
((void(*)(void))"\xEB\xFE")();
}
Run Code Online (Sandbox Code Playgroud)
......它奏效了!没有段错误,程序(正确?)进入无限循环。查看反汇编 ( objdump -d a.out),您可以看到对...的调用,无论地址是什么0x8048480:
080483d6 <main>:
....
80483e7: b8 80 84 04 08 mov $0x8048480,%eax
80483ec: ff d0 call *%eax
....
Run Code Online (Sandbox Code Playgroud)
objdump -s -j .rodata a.out 给出:
Contents of section .rodata:
8048478 03000000 01000200 ebfe00 ...........
~~~~
Run Code Online (Sandbox Code Playgroud)
所以它确实在执行存储在.rodatasection中的字符串。所以我跑了readelf --sections a.out …
我有以下代码:
#include <iostream>
void f(int &x) {
x = 5;
}
int main() {
int x;
f(x);
std::cout << x << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
此代码是否在C++中调用未定义的行为?g ++编译它没有任何警告,代码打印出来5(如预期的那样?).
我想了解以下之间的区别:
try:
raise Exception("wat")
except Exception:
extype, exc, tb = sys.exc_info()
traceback.print_exception(extype, exc, tb)
Run Code Online (Sandbox Code Playgroud)
和:
try:
raise Exception("wat")
except Exception as exc:
extype = type(exc)
tb = exc.__traceback__
traceback.print_exception(extype, exc, tb)
Run Code Online (Sandbox Code Playgroud)
是否存在type(exc)和exc.__traceback__与 返回的值不同的情况sys.exc_info()?如果不是,我什么时候应该选择其中一种而不是另一种?当我对此进行测试时(Python 3.7),返回的对象在引用上是相同的。
查看 CPython 中的实现exc_info(),第一个返回值(异常类型)似乎是通过调用PyExceptionInstance_Class获得的,这与type(exc). 但是,我无法找到如何设置回溯。
(FWIW我知道速记traceback.print_exc(),这与这个问题无关)
我正在为我的hashmap数据结构设计一个迭代器接口.目前的设计如下:
// map.h
typedef struct map_iterator map_iterator;
// map.c
struct map_iterator
{
// Implementation details
};
// client.c
map *m = map_new();
map_iterator *it = map_iterator_new(m);
void *key, *value;
while (map_iterator_next(it, &key, &value)) {
// Use key, value
}
map_iterator_free(it);
Run Code Online (Sandbox Code Playgroud)
但是,这需要为迭代器对象分配堆,并且客户端必须记住在迭代器完成时释放迭代器.如果我map_iterator_new在堆栈上返回迭代器,代码如下所示:
// map.h
typedef struct map_iterator
{
// Implementation details
};
// client.c
map *m = map_new();
map_iterator it = map_iterator_new(m);
void *key, *value;
while (map_iterator_next(&it, &key, &value)) {
// Use key, value
}
Run Code Online (Sandbox Code Playgroud)
但是,这要求我将map_iterator结构的定义提供给客户端代码(否则我会得到不完整的类型错误).我想隐藏这个定义并仅提供声明.
有没有办法实现这个目标?本质上,我正在寻找一种方法来告诉客户端代码"这个结构占用了X个字节,因此你可以在堆栈上分配它,但我不会告诉你如何访问它的成员". …
我试图在图像上绘制带有反色的十字准线("加号"),以显示图像中所选点的位置.我是这样做的:
private static void DrawInvertedCrosshair(Graphics g, Image img, PointF location, float length, float width)
{
float halfLength = length / 2f;
float halfWidth = width / 2f;
Rectangle absHorizRect = Rectangle.Round(new RectangleF(location.X - halfLength, location.Y - halfWidth, length, width));
Rectangle absVertRect = Rectangle.Round(new RectangleF(location.X - halfWidth, location.Y - halfLength, width, length));
ImageAttributes attributes = new ImageAttributes();
float[][] invertMatrix =
{
new float[] {-1, 0, 0, 0, 0 },
new float[] { 0, -1, 0, 0, 0 },
new float[] { …Run Code Online (Sandbox Code Playgroud)