因此,当我尝试访问内存中的特定指针时,我遇到了段错误。我已经将代码修剪为我认为需要的所有内容,但是如果我忘记了某些内容,请告诉我。
char memoryPool[100000];
static const int HEADER_NEXT = 4;
char* firstHole;
void initializeManager(void)
{
*( (void**)(memoryPool + HEADER_NEXT) ) = 0;
std::cout << "setting header next: " << (char**)(block + HEADER_NEXT) << std::endl;
firstHole = memoryPool;
}
int freeRemaining(void)
{
int result = 0;
char* block = firstHole;
while( block != 0 )
{
result += *( (int*)(block + HEADER_SIZE) );
std::cout << "running Result: " << result << std::endl;
std::cout << "header next: " << (char**)(block + HEADER_NEXT) << std::endl;
block = *( (char**)(block + HEADER_NEXT) ); // seg fault
}
return result;
}
这是终端输出:
setting header next: 0x107e760e4
Entering freeRemaining
running result: 65536
header next: 0x107e760e4
Segmentation fault: 11
logout
所以地址是相同的,但为什么我可以访问存储在该地址的值(0)?