我有一个如下所示的程序:
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++没有任何参数来编译它.
如果是编译器错误,它们是否会根据标准崩溃?
为什么不能取位域的地址?
如何制作指向位字段的指针?
这是代码......
struct bitfield {
unsigned int a: 1;
unsigned int b: 1;
unsigned int c: 1;
unsigned int d: 1;
};
int main(void)
{
struct bitfield pipe = {
.a = 1, .b = 0,
.c = 0, .d = 0
};
printf("%d %d %d %d\n", pipe.a,
pipe.b, pipe.c, pipe.d);
printf("%p\n", &pipe.a); /* OPPS HERE */
// error: cannot take address of bit-field ...
return 0;
}
Run Code Online (Sandbox Code Playgroud) MyCustomObject * object=new MyCustomObject();
Run Code Online (Sandbox Code Playgroud)
假设我的许多类都使用了对象指针,但突然间我想改变指针的内容而不改变地址.
我错误地认为object = new MyCustomObject()会给对象一个新的指针地址吗?我想要新对象而不更改指针地址(是的,我将确保清理旧对象).
我在教自己C++,我对指针有点困惑(特别是在下面的源代码中).但首先,我继续向你展示我所知道的内容(然后将代码与此对比,因为我觉得好像存在一些矛盾).
我知道的:
int Age = 30;
int* pointer = &Age;
cout << "The location of variable Age is: " << pointer << endl;
cout << "The value stored in this location is: " << *pointer << endl;
Run Code Online (Sandbox Code Playgroud)
指针保存内存地址.使用间接(取消引用)运算符(*),可以访问存储在指针的内存位置的内容.在本书的代码中,我无法理解......
cout << "Enter your name: ";
string name;
getline(cin, name); //gets full line up to NULL terminating character
int CharsToAllocate = name.length() + 1; //calculates length of string input
//adds one onto it to adjust for NULL character
char* CopyOfName = new …Run Code Online (Sandbox Code Playgroud) 我正在玩弄溪流一点点,无法理解下面的内容.
这里我们有一个基本的ostream ptr,设置为不同的输出流,无论是cout,cerr还是a file.
// ostream ptr
std::ostream* outstream;
// set output ostream
void setOutput(std::ostream & os)
{
outstream = &os;
}
// write message to ostream
void writeData(const std::string & msg)
{
*outstream << msg << '\n';
}
int main (int argc, char * const argv[])
{
// init to std out
setOutput(std::cout);
writeData("message to cout");
setOutput(std::cerr);
writeData("message to cerr");
std::ofstream fileout("test.txt", std::ofstream::out | std::ofstream::app);
setOutput(fileout);
writeData("message to file");
//fileout.close();
setOutput(std::cout);
writeData("message2 to cout"); …Run Code Online (Sandbox Code Playgroud) 这是有效的 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)
更一般地说,我们可以说带有指针键的映射是一个危险的*命题吗?或者有什么特别的规则我们可以依靠?
* 从学术上讲,即;实际上,我不知道主流实现实际上会在比较任意指针时引发地狱。
pointers ×10
c++ ×8
bit-fields ×1
c ×1
c++17 ×1
constexpr ×1
iostream ×1
new-operator ×1
ostream ×1
qstring ×1
qt ×1
sorting ×1
syntax-error ×1
this ×1
vba ×1