这是我第一次在这里提问,所以这里是上下文:
我工作的这个项目是保密的,而且相当复杂,然而,它是一个混合了C,C++和PostgreSQL,这是我能说的全部。 然而,这里有一个最小的工作示例(实际上编译良好),但在生成目标代码后会抛出一个错误:
“
#include <iostream>
using namespace std;
typedef size_t Size;
#define INDEX_SIZE_MASK 0x1FFF
typedef struct IndexTupleData
{
//ItemPointerData t_tid; /* reference TID to heap tuple */
/* ---------------
* t_info is laid out in the following fashion:
*
* 15th (high) bit: has nulls
* 14th bit: has var-width attributes
* 13th bit: AM-defined meaning
* 12-0 bit: size of tuple
* ---------------
*/
unsigned int t_info; /* various info about tuple */
} IndexTupleData; /* MORE DATA FOLLOWS AT END OF STRUCT */
typedef IndexTupleData *IndexTuple;
#define IndexTupleDSize(itup) ((Size) ((itup).t_info & INDEX_SIZE_MASK))
int main ()
{
IndexTuple itup;
(*itup).t_info = 1;
cout << ((*itup).t_info) << endl;
//cout << *itup.t_info << endl;
//int itemsz = IndexTupleDSize(*itup);
//cout << itup.t_info << endl;
//cout << *itup.t_info << endl;
return 0;
}
“
虽然编译/链接非常简单:
G++-C indextupledsize.cpp
G++-O I索引图大小。O
。/I
然后,它抛出:
Segmentation fault (core dumped)
我知道,根据指针在C语言中的分段错误(核心转储)错误和在谷歌发现的许多相关主题; 问题是我想访问一个不允许我访问的内存区域。 另一个提示是使用箭头运算符(itup->t_info而不是*itup.t_info),两个块代码编译但最终结果是相同的。。。
你能解释一下为什么以及它是如何抛出这个错误的吗?
您必须分配要访问的对象,并在取消引用指针之前分配它们的指针。
int main ()
{
IndexTuple itup;
itup = new IndexTupleData; // allocate object and assign its pointer
(*itup).t_info = 1;
cout << ((*itup).t_info) << endl;
delete itup; // delete allocated object
return 0;
}