0

In resize, when using fseek(), I used -(old_Width * sizeof(RGBTRIPLE)) to make the cursor go back to start of the line, however this returned an error : runtime error: negation of 36 cannot be represented in type 'unsigned long'

The problem was fixed when I replaced the sizeof(RGBTRIPLE) with 3, but I'm not quite sure why...

Would someone please mind explaining? Thanks very much in advance

1
  • Hmmm... tried to duplicate the error unsuccessfully. Can you please edit your question and add the complete line of code with the error, and the declarations for every var that you used in that line?
    – Cliff B
    Commented Jun 20, 2017 at 19:28

1 Answer 1

0

sizeof returns a size_t, which is unsigned, while 3 could be signed or unsigned. Not sure which type your old_Width is, but the product of unsigned variables is probably unsigned itself.

I think in my case the compiler did put the result into an unsigned long (interpreting the negative result as an incredibly big number) and the runtime complained about not being able to put that into a long as specified by fseek. Changing parentheses did the trick for me, taking a different path of type conversions.

3
  • I see, so it thinks that instead of a negative number, I'm asking it to shift by a much larger positive number which goes beyond the size of a normal "long" and so it throws an error. Is that correct? Btw, thanks very much for your earlier response Commented Jun 21, 2017 at 11:06
  • Yeah, that's what it seems to do in certain situations. I haven't fully understood when it does it one way or the other, but everything is based on the first bit being interpreted as a most significant bit of the number, or the sign (which is the difference between 0..255 for unsigned char and -128..127 for signed char, 255 of unsigned and -1 of signed have the same representation in memory)
    – Blauelf
    Commented Jun 21, 2017 at 12:24
  • alright thanks very much for all your help :) Commented Jun 21, 2017 at 15:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .