C aio_写入和memset无效参数和分段错误(内核转储)

C aio_写入和memset无效参数和分段错误(内核转储),c,segmentation-fault,memset,invalid-argument,C,Segmentation Fault,Memset,Invalid Argument,我试图用“a”字符填充我的文件,我需要使用aio\u write。这是我的写作功能 int da_aio_write(const int d, struct aiocb *aiorp, void *buf, const int count){ int rv = 0; // memset( (void *)aiorp, 'a', sizeof( struct aiocb ) ); // <--- I get Invalid argument (Error da_aio_write)

我试图用“a”字符填充我的文件,我需要使用
aio\u write
。这是我的写作功能

int da_aio_write(const int d, struct aiocb *aiorp, void *buf, const int count){
   int rv = 0;
  // memset( (void *)aiorp, 'a', sizeof( struct aiocb ) ); // <--- I get Invalid argument (Error da_aio_write)

  // memset(&aiorp, 'a', sizeof( struct aiocb )); // <--- I get Segmentation Fault (core dumped)
   aiorp->aio_fildes = d;
   aiorp->aio_buf = buf;
   aiorp->aio_nbytes = count;
   aiorp->aio_offset = 0;

   rv = aio_write( aiorp );

   if( rv == -1) {
       perror("Error da_aio_write\n");
       exit(1);
       return rv;
   }
   return rv;
}

如果要将
aiorp
的每个字节设置为
'a'
的ASCII值,则第一个变量:

memset((void *) aiorp, 'a', sizeof(struct aiocb));
这很好。(不过,您不需要强制转换为
void*
,可以将大小重写为
sizeof(*aiorp)
,以遵循常见模式。)

但你为什么要这么做?这就是控件struct,它现在包含了大量(无意义的)数据,这些数据由
'a'
字节组成,但后面显式覆盖的值除外

难怪您会得到一个
EINVAL
,根据手册页:
aiorp->aio\u erqprio
0x6161
,其中“一个或多个
aio\u offset
aio\u reqprio
无效”

您想将数据缓冲区设置为包含
'a'
s:

memset(buf, 'a', count);

(但假定您的<代码> AIOCB < /C>结构被分配在本地存储中,在代码>主代码< /代码>中,因此未初始化,您可以考虑<代码> MyStuts<代码>将其全部调用到上面的调用。

memset(buf, 'a', count);