c - 为什么这个程序中的指针指向不相关的字段?

标签 c pointers struct

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>


struct node {
int n;
int d;
struct node *ptr;
int *ec;
int *fl;
int xs;
};

// This is a structure of node of a graph

void createNode(struct node *newNode,int no,int dis)
{
newNode->n=no;
newNode->d=dis;
newNode->ptr=(struct node*)malloc(sizeof(struct node));
newNode->ec=(int*)malloc(sizeof(int));
newNode->fl=(int*)malloc(sizeof(int));
newNode->xs=(int)malloc(sizeof(int));
}

void createArr(struct node *newNode,struct node *toNode, int capt,int e_no)
{
newNode->ptr[e_no]=*toNode;
newNode->ec[e_no]=capt;
newNode->fl[e_no]=0;
newNode->xs=0;
}

void printNode(struct node *newNode,int e)
{
printf("%d\t%d\t%d\t%d\t%d\t%d\n",newNode->n,(newNode->ptr[e]).n,newNode->ec[e],
newNode->d,newNode->fl[e],newNode->xs);
}

int main()
{

// Assigning memory to nodes


struct node *newNode1 =(struct node*)malloc(sizeof(struct node));
struct node *newNode2 =(struct node*)malloc(sizeof(struct node));
struct node *newNode3 =(struct node*)malloc(sizeof(struct node));
struct node *newNode4 =(struct node*)malloc(sizeof(struct node));

// This function call allocates memory to arrays inside structure node

createNode(newNode1,1,4);
createNode(newNode2,2,0);
createNode(newNode3,3,0);
createNode(newNode4,4,0);

// This function call forms edges between nodes, sets capacity of edges, flow of edges, excess at each node

createArr(newNode1,newNode2,1,1);
createArr(newNode1,newNode4,2,2);
createArr(newNode2,newNode3,3,1);
createArr(newNode4,newNode2,5,1);
createArr(newNode4,newNode3,5,2);

// This function call prints the node

printNode(newNode1,1);
printNode(newNode1,2);
printNode(newNode2,1);
printNode(newNode4,1);
printNode(newNode4,2);
return 0;
}

输出:

1   2   1   4   0   0
1   4   2   4   0   0
2   3   3   0   0   0
4   2   5   0   0   0
4   3   5   0   0   0

如果我这样改变输入

/*only this part changed, rest input through main() same*/ 

createNode(newNode1,1,4);
createNode(newNode2,2,1);
createNode(newNode3,3,2);
createNode(newNode4,4,3);

输出:

1   2   1   4   3   0
1   4   2   4   0   0
2   3   3   1   0   0
4   2   5   3   2   0
4   3   5   3   0   0

为什么节点 1 的“fl”获得节点 4 的“d”的值,即使两者不相关并且我已明确将每个节点的“fl”设为 0?

最佳答案

可能是因为来自 createArr 的指令:

newNode->ec[e_no]=capt;
newNode->fl[e_no]=0;

正在写入内存中不需要的地址。这是初始化字段 fl 和 ec 的操作:

newNode->ec=(int*)malloc(sizeof(int));
newNode->fl=(int*)malloc(sizeof(int));

因此,您正在为 ONE int 保留内存以供 fl 指向,而 ONE int 为由 ec 指向。但是在调用 createArray() 时,参数 e_no 在您的示例中为 1 或 2。那就是试图在保留的整数数量之后写入一个值 1 或 2 个位置。事实上,对于您当前的初始化,e_no 的唯一有效值应该是 0。

因此,您必须根据 e_no 可以拥有的最大值保留所需的整数。

newNode->ec=malloc(MAX_ENO * sizeof newNode->ec);
newNode->fl=malloc(MAX_ENO * sizeof newNode->fl);

关于c - 为什么这个程序中的指针指向不相关的字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20784818/

相关文章:

c - 如何减少C中文件指针的值?

c++ - 动态内存分配并接受输入

c - 如果一个字符串是其他字符串的旋转。所有测试用例未运行

c - 在c中访问数组的-1元素

Python:将 float 字符串解包为复数

从 void * 转换为 int

c - 如何在 C 中使用 malloc() 分配结构数组?

C 段错误双指针赋值位于结构内部而不是外部。

c - 结构声明中的结构数组 : "array type has incomplete element type"

比较接口(interface)的 ipv4 地址和数据包的源地址(libpcap)