C 为什么这个程序会出现分段错误?

C 为什么这个程序会出现分段错误?,c,segmentation-fault,disk-io,C,Segmentation Fault,Disk Io,这是我写的一个程序,用来检查文件和磁盘之间的字节 #include <stdio.h> #include <fcntl.h> #include <unistd.h> #define BYTES_TO_READ 64 int main(int argc, char **argv) { int device = open("/dev/sdz", O_RDWR); if(device < 0) { printf("Device op

这是我写的一个程序,用来检查文件和磁盘之间的字节

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>

#define BYTES_TO_READ 64

int main(int argc, char **argv)
{
  int device = open("/dev/sdz", O_RDWR);
  if(device < 0)
  {
      printf("Device opening error\n");
      return 1;
  }
  int file = open("test.txt", O_RDONLY);
  if(file < 0)
  {
      printf("File opening error\n");
      return 2;
  }
  int byte, device_loc, file_loc;
  char *buff_device, *buff_file;
  for(byte = 0; byte<BYTES_TO_READ; byte++)
  {
      device_loc = lseek(device, byte, SEEK_SET); /* SEG FAULT */
      file_loc = lseek(file, byte, SEEK_SET);
      printf("File location\t%d",file_loc);
      printf("Device location\t%d",device_loc);
      read(device, buff_device, 1);
      read(file, buff_file, 1);
      if( (*buff_device) == (*buff_file) )
      {
          printf("Byte %d same", byte);
      }
      else
      {
          printf("Bytes %d differ: device\t%d\tfile\t%d\n",byte, *buff_device, *buff_file);
      }
  }
  return 0;
}
#包括
#包括
#包括
#定义字节\u至\u读取64
int main(int argc,字符**argv)
{
int设备=打开(“/dev/sdz”,O_RDWR);
如果(设备<0)
{
printf(“设备打开错误\n”);
返回1;
}
int file=open(“test.txt”,仅限ordu);
如果(文件<0)
{
printf(“文件打开错误\n”);
返回2;
}
int字节,设备位置,文件位置;
char*buff_设备,*buff_文件;

对于(byte=0;byte,这些是对内存中随机位置的写入:

read(device, buff_device, 1);
read(file, buff_file, 1);
由于
buff_设备
buff_文件
是未初始化的指针。请使用
char
键入并传递其地址

char buff_device;
char buff_file;

/* Check return value of read before using variables. */
if (1 == read(device, &buff_device, 1) &&
    1 == read(file, &buff_file, 1))
{
    if (buff_device == buff_file)
    /* snip */
}
else
{
    /* Report read failure. */
}

它们正在写入内存中的随机位置:

read(device, buff_device, 1);
read(file, buff_file, 1);
由于
buff_设备
buff_文件
是未初始化的指针。请使用
char
键入并传递其地址

char buff_device;
char buff_file;

/* Check return value of read before using variables. */
if (1 == read(device, &buff_device, 1) &&
    1 == read(file, &buff_file, 1))
{
    if (buff_device == buff_file)
    /* snip */
}
else
{
    /* Report read failure. */
}

它们正在写入内存中的随机位置:

read(device, buff_device, 1);
read(file, buff_file, 1);
由于
buff_设备
buff_文件
是未初始化的指针。请使用
char
键入并传递其地址

char buff_device;
char buff_file;

/* Check return value of read before using variables. */
if (1 == read(device, &buff_device, 1) &&
    1 == read(file, &buff_file, 1))
{
    if (buff_device == buff_file)
    /* snip */
}
else
{
    /* Report read failure. */
}

它们正在写入内存中的随机位置:

read(device, buff_device, 1);
read(file, buff_file, 1);
由于
buff_设备
buff_文件
是未初始化的指针。请使用
char
键入并传递其地址

char buff_device;
char buff_file;

/* Check return value of read before using variables. */
if (1 == read(device, &buff_device, 1) &&
    1 == read(file, &buff_file, 1))
{
    if (buff_device == buff_file)
    /* snip */
}
else
{
    /* Report read failure. */
}
更改:

char *buff_device, *buff_file;

更改:

char *buff_device, *buff_file;

更改:

char *buff_device, *buff_file;

更改:

char *buff_device, *buff_file;



buff_设备
看起来未初始化。我提到了
sdz
是一个环回设备。如果它是一个真正的磁盘,会有什么区别吗?
buff_设备
看起来未初始化。我提到了
sdz
是一个环回设备。如果它是一个真正的磁盘,会有什么区别吗?
buff_设备
看起来未初始化d、 我提到了
sdz
是一个环回设备。如果它是一个真实的磁盘,会有什么区别吗?
buff_设备
看起来没有初始化。我提到了
sdz
是一个环回设备。如果它是一个真实的磁盘,会有什么区别吗?我想
read
会分配内存,然后将其地址放入缓冲区提供给函数。所以它不,是吗?@LastBlack,不,它不分配内存。@LastBlack只是一个思维实验:如果
read()
确实分配了内存,那会对
buff\u设备
产生什么影响?
buff\u设备
是一个指针(目前指向la la land),该值被赋予
read()
read()
对该值的副本所做的任何操作都不会影响原始
buff\u设备
@chux,正确。如果
read()
是分配内存,然后需要将指向指针的指针作为参数传递,以便调用者可以看到更改。@chux当我想到它时,你们是完全正确的。我不是一个很好的C程序员。我主要做脚本,不涉及指针:D感谢澄清。不过
read
会分配内存,then将其地址放入为函数提供的缓冲区中。所以它不,是吗?@LastBlack,不,它不分配内存。@LastBlack只是一个思维实验:如果
read()
确实分配了内存,那会对
buff\u设备
产生什么影响?
buff\u设备
是一个指针(目前指向la la land)该值被赋予
read()
read()
对该值的副本所做的任何操作都不会影响原始
buff\u设备
@chux,正确。如果
read()
是分配内存,然后需要将指向指针的指针作为参数传递,以便调用者可以看到更改。@chux当我想到它时,你们是完全正确的。我不是一个很好的C程序员。我主要做脚本,不涉及指针:D感谢澄清。不过
read
会分配内存,then将其地址放入为函数提供的缓冲区中。所以它不,是吗?@LastBlack,不,它不分配内存。@LastBlack只是一个思维实验:如果
read()
确实分配了内存,那会对
buff\u设备
产生什么影响?
buff\u设备
是一个指针(目前指向la la land)该值被赋予
read()
read()
对该值的副本所做的任何操作都不会影响原始
buff\u设备
@chux,正确。如果
read()
是分配内存,然后需要将指向指针的指针作为参数传递,以便调用者可以看到更改。@chux当我想到它时,你们是完全正确的。我不是一个很好的C程序员。我主要做脚本,不涉及指针:D感谢澄清。不过
read
会分配内存,then将其地址放入为函数提供的缓冲区中。所以它不,是吗?@LastBlack,不,它不分配内存。@LastBlack只是一个思维实验:如果
read()
确实分配了内存,那会对
buff\u设备
产生什么影响?
buff\u设备
是一个指针(目前指向la la land)该值被赋予
read()
read()
对该值的副本所做的任何操作都不会影响原始
buff\u设备
@chux,正确。如果
read()
是分配内存,然后需要将指向指针的指针作为参数传递,以便调用者可以看到更改。@chux当我想到它时,你们是完全正确的。我不是一个C程序员。我主要编写脚本,不涉及指针:D感谢澄清