传递到结构功能 [英] Passing struct to functions

查看:125
本文介绍了传递到结构功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我不知道为什么,当我传递结构阵列功能;当我尝试访问它的成员将打印随机数。以下声明的printf(%d个\\ N,netTopo [0] .nodes [1]);工作正确的,但我是在功能和尝试打印相同的数据,它打印一串随机数?不知道我做错了。

  INT主(INT ARGC,CHAR *的argv []){如果(argc个!= 3){
        的printf(不正确的命令行参数必修2文件\\ n);
        出口(-1);
    }    FILE * NETFILE,* schFile; //放入一个循环
    NETFILE = FOPEN(的argv [1],R);
    schFile = FOPEN(的argv [2],R);    为int * =附表getSchedFile(schFile);    结构的nodeinfo * netTopo = getTopology(NETFILE);
    的printf(%d个\\ N,netTopo [0] .nodes [1]);    INT nodeSocks [nodeCount]
    的for(int i = 0; I< nodeCount;我++){
        nodeSocks [I] = getSocketNo();
    }    get_elapsed_time(); //启动时钟    的for(int i = 0; I< nodeCount;我++){
        如果(叉()== 0){
            nodeExecution(I,nodeSocks,netTopo,章附表);
            出口(0);
        }
    }
}无效nodeExecution(INT ID,INT节点[],结构的nodeinfo * netTopo,为int * SCHD){
    的printf(%d个\\ N,netTopo [0] .nodes [1]);
......


解决方案

让你从getTopology在栈上返回一个指针到局部变量()?这就是错误。

netTopo是堆栈,当你从getTopology返回()有会重用哪里netTopo存储在存储区域其他函数调用。该内存被修改,并呼吁nodeExecution当你得到不同的输出()

地址:解决这个问题,你可以在getTopology()分配内存:

 结构的nodeinfo * getTopology(FILE *文件){
    INT ID,数字= 0,totLinks = 0;
    的fscanf(文件%d个,&安培; nodeCount);
    结构的nodeinfo * netTopo =的malloc(sizeof的(结构的nodeinfo)* nodeCount);  ....

Hey I'm not sure why when I pass a Struct array to a function; When I try to access it's members it prints random number. Below the statement "printf("%d\n", netTopo[0].nodes[1]);" works correct but I'm in the function and try print the same data, it prints a bunch of random number? Not sure what I'm doing wrong.

int main(int argc, char *argv[]) {

if (argc != 3){
        printf("Incorrect command line arguments. Required 2 files.\n");
        exit(-1);
    }

    FILE *netFile, *schFile; // put into a loop
    netFile = fopen(argv[1], "r");
    schFile = fopen(argv[2], "r");

    int *sched = getSchedFile(schFile);

    struct nodeInfo *netTopo = getTopology(netFile);
    printf("%d\n", netTopo[0].nodes[1]);

    int nodeSocks[nodeCount];
    for (int i=0; i<nodeCount; i++){
        nodeSocks[i]=getSocketNo();
    }

    get_elapsed_time(); // start clock

    for (int i=0; i<nodeCount; i++){
        if (fork()==0){
            nodeExecution(i, nodeSocks, netTopo, sched);
            exit(0);
        }
    }
}

void nodeExecution(int id, int nodes[], struct nodeInfo *netTopo, int *schd){
    printf("%d\n", netTopo[0].nodes[1]);
......

解决方案

so you return a pointer to local var on stack from getTopology()? that's the bug.

netTopo is on stack and when you return from getTopology() there are other function calls which would reuse the memory region where netTopo is stored. That memory is modified and you get different output when calling nodeExecution()

ADD: to fix this you may allocate memory in getTopology():

struct nodeInfo* getTopology(FILE *file){
    int id, digit=0, totLinks=0;
    fscanf(file, "%d", &nodeCount);
    struct nodeInfo * netTopo = malloc(sizeof(struct nodeInfo)*nodeCount);

  ....

这篇关于传递到结构功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆