gpt4 book ai didi

c - 为什么我的指针会改变固定的数字?

转载 作者:行者123 更新时间:2023-11-30 15:15:11 25 4
gpt4 key购买 nike

我已经定义了自己的类型。它包含一个指向数组的指针,以及该数组中有多少项

struct neighborList
{
unsigned int nNeighbors;
unsigned int* pNeighbors;
};

这些被实例化、填充,最终我想回顾它们。然后发生了一些非常奇怪的事情。我认为这里的屏幕截图比文字更好。

enter image description here

我已经显示了下一个要执行的语句。我有一个上述数据类型的数组,这里考虑的数组有 1 个邻居,该 1 个邻居的地址是 0x107a28;凉爽的。但实际分配给 pLook 的是什么?

enter image description here

地址总是偏离 0x40。有人见过这样的东西吗?感谢这里的帮助。

编辑:这就是整个内容,因为有几个人想看它。

#include "stdafx.h"
#include <stdlib.h>
#include <time.h>

//#define NVERTEX 875714
#define NVERTEX 9

struct linkedNode
{
unsigned int node;
linkedNode* pNextLinkedNode;
linkedNode* pPrevLinkedNode;
};

struct neighborList
{
unsigned int nNeighbors;
unsigned int* pNeighbors;
};

struct linkedNodeList
{
linkedNode* pHead;
linkedNode* pTail;
};

void populateNeighbors(neighborList* pNeighborList, FILE* fp);
void DFSLoop(neighborList* pNeighborList, linkedNode* pOutput, unsigned int nNodes);
void append(linkedNodeList* pLinkedList, unsigned int node);

void DFSLoop(neighborList* pNeighborList, linkedNodeList* pOutput, unsigned int nNodes)
{
bool* visitedArray;
bool* cashedArray;
unsigned int* leaderArray;
unsigned int* finishingTimes;
unsigned int t = 0;

visitedArray = (bool*)malloc(nNodes*sizeof(bool));
cashedArray = (bool*)malloc(nNodes*sizeof(bool));
leaderArray = (unsigned int*)malloc(nNodes*sizeof(unsigned int));
finishingTimes = (unsigned int*)malloc(nNodes*sizeof(unsigned int));

//initialize all arrays to all false/0
for (unsigned int i = 0; i < nNodes; i++)
{
visitedArray[i] = false;
cashedArray[i] = false;
leaderArray[i] = 0;
finishingTimes[i] = 0;
}

//firstly, pick a starting node and put it on the linkedList
//initialize head and tail
(pOutput->pHead)->node = 1;
(pOutput->pHead)->pNextLinkedNode = NULL;
(pOutput->pHead)->pPrevLinkedNode = NULL;

(pOutput->pTail)->node = 1;
(pOutput->pTail)->pNextLinkedNode = NULL;
(pOutput->pTail)->pPrevLinkedNode = NULL;

unsigned int curNode = (pOutput->pTail)->node;
for (;;)
{
//Start DFS
//#1 If current node under consideration has an unexplored neighbor, make it the new tail and repeat
// If not, current node is cashed. Set it's finishing time, and leader. Work back through the list
// Until you find a node with an unexplored neighbor
unsigned int nNeighbors = pNeighborList[curNode].nNeighbors;

for (unsigned int i = 0; i < nNeighbors; i++)
{
unsigned int* pLook = (pNeighborList[curNode]).pNeighbors;
unsigned int neighbor = pLook[0];
/*
unsigned int nodeUnderConsideration = (pNeighborList[curNode].pNeighbors)[i];

if ( !cashedArray[nodeUnderConsideration])
{
append(pOutput, (pNeighborList[curNode].pNeighbors)[i]);
curNode = (pOutput->pTail)->node;
continue;
}
*/
}


//#2 If you make it back to the head and have no unexplored neighbors, pick new vertex (if unvisited) and repeat



}


free(visitedArray);
free(cashedArray);
free(leaderArray);
free(finishingTimes);
}

int _tmain(int argc, _TCHAR* argv[])
{
//open file
FILE* fp;
FILE* fpRev;
//fp = fopen("SCC.txt", "rb");
//fpRev = fopen("SSCrev.txt", "rb");
fp = fopen("SSCsmall1.txt", "rb");
fpRev = fopen("SSCsmall1rev.txt", "rb");

/* read file. When reading, keep track of how much memory to malloc */
/* for each vertex */
neighborList* pAllEdges;
neighborList* pAllEdgesRev;
pAllEdges = (neighborList*)malloc(NVERTEX*sizeof(neighborList));
pAllEdgesRev = (neighborList*)malloc(NVERTEX*sizeof(neighborList));

populateNeighbors(pAllEdges, fp);
populateNeighbors(pAllEdgesRev, fpRev);

//instantiate pointers for linkedlists needed for DFS
linkedNodeList NodesFirstPass, NodesSecondPass;
NodesFirstPass.pHead = (linkedNode*)malloc(sizeof(linkedNode));
NodesFirstPass.pTail = NodesFirstPass.pHead;
NodesSecondPass.pHead = (linkedNode*)malloc(sizeof(linkedNode));
NodesSecondPass.pTail = NodesSecondPass.pHead;

DFSLoop(pAllEdges, &NodesFirstPass, NVERTEX);

free(pAllEdges);
free(pAllEdgesRev);
return 0;
}

void populateNeighbors(neighborList* pNeighborList, FILE* fp)
{
unsigned int v1 = 1;
unsigned int v2 = 1;

unsigned int v1_next = 1;
unsigned int v2_next = 1;
unsigned int neighbors [1000];
fscanf(fp, "%u", &v1_next);
fscanf(fp, "%u", &v2_next);

for (unsigned int i = 0; i < (NVERTEX - 1); i++)
{
//initialize nNeigbors to 0
unsigned int nNeighbors = 0;

for (;;)
{
//if v1_next is a different vertex then v1, then copy v1_next to v1,
//malloc what we need to, copy over the array and continue
if (v1_next != v1)
{
pNeighborList[i].nNeighbors = nNeighbors;

if (nNeighbors != 0)
{
pNeighborList[i].pNeighbors = (unsigned int*)malloc(nNeighbors * sizeof(unsigned int));

for (unsigned int j = 0; j < nNeighbors; j++)
{
pNeighborList[i].pNeighbors[j] = neighbors[j];
}

}

v1++;
break;
}

//else, increment the neighbor count for this particular vertex and continue
//within this loop, getting new neighbors (edges)
else
{
neighbors[nNeighbors] = v2_next;
nNeighbors++;
if (nNeighbors == 1000)
{
break;
}
fscanf(fp, "%u", &v1_next);
fscanf(fp, "%u", &v2_next);
}
}
}

}

void append(linkedNodeList* pLinkedList, unsigned int node)
{
//make new node with the intention that it's going to be the new tail
linkedNode* pNewNode = (linkedNode*)malloc(sizeof(linkedNode));
pNewNode->node = node;
pNewNode->pNextLinkedNode = NULL;
pNewNode->pPrevLinkedNode = pLinkedList->pTail;

//set next node of current tail to new node
(pLinkedList->pTail)->pNextLinkedNode = pNewNode;

//new tail becomes new node
pLinkedList->pTail = pNewNode;

//lastly, set old tail's next node to point to new tail
(pLinkedList->pTail->pPrevLinkedNode)->pNextLinkedNode = pLinkedList->pTail;
}

最佳答案

从屏幕截图来看,假设您使用的是 64 位系统(指针有 8 个字节宽),指针 pNeighborList 链接到列表的开始 ,而 pLook 链接到 索引 5neighborList 元素的 pNeighbors 属性:

// assuming sizeof(neighborList) == 4 (int) + 8 (pointer) = 12 bytes

neighborList* pNeighborList = new neighborList[10];
// pNeighborList points to the start of the list, 0x00107a28

// pNeighborList[5] is at address 0x00107a64 (start + 5 * sizeof(neighborList)
// .pNeighbors is offset 4 more bytes (sizeof(unsigned int)) = 0x00107a68

int curNode = 5;
unsigned int* pLook = (pNeighborList[curNode]).pNeighbors;
// pLook points to pNeighbors of the element at index 5, 0x00107a68

当您将指针悬停在 Visual Studio 中的 pNeighborList 时,它会显示指针(指向列表的开头),而不是完整值 ((pNeighborList[curNode]) .pNeighbors)。

关于c - 为什么我的指针会改变固定的数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33714718/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com