implicit conversion changes signedness 'int" to 'unsigned int"

TommyLan picture TommyLan · Feb 11, 2015 · Viewed 15.5k times · Source

I was using clang++ to compile the program and I need to compile it with no error in clang++. I got no error with other compilers.

The error line in the code is

memset(grid_, 0, sizeof(int) * x_quadrants * y_quadrants);

The whole function is like this:

Ocean::Ocean(int num_boats, int x_quadrants, int y_quadrants)
{
  grid_ = new int[x_quadrants * y_quadrants];
  memset(grid_, 0, sizeof(int) * x_quadrants * y_quadrants);
  x_quadrants_ = x_quadrants;
  y_quadrants_ = y_quadrants;
  boats_ = new Boat[num_boats];
  num_boats_ = num_boats;

  stats_.hits = 0;
  stats_.misses = 0;
  stats_.duplicates = 0;
  stats_.sunk = 0;
}

I am using memset so I won't get garbage value output when test with different driver. There's no need to provide command line for clang because I'm not allowed to change it.

Answer

Cheers and hth. - Alf picture Cheers and hth. - Alf · Feb 11, 2015

Replace

grid_ = new int[x_quadrants * y_quadrants];
memset(grid_, 0, sizeof(int) * x_quadrants * y_quadrants);

with just

grid_ = new int[x_quadrants * y_quadrants]();

Note the parenthesis, that tells the compiler you want this zero-initialized (or really value-initialization, which reduces to zero-initialization here).

Even better, use a std::vector instead of this dangerous DIY scheme.