0

What is the problem in this line

char* buffer = (char*)malloc(height * widthStep * 1.2);

when I try to compile it I can't and get following message

...
PNGSave.h(144): error C2220: warning treated as error - no 'object' file generated
PNGSave.h(144): warning C4244: 'argument': conversion from 'double' to '::size_t', possible loss of data
...

I tried to disable warning treatment in project c++ -> treatment (NO) and also tried to add #pragma around this line of code but it doesn't help.

what am I doing wrong?

Sirop4ik
  • 4,543
  • 2
  • 54
  • 121
  • Consider reading [in what chases one should be using `malloc` in C++](https://stackoverflow.com/questions/184537/in-what-cases-do-i-use-malloc-and-or-new). Prefer the `new` operator, or even better, high level constructs such as `std::vector`. – E_net4 Jul 16 '20 at 08:17

3 Answers3

3

As others have said, the problem is that you have to pass malloc and integer because it doesn't make sense to allocate e.g. 1.2 bytes.

Even with this message, you code would normally compile and run: it is a warning, not an error. But you must have turned on treating warnings as errors (the clue is that the error message explicitly tells you this!), which is why it's not compiling at all. If you didn't have that turned on then the amount allocated would be rounded down from the number you asked, because that's how floating point numbers are converted to integers e.g.

char* x = malloc(3.7);

Will produce equivalent code to:

char* x = malloc(3);

If you are OK with that behaviour then you can avoid the warning - which in your case will allow the code to compile - by adding a cast:

char* buffer = (char*)malloc((size_t)(height * widthStep * 1.2));

Almost certainly you want to round up rather than down though, and probably don't want to use floating point numbers in your computation at all:

char* buffer = (char*)malloc((height * widthStep * 6 + 4) / 5);

More to the point, you probably don't want to be using malloc in C++ at all:

std::vector<char> buffer((height * widthStep * 6 + 4) / 5);
Arthur Tacca
  • 8,833
  • 2
  • 31
  • 49
0

The 1.2 literal is a double, but the malloc function requires ::size_t (at least integral type) value. Try fixing that.

Edit

Notice, that the program can allocate only a discrete amount of memory. It cannot allocate memory of 1.2 bytes.

E_net4
  • 27,810
  • 13
  • 101
  • 139
arsdever
  • 1,111
  • 1
  • 11
  • 32
0

You doing two things wrong.

First you aren't disabling warnings properly. 'Treat warnings as errors = NO' should fix this problem. I did a test on your code and it fixed it for me. So whatever you are trying you didn't do it right.

But the real thing you are doing wrong is trying to allocate a fractional amount of memory. You cannot allocate one fifth of a byte. It's such a strange idea that I'm not sure what to suggest instead. Anyway I'm sure you know what you are doing, so choose some method that converts your floating point number into an integer and the warning will go away.

john
  • 85,011
  • 4
  • 57
  • 81