小编dav*_*vid的帖子

带有模数运算符的无符号溢出

我在我编写的一些c代码中遇到了一个错误,虽然它相对容易修复,但我希望能够更好地理解它背后的问题.基本上发生的事情是我有两个无符号整数(实际上是uint32_t),当应用模数运算时,产生了一个负数的无符号等价物,一个已被包裹的数字,因此是"大".这是一个示例程序来演示:

#include <stdio.h>
#include <stdint.h>

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

  uint32_t foo = -1;
  uint32_t u   = 2048;
  uint64_t ul  = 2048;

  fprintf(stderr, "%d\n", foo);
  fprintf(stderr, "%u\n", foo);
  fprintf(stderr, "%lu\n", ((foo * 2600000000) % u));
  fprintf(stderr, "%ld\n", ((foo * 2600000000) % u));
  fprintf(stderr, "%lu\n", ((foo * 2600000000) % ul));
  fprintf(stderr, "%lu\n", foo % ul);

  return 0;

}
Run Code Online (Sandbox Code Playgroud)

这会在我的x86_64机器上产生以下输出:

-1
4294967295
18446744073709551104
-512
1536
2047
Run Code Online (Sandbox Code Playgroud)

1536是我期待的数字,但是(uint32_t)( - 512)是我得到的数字,正如你可能想象的那样,它会让人感觉不舒服.

所以,我想我的问题是:为什么两个无符号数之间的模数运算,在这种情况下,产生的数字大于除数(即负数)?这种行为是首选的原因吗?

c unsigned overflow modulus

5
推荐指数
1
解决办法
2076
查看次数

连接到hadoop中的HDFS时出现EOFException

在包含的测试程序中,我尝试将文件从本地磁盘复制到HDFS.代码如下:

package foo.foo1.foo2.test;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

public class TestTestTest {

    public static void main(String[] args) {

    String srcLocation = "foo";
    String destination = "hdfs:///tmp/";

    FileSystem hdfs = null;

    Configuration configuration = new Configuration();
    configuration.set("fs.default.name", "hdfs://namenode:54310/");

    try {
        hdfs = FileSystem.get(configuration);
    } catch (IOException e2) {
        e2.printStackTrace();
        return;
    }

    Path srcpath = new Path(srcLocation);
    Path dstpath = new Path(destination);

    try {
        hdfs.copyFromLocalFile(srcpath, dstpath);
    } catch (IOException e) {
        e.printStackTrace();
    }

    }

}
Run Code Online (Sandbox Code Playgroud)

这失败,出现以下异常:

java.io.IOException: Call to …
Run Code Online (Sandbox Code Playgroud)

java hadoop eofexception hdfs

4
推荐指数
1
解决办法
1万
查看次数

标签 统计

c ×1

eofexception ×1

hadoop ×1

hdfs ×1

java ×1

modulus ×1

overflow ×1

unsigned ×1