Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it safe to use _msize with new[]?

Is it safe to use Microsoft specific _msize() function with new []?

Example:

  int* i = new int[100];      
  size_t s = _msize(i);    
  cout << "Size of the array in bytes: " << s << endl;
  delete [] i;

MSDN does describe just the usage with malloc & Co.

I've tested the code with Visual Studio 2010, and it looks to work! But i would like to know if there are some expected issues or any special cases?

like image 328
mem64k Avatar asked Dec 26 '22 20:12

mem64k


1 Answers

There could be a problem if someone overrides operator new for your type.

It is just as easy to write

const size_t s = 100;
int* i = new int[s];

or, if you really write C++

std::vector<int>   i(100);
like image 71
Bo Persson Avatar answered Jan 08 '23 02:01

Bo Persson