无法理解 C 源代码,它不能在 GCC 中编译,而是在 Visual C++ 中编译。

发布于 2024-10-14 23:49:20 字数 526 浏览 4 评论 0原文

在 GCC 中我收到以下错误:

aes.c:在函数“copy_block”中:
aes.c:278: 错误:需要左值作为增量操作数
aes.c:278:错误:需要左值作为增量操作数

这是一段代码:

static void copy_block( void * d, void *s, uint_8t nn )
{
    while( nn-- )
        *((uint_8t*)d)++ = *((uint_8t*)s)++;
}

我试图将其更改为可编译版本,但不幸的是,对于我作为 Java 程序员来说,不清楚这里到底发生了什么。

也许有人知道我如何更改它在 GCC 中可编译的源代码,或者有人知道这里详细发生了什么。对我来说,左手值的取消引用似乎很奇怪,但不知怎的,它似乎在 Visual C++ 中完美工作。

这是一个小的遗留程序,我必须将其移植到 Linux 机器上。

提前感谢您的帮助。

In GCC I got the following error:

aes.c: In function ‘copy_block’:
aes.c:278: error: lvalue required as increment operand
aes.c:278: error: lvalue required as increment operand

This is the piece of code:

static void copy_block( void * d, void *s, uint_8t nn )
{
    while( nn-- )
        *((uint_8t*)d)++ = *((uint_8t*)s)++;
}

I tried to change it to a compileable version, but unfortunately for me as a Java programmer, it's not clear what is really happening here.

Maybe someone has an idea how I can change the source that it is compileable in GCC or someone has an idea what is happening here in detail. For me it seems wierd with the dereferencing of the left hand value, but somehow it seems to work perfectly in Visual C++.

This is a small legacy program, which I have to port to a Linux machine.

thank you for your help in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

魂牵梦绕锁你心扉 2024-10-21 23:49:20

试试这个:

#include <string.h>
static void copy_block( void * d, void *s, uint_8t nn ) {
  memcpy(d, s, (size_t)nn);
}

那里所做的事情并不好。

Try this:

#include <string.h>
static void copy_block( void * d, void *s, uint_8t nn ) {
  memcpy(d, s, (size_t)nn);
}

What's being done there isn't good.

趴在窗边数星星i 2024-10-21 23:49:20

这应该可以做到:

static void copy_block(void *d, void *s, uint_8t nn)
{
    uint_8t *ud = (uint_8t *)d;
    uint_8t *us = (uint_8t *)s;

    while (nn--)
    {
        *ud = *us;
        ++ud;
        ++us;
    }
}

它也更具可读性。

This should do it:

static void copy_block(void *d, void *s, uint_8t nn)
{
    uint_8t *ud = (uint_8t *)d;
    uint_8t *us = (uint_8t *)s;

    while (nn--)
    {
        *ud = *us;
        ++ud;
        ++us;
    }
}

It's also much more readable.

也只是曾经 2024-10-21 23:49:20

这是一些看起来非常毛茸茸的代码。看起来 *(pointer)++ 子表达式的优先级规则存在问题。 (Visual C++ 接受它介于奇迹和错误之间)它似乎只是一个(可怕的)memcpy 重新实现,所以我建议您使用标准版本:

memcpy(d, s, nn);

我建议在 Visual C++ 中进行快速特征测试,以确保两个版本的代码实际上执行相同的操作(正如它们看起来的那样)。您可能在这里依赖于编译器中的一些奇怪的边缘情况。

This is some seriously hairy looking code. Looks like there's an issue with precedence rules on the *(pointer)++ subexpressions. (that Visual C++ accepts it is somewhere between miracle and bug) It seems to be just a (terrible) memcpy reimplementation, so I suggest you use the standard version:

memcpy(d, s, nn);

I suggest doing a quick characterisation test in Visual C++ to make sure the two versions of the code do in fact do the same thing (as they appear to). You might be relying on some quirky edge case in the compiler here.

沙与沫 2024-10-21 23:49:20

问题是强制转换返回右值,但您试图对强制转换返回的指针执行自动增量。试试这个:

uint8_t *da = d, *sa = s;
while(nn--) *da++ = *sa++;

另外两个注意事项:

  • nn 可能应该比 uint8_t 大很多,并且可能应该是 size_t
  • 经过这一更改,该函数被命名为 memcpy 并在您的标准库中找到(可能更有效)(我相信在 string.h 中)。

The problem is that casts return rvalues, but you're trying to do an autoincrement on the pointer returned by a cast. Try this:

uint8_t *da = d, *sa = s;
while(nn--) *da++ = *sa++;

Two other notes:

  • nn should probably be a good deal larger than uint8_t and should probably be size_t.
  • With that change, this function is named memcpy and is found (probably more efficiently) in your standard library (in string.h I believe).
野生奥特曼 2024-10-21 23:49:20

您可能搞砸了括号位置。
您想要增加指针,而不是取消引用的值。

static void copy_block( void * d, void *s, uint_8t nn )
{
    while( nn-- )
        *((uint_8t*)d++) = *((uint_8t*)s++);
}

作为奖励提示,使用 memcpy ...更快!

You probably screwed up the parenthesis position.
You want to increment the pointer, not the dereferenced value.

static void copy_block( void * d, void *s, uint_8t nn )
{
    while( nn-- )
        *((uint_8t*)d++) = *((uint_8t*)s++);
}

As a bonus tip, use memcpy ... much faster !

卖梦商人 2024-10-21 23:49:20

它是 为什么这个转换的结果不是一个重复的左值?

基本上强制转换不是左值。但它可以与某些编译器一起使用。不仅是旧的 GCC,最近的 Tasking 也“正确”编译并且代码可以工作。

It's a duplicate of Why isn't the result of this cast an lvalue?

Basically casts are not l-value. It works with some compilers though. Not only old GCC, quite recent Tasking also compiled "properly" and the code worked.

ぶ宁プ宁ぶ 2024-10-21 23:49:20

应该是:

static void copy_block( void * d, void *s, uint_8t nn )
{
    while( nn-- )
        (*((uint_8t*)d))++ = (*((uint_8t*)s))++;
}

Should be:

static void copy_block( void * d, void *s, uint_8t nn )
{
    while( nn-- )
        (*((uint_8t*)d))++ = (*((uint_8t*)s))++;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文