指针在主不更新 [英] Pointer not updating in main

查看:117
本文介绍了指针在主不更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在另一个文件中调用一个函数主程序 cbuf_update()这反过来又来电 cbuf_sizeChange()这里是什么困惑我:在 cbuf_update ,要求 cbuf_sizeChange 正确更新 cb_ptr ,但main.c中,它是垃圾从我释放在 sizeChange()。我不能让静态的,因为有主cbufs的变量数。我该怎么办?我不能改变的签名 cbuf_update()

结构体DEF:

  typedef结构{CBUF
    unsigned int类型最高;
    无符号整型启动;
    无符号整型结束;
    unsigned int类型的大小;
    报价*报价;
} CBUF;

从main.c中召唤:

  CBUF * eur_jpy;
eur_usd = cbuf_init();
cbuf_update(eur_jpy,时间,速度);

在其他文件中的相关方法:

  CBUF * cbuf_init()
{
    //初始化使用malloc的CBUF    返回CB1;
}无效cbuf_update(CBUF * cb_ptr,双速率)
{
    cb_ptr = cbuf_sizeChange(cb_ptr,2);
}CBUF * cbuf_sizeChange(CBUF * CB1,双因子)
{
        CBUF * CB2;
    报价*报价;
    报价=(报价*)malloc的(cb1->最大* *因素的sizeof(报价));
    CB2 =(CBUF *)malloc的(sizeof的(*引号)+ 4 * sizeof的(无符号整数));        //更新报价这里(exluding它)        cb2->大小= cb1->大小;
    cb2->结束= cb1->大小 - 1;
    cb2->最大=系数* cb1->最大;
    cb2->启动= 0;
    免费(cb1->引号);
    免费(CB1);    cb2->报价=报价;    返回CB2;
}


解决方案

这一点看起来不正确的:

 无效cbuf_update(CBUF * cb_ptr,双速率)
{
    cb_ptr = cbuf_sizeChange(cb_ptr,2);
}

它修改 cb_ptr ,这是一个本地副本无论你传递给 cbuf_update()作为第一个参数

您可能想沿着这些线路认为:

 无效cbuf_update(CBUF ** cb_ptr,双速率)
{
    * cb_ptr = cbuf_sizeChange(* cb_ptr,2);
}

和,而不是调用 cbuf_update(什么东西,速度)通话 cbuf_update。(安培;什么东西,速度)

I have a main program in another file calling a function cbuf_update() that in turn calls cbuf_sizeChange() Here is what is confusing me: Within cbuf_update, calling cbuf_sizeChange properly updates cb_ptr, but in main.c, it is garbage from when I freed cb1 in sizeChange(). I cannot make it static because there are a variable number of cbufs in main. What do I do? I cannot change the signature of cbuf_update().

Struct def:

typedef struct cbuf {
    unsigned int max;
    unsigned int start;
    unsigned int end;
    unsigned int size;
    quote *quotes;
} cbuf;

Call from main.c:

cbuf *eur_jpy;
eur_usd = cbuf_init() ;
cbuf_update(eur_jpy, time, rate) ;

Relevant methods in other file:

cbuf * cbuf_init()
{
    //initialize the cbuf with malloc

    return cb1;
}

void cbuf_update(cbuf *cb_ptr, double rate)
{
    cb_ptr = cbuf_sizeChange(cb_ptr, 2);
}

cbuf *cbuf_sizeChange(cbuf *cb1, double factor)
{
        cbuf *cb2;
    quote *quotes;
    quotes = (quote *)malloc(cb1->max * factor * sizeof(quote));
    cb2 = (cbuf *)malloc(sizeof(*quotes) + 4 * sizeof(unsigned int));

        //Update quotes here(exluding it)

        cb2->size = cb1->size;
    cb2->end = cb1->size - 1;
    cb2->max = factor * cb1->max;
    cb2->start = 0;
    free(cb1->quotes);
    free(cb1);

    cb2->quotes = quotes;

    return cb2;
}

解决方案

This bit doesn't look right:

void cbuf_update(cbuf *cb_ptr, double rate)
{
    cb_ptr = cbuf_sizeChange(cb_ptr, 2);
}

It modifies cb_ptr, which is a local copy of whatever you passed into cbuf_update() as the first parameter.

You might want to think along these lines:

void cbuf_update(cbuf **cb_ptr, double rate)
{
    *cb_ptr = cbuf_sizeChange(*cb_ptr, 2);
}

and instead of calling cbuf_update(something, rate) call cbuf_update(&something, rate).

这篇关于指针在主不更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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