我有一个如下所示的程序:
double[4][4] startMatrix;
double[4][4] inverseMatrix;
initialize(startMatrix) //this puts the information I want in startMatrix
Run Code Online (Sandbox Code Playgroud)
我现在想要计算startMatrix的反转并将其放入inverseMatrix.我有一个用于此目的的库函数,其原型如下:
void MatrixInversion(double** A, int order, double** B)
Run Code Online (Sandbox Code Playgroud)
取A的倒数并将其放入B.问题是我需要知道如何将double [4] [4]转换为双**以赋予函数.我尝试过"明显的方式":
MatrixInversion((double**)startMatrix, 4, (double**)inverseMatrix))
Run Code Online (Sandbox Code Playgroud)
但这似乎不起作用.这实际上是正确的方法吗?
出于调试目的,我经常将指针值(大多数this)输出到qDebug:
qDebug("pointer of current object = 0x%08x",this);,使用"%08x"作为格式字符串,只是this作为参数传递.
如何将指针值转换为QString?
这是我到目前为止所得到的:
char p = (char)this;
return QString("0x%1").arg(p, 8, '0');
Run Code Online (Sandbox Code Playgroud)
但编译器似乎并没有弄清楚如何处理该值.char在这种情况下铸造是否正确?或者更安全的方法是什么?
使用Visual C++和Qt 4.7.4.
编辑
qulonglong按建议使用
qulonglong p = (qulonglong)this;
return QString("0x%1").arg(p, 8, '0');
Run Code Online (Sandbox Code Playgroud)
产生编译器错误消息(错误C2666).
我有两个代码块new[]和delete[]:
1)
#include <string>
int main()
{
std::string *p = new std::string[0];
delete[] p;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
2)在这种情况下,我只是std::string改为int
int main()
{
int *p = new int[0];
delete[] p;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:
为什么第一个程序崩溃时出现以下消息(在linux环境中):
Segmentation fault (core dumped)
Run Code Online (Sandbox Code Playgroud)
但第二个程序运行良好没有任何错误?
编辑
编译: g++ (Ubuntu/Linaro 4.7.2-2ubuntu1) 4.7.2
我只是使用g++没有任何参数来编译它.
如果是编译器错误,它们是否会根据标准崩溃?
这是有效的 C++ 吗?
int main() {
constexpr auto sz = __func__ - __func__;
return sz;
}
Run Code Online (Sandbox Code Playgroud)
GCC 和 MSVC 认为可以,Clang 认为不是:Compiler Explorer。
所有编译器都同意这个是可以的:Compiler Explorer。
int main() {
constexpr auto p = __func__;
constexpr auto p2 = p;
constexpr auto sz = p2 - p;
return sz;
}
Run Code Online (Sandbox Code Playgroud)
Clang 再次不喜欢这个,但其他人都可以:编译器资源管理器
int main() {
constexpr auto p = __func__;
constexpr auto p2 = __func__;
constexpr auto sz = p2 - p;
return sz;
}
Run Code Online (Sandbox Code Playgroud)
这里有什么?我认为对无关指针的算术是未定义的行为,但__func__返回相同的指针,不是吗?我不确定,所以我想我可以测试一下。如果我没记错的话, …
这似乎表明该程序可能具有未定义的行为,至少因为我们不能保证键类型的严格弱排序:
#include <map>
int main()
{
int x = 0, y = 1;
bool arbitrary = false;
std::map<int*, bool> m{
{&x, arbitrary},
{&y, arbitrary}
};
}
Run Code Online (Sandbox Code Playgroud)
更一般地说,我们可以说带有指针键的映射是一个危险的*命题吗?或者有什么特别的规则我们可以依靠?
* 从学术上讲,即;实际上,我不知道主流实现实际上会在比较任意指针时引发地狱。
我不想实现以下代码 - 检查指针是否为null或不为null.如果指针指向对象,则对该对象执行操作,否则 - 跳过该代码块.
我的代码:
ref class EchoClient {
private:
GameMatrix^ gameMatrix;
public:
EchoClient(void);
EchoClient(GameMatrix^);
void do();
};
EchoClient::EchoClient(void)
{
this->gameMatrix = NULL;
}
EchoClient::EchoClient(gameMatrix)
{
this->gameMatrix = gameMatrix;
}
void EchoClient::do() {
if(this->gameMatrix != NULL)
{
this->gameMatrix->redrawMatrix();
}
}
Run Code Online (Sandbox Code Playgroud)
错误:
error C2446: '!=' : no conversion from 'int' to 'GameMatrix ^' k:\visual studio 2010\Projects\EchoClient3WS\EchoClient3WS\EchoClient.cpp 106
Run Code Online (Sandbox Code Playgroud)
任何解决方案
我对指针和它们占用的字节有点困惑.在我的教科书中,它首先说明16位系统上的指针占用2个字节,32位系统占用4个字节,64位系统占用8个字节,依此类推.然后是10行,它表示指针占用了许多字节,这是保存地址所需的.这是我的问题:
linux内核(和其他地方)中常用的宏container_of是(基本上)定义如下:
#define container_of(ptr, type, member) (((type) *)((char *)(ptr) - offsetof((type), (member))))
Run Code Online (Sandbox Code Playgroud)
在给定指向其中一个成员的指针的情况下,它基本上允许恢复"父"结构:
struct foo {
char ch;
int bar;
};
...
struct foo f = ...
int *ptr = &f.bar; // 'ptr' points to the 'bar' member of 'struct foo' inside 'f'
struct foo *g = container_of(ptr, struct foo, bar);
// now, 'g' should point to 'f', i.e. 'g == &f'
Run Code Online (Sandbox Code Playgroud)
但是,并不完全清楚其中包含的减法是否container_of被视为未定义的行为.
一方面,因为barinside struct foo只是一个整数,所以只*ptr应该是有效的(以及ptr + 1).因此,container_of有效地产生一个表达式 …
我们编写了一个程序,通过它我们试图找到一个常量的地址.有可能这样做吗?
package main
func main() {
const k = 5
address := &k
}
Run Code Online (Sandbox Code Playgroud)
它给出了一个错误,任何人都可以告诉我们如何找到常量的地址?
我有一个工厂方法,它创建一堆对象并返回指向它们的指针.对象的所有权转移给调用者:
std::vector<animal*> create_zoo();
Run Code Online (Sandbox Code Playgroud)
这有效,但容易出现内存泄漏.
auto zoo = create_zoo();
Run Code Online (Sandbox Code Playgroud)
向量在堆栈上并自动清理,包含的对象不是.
返回各种子类型的对象.撤消值而不是指针是行不通的.
我在考虑使用
std::vector<std::unique_ptr<animal> > create_zoo();
Run Code Online (Sandbox Code Playgroud)
但是unique_ptr没有复制语义,我返回了vectorby值,理论上它创建了一个副本.
我可以放在vector堆上以避免这种情况
std::unique_ptr<std::vector<std::unique_ptr<animal> > > create_zoo();
Run Code Online (Sandbox Code Playgroud)
但这太荒谬了.
这也应该有效:
std::vector<std::shared_ptr<animal> > create_zoo();
Run Code Online (Sandbox Code Playgroud)
这可行,但它并没有真正转移所有权.调用者必须假设可能有其他指向对象的指针.
我愿意接受建议.不需要std::vector.我只是在寻找一种实现工厂的好方法,该工厂使用现代c ++返回多个对象的所有权.我现在正在避免提振.我正在尝试探索新的c ++ 11内容.