【发布时间】:2020-07-22 14:34:27
【问题描述】:
这是我第一次在这里提问,所以这里是上下文:
我所从事的项目是机密且相当复杂的,但是,它是 C C++ 和 PostgreSQL 的混合体,我只能这么说。尽管如此,这是一个最小的 WORKING 示例(实际上可以正常编译),但在生成目标代码后会引发错误:
`
#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 IndexTupleDSize.o
./我
然后,它抛出:
Segmentation fault (core dumped)
我知道根据Pointers in C with Segmentation fault (core dumped) error 和在谷歌找到的许多相关主题;问题是我想访问一个不允许访问的内存区域。另一个提示是使用箭头运算符( itup->t_info 而不是 *itup.t_info ),两个块代码编译但最终结果相同......
您能否解释一下为什么以及如何引发此错误?
【问题讨论】:
-
(*itup).t_info = 1;,不。您只有一个不指向任何有效内存的指针。 -
itup是一个指针,这意味着它是一个存储另一个变量地址的变量。您希望存储其地址的另一个变量在哪里?
标签: c++ postgresql pointers struct