我有一个如下所示的程序:
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)
但这似乎不起作用.这实际上是正确的方法吗?
由于遗留原因,我需要使用侵入式指针,因为我需要能够将原始指针转换为智能指针.
但是我注意到没有弱的侵入式指针可以提升.我确实在boost线程列表上找到了关于它的讨论,但没有具体的.
有谁知道弱侵入指针的线程安全实现?
谢谢Rich
出于调试目的,我经常将指针值(大多数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).
考虑以下程序:
#include <string>
struct S {
S (){}
private:
void *ptr = nullptr;
std::string str = "";
};
int main(){}
Run Code Online (Sandbox Code Playgroud)
在使用-Weffc++GCC 4.7.1 编译时,这将吐出:
warning: 'struct S' has pointer data members [-Weffc++] warning: but does not override 'S(const S&)' [-Weffc++] warning: or 'operator=(const S&)' [-Weffc++]
这通常没问题,除了这个例子的几个东西:
如果我注释掉任何构造函数,指针声明或字符串声明,警告就会消失.这很奇怪,因为你认为指针就足够了,但事实并非如此.此外,将字符串声明更改为整数声明也会导致它消失,因此只有在有字符串(或可能是其他选择类)时才会出现.为什么在这种情况下警告会消失?
通常,当所有指针都在指向现有变量(通常由OS维护)时,会出现此警告.没有new,没有delete.当复制带有句柄的类时,我不想要深层复制.我希望两个句柄指向同一个内部对象(例如窗口).有没有办法让编译器实现这一点,而不会不必要地重载复制构造函数和赋值运算符,或完全禁用警告#pragma?当三法则甚至不适用时,为什么我首先被困扰?
我有两个代码块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) pointers ×10
c++ ×8
bit-fields ×1
boost ×1
c ×1
g++ ×1
iostream ×1
new-operator ×1
ostream ×1
qstring ×1
qt ×1
syntax-error ×1
this ×1
vba ×1
weak ×1