【问题标题】:Variable 'c' set but not used [-Wunused-but-set-variable]变量 'c' 已设置但未使用 [-Wunused-but-set-variable]
【发布时间】:2020-10-27 22:35:59
【问题描述】:

我已经搜索并没有真正找到并理解这个错误。奇怪的是,我只得到 c、d、e 的错误,而不是 a 和 b 或它们所有的错误。

节目是关于双链表的。

当我编译时会发生这种情况:

gcc -Wall -g -c program.c

错误部分:

void try_mymem(int argc, char** argv) {
strategies strat;
void *a, *b, *c, *d, *e;
if (argc > 1)
    strat = strategyFromString(argv[1]);
else
    strat = First;

/* A simple example.
   Each algorithm should produce a different layout. */

initmem(strat, 500);

a = mymalloc(100);
b = mymalloc(100);
c = mymalloc(100);
myfree(b);
d = mymalloc(50);
myfree(a);
e = mymalloc(25);

print_memory();
print_memory_status();

}

我做错了什么?

【问题讨论】:

    标签: c ubuntu linked-list doubly-linked-list


    【解决方案1】:

    正如编译器所说,它们没有被使用。您分配但从未阅读。 ab 被用作参数,其他的不是。

    【讨论】:

    • 但是,这不就是:a = mymalloc(100); b = mymalloc(100); c = mymalloc(100);我的免费(b); d = mymalloc(50);我的免费(一); e = mymalloc(25);还是我完全错了?
    • assignment(如a = ...)和用法(如f(a))之间存在差异。编译器说你分配了很多从未使用过的变量。
    猜你喜欢
    • 2023-02-26
    • 1970-01-01
    • 1970-01-01
    • 2011-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多