简体   繁体   English

将参数传递给函数(int传递给size_t)

[英]Passing arguments to function (int to size_t)

In function writeDmpFile I'm calling writeFile . 在函数writeDmpFile我叫writeFile

Please see the code and the comments. 请查看代码和注释。

My problem is size. 我的问题是尺寸。 In writeDmpFile I see it is 380316. I try to pass it to writeFile. writeDmpFile我看到它是380316。我尝试将其传递给writeFile。

But while stepping here I'm getting very large number(3832907636190596508). 但是当我走到这里时,我得到了很多(3832907636190596508)。

What am I doing wrong. 我究竟做错了什么。 I'd expect, that 380316 will be passed. 我希望能够通过380316。

int writeFile(char *name,  unsigned char *buff, size_t *size,const char *dir )
{
  FILE * pFile;
  chdir (dir);
  pFile = fopen ( name, "wb");
  //(gdb) print *size
  //$5 = 3832907636190596508
  fwrite (buff , sizeof(unsigned char), *size, pFile);
  fclose (pFile);

  return 1;
}
int writeDmpFile(GTree *tree, char *filename)
{
  char dmpfilename[32];

  dmpfilename[0] ='\0';
  dmpParams_t params;
  params.buff[0]   ='\0';
  int size =0;
  params.size=&size ;
  g_tree_foreach(tree, (GTraverseFunc)writeDmpFileLine, &params);
  sprintf (dmpfilename, "InstrumentList_FULL.csv_%.*s", 15, filename);
  //here (gdb) print size
  //$1 = 380316
  writeFile(dmpfilename,  ( unsigned char *)params.buff, ( size_t *)&size , dmpdir);//(size_t *)params.size, dmpdir);
}
( size_t *)&size

This is bad as size_t and int are different types with different representations. 这很不好,因为size_tint是具有不同表示形式的不同类型。 size_t is an alias for an unsigned integer type, often unsigned long . size_t是无符号整数类型(通常为unsigned long )的别名。 Here you should declare size as a size_t variable. 在这里,您应该将size声明为size_t变量。

There are two fundamental problems here. 这里有两个基本问题。

  1. You declared size to be an int , but really it needs to be a size_t . 您声明sizeint ,但实际上它必须为size_t Change its type to size_t . 将其类型更改为size_t
  2. You should not be passing the address of size to writeFile . 您不应将size的地址传递给writeFile You should be passing it as a const value param. 您应该将其作为const值参数传递。 You don't need to modify it, nor do you. 您不需要修改它,也不需要。 So make that clear in the signature of the function. 因此,请在函数签名中明确说明。

As a general rule, any time you encounter a type mismatch compiler error and are tempted to suppress the error with a cast, you are almost certainly making a mistake. 通常,每当遇到类型不匹配的编译器错误并想通过强制转换来抑制该错误时,几乎肯定会犯一个错误。 The compiler reported an error because you made a mistake. 编译器报告错误,因为您犯了一个错误。 Sure you can shut the compiler up, but experience tells me that the compiler is usually right, and we humans are very good at making mistakes. 当然,您可以关闭编译器,但经验告诉我,编译器通常是正确的,而且我们的人都很擅长犯错。

So, don't suppress type mismatch compiler errors with casts. 因此,不要通过强制转换来抑制类型不匹配的编译器错误。 Seek to understand why the types don't match and thus resolve the problem. 试图了解为什么类型不匹配,从而解决问题。

Despite the question why the writeFile() function insists on takeing the size via its address, there are two possiblities to solve this issue: 尽管有一个问题,为什么writeFile()函数坚持通过其地址获取size ,但是有两种方法可以解决此问题:

  1. Declare an intermediate size_t -typed variable: 声明一个中间的size_t型变量:

     { size_t _s = size; writeFile(dmpfilename, (unsigned char *) params.buff, &_s, dmpdir); } 
  2. Or use a (nice) compound statement: 或使用(好的)复合语句:

     writeFile(dmpfilename, (unsigned char *) params.buff, &((size_t){size}), dmpdir); 
( size_t *)&size

This is a big mistake which you face while migrating from 32bit system to 64bit system. 从32位系统迁移到64位系统时,这是一个很大的错误。 On 32 bit system this code may work properly as both int and size_t will be of 32 bit. 在32位系统上,此代码可能会正常工作,因为int和size_t均为32位。 but on 64bit system int will be 32 bit but size_t may be of size 64 bit. 但在64位系统上,int将为32位,但size_t的大小可能为64位。

Basically size_t was designed to hold pointer arithmetic. 基本上,size_t旨在容纳指针算术。 This is the type which is returned from sizeof operator. 这是从sizeof运算符返回的类型。

For your problems solution better you convert int size =0; 为了更好地解决问题,可以将int size =0;转换为int size =0; to size_t size = 0 size_t size = 0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM