0

If I am using an int as the array size when declaring a new array lint complains. What is the correct way to resolve this:

I could statically cast int to unsigned but that looks a bit odd...

What is the best way to resolve this without changing the use of an array and the need for integer as size?

MapItemPtr* pMapItems = new MapItemPtr[ (unsigned int)iRecordCount ];

Could do:

MapItemPtr* pMapItems = new MapItemPtr[static_cast<unsigned int>( iRecordCount )];

Is this the correct way to resolve the lint error: Line 3811 Error 737: Loss of sign in promotion from int to unsigned int

or is there a more elegant way?

4
  • 4
    You submit 7 questions, post no votes and accept no answers. Commented Nov 10, 2011 at 15:01
  • 4
    You are not contributing back to this community at present. Commented Nov 10, 2011 at 15:04
  • 1
    Wow I didn't know that. When somebody has answered I have always just replied with a thank you message. I didn't realise that I could do these things. I will go back to my posts and rectify. Commented Nov 10, 2011 at 15:11
  • Yep, you should always upvote the ones you like, and accept the one that best solves your problem (if any of them do). Commented Nov 10, 2011 at 15:29

2 Answers 2

6

Since iRecordCount is clearly intended to hold a nonnegative integer, why not just declare it as unsigned int in the first place?

Sign up to request clarification or add additional context in comments.

3 Comments

Indeed, it should be a size_t.
But if iRecordCount is a constant integral expression (as it must be, if it is used as the dimension of an array), lint is surely off base complaining about it here.
Yes I agree. Even when the variable is const int of value greater than 0 it still complains which I think is wrong. Variable cannot be changed as it is const. Perhaps lint doesn't evaluate the code properly in this situation?
2

The error is not the constness of the variable, as the lint said:

 : Line 3811 Error 737: Loss of sign in promotion from int to unsigned int.

You have a few options:

1 - static_cast<> int to unsigned int,
2 - (unsigned int*) c-style cast, but that should be avoided in c++ code.
3 - create a temporary unsigned int variable that will create the vector for you.

since you are just creating an array, the type of the variable could be size_t, declared in cstddef.

1 Comment

Thanks for all of your advice guys. I have simply changed the int to an unsigned int as this seems the most practical? I have marked ticked an answer. So how do I upvote now then? As you may be able to tell as a programmer I have pretty shocking IT skills haha.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.