10 Replies - 9850 Views - Last Post: 13 December 2012 - 12:19 PM

#1 nuclearfroggy  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 82
  • View blog
  • Posts: 167
  • Joined: 04-August 08

Use of Unsigned

Posted 12 July 2012 - 01:01 PM

Today I started reading through Stroustrup's "The C++ Programming Reference", and 4.4 (pg 73 in my copy) got me thinking about how I use unsigned. He states "Using an unsigned instead of an int to gain one more bit to represent positive integers is almost never a good idea. Attempts to ensure that some values are positive by declaraing variables unsigned will typically be defeated by the implicit conversion rules".

I'm aware of the nasty stuff that can happen if you mix unsigned's and signed's, and I tend to only ever use unsigned for integers, usually for something like array indices where the value should never be negative, rather than to squeeze another bit.

However, I can see that if an unsigned value is expected, and the user supplies a negative value, you'll get some gigantic number which is essentially garbage. If you then try to access an array or something similar the result will be the same with some out of bound stuff going on, or some allocation will fail. Thinking about it though, with the signed value it could be easier to check for this, because you'll have a negative rather than a huge spurious number.

An example of the kind of place I used unsigned:
 
template <typename T>
matrix<T>::matrix(const unsigned int r, const unsigned int c) : 
rows(r), columns(c)
{
	el = new T *[rows];
	for (unsigned int i = 0; i < rows; i++)
		el[i] = new T[columns];
}

Stylistically I prefer using unsigned because I'm explicitly declaring: this value must be positive, but I can see how this could also cause problems. If a function has unsigned parameters then you know at a glance the value passed must be positive, but then this is probably apparent from the parameter anyway e.g size, index. I guess the question is, what are others' views on using unsigned integers, what is done in practice?

Is This A Good Question/Topic? 0
  • +

Replies To: Use of Unsigned

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,437
  • Joined: 25-December 09

Re: Use of Unsigned

Posted 12 July 2012 - 01:40 PM

I really don't make much of an issue of using unsigned or signed types for indexes or sizes unless I am going to interface with one of standard C++ container classes. Then I will usually use a size_t because that is what the standard containers use. If I am unsure how large these indexes or sizes will be I will also use a size_t, because then I am guaranteed to have a large enough size to hold the largest possible size by the standard. Otherwise I will normally use a signed value.

Jim
Was This Post Helpful? 1
  • +
  • -

#3 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,466
  • Joined: 05-May 12

Re: Use of Unsigned

Posted 12 July 2012 - 01:52 PM

For indexing and size of containers, I've been trying to train myself to use size_t.

For bit twiddling (and's, or's, xor's), try to make sure that I'm using unsigned if at least to signal intent to the reader that all bits of the type are significant.
Was This Post Helpful? 3
  • +
  • -

#4 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 687
  • View blog
  • Posts: 2,376
  • Joined: 31-December 10

Re: Use of Unsigned

Posted 12 July 2012 - 02:24 PM

View PostSkydiver, on 12 July 2012 - 04:52 PM, said:

For indexing and size of containers, I've been trying to train myself to use size_t.

This shouldn't be too hard if you have the warning level set high on your compiler and never leave any warnings.
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,466
  • Joined: 05-May 12

Re: Use of Unsigned

Posted 12 July 2012 - 02:29 PM

View Postvividexstance, on 12 July 2012 - 02:24 PM, said:

View PostSkydiver, on 12 July 2012 - 04:52 PM, said:

For indexing and size of containers, I've been trying to train myself to use size_t.

This shouldn't be too hard if you have the warning level set high on your compiler and never leave any warnings.


Yup! Unfortunately, those unbalanced #pragma warning disable ... that others stick in their header files that can make for an "interesting" afternoon.

What was the quote from Firefly? "...very special level of Hell."

This post has been edited by Skydiver: 12 July 2012 - 02:36 PM

Was This Post Helpful? 1
  • +
  • -

#6 nuclearfroggy  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 82
  • View blog
  • Posts: 167
  • Joined: 04-August 08

Re: Use of Unsigned

Posted 12 July 2012 - 03:40 PM

Ah, I completely forgot about size_t, that definitely seems more appropriate than unsigned int in the given instance.

From what I understand though, I should use unsigned int much more sparingly than I have done. I guess I had a strange preconception it was similar to const correctness in that I should put it wherever values must be positive.
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is offline

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,437
  • Joined: 25-December 09

Re: Use of Unsigned

Posted 12 July 2012 - 08:04 PM

Normally I prefer to use signed int because "in the olden days" a signed int was usually more efficiently handled by the processors than their unsigned counterparts. I don't really know it this still holds but old habits die hard. I was also was taught to use a char for known small loops as it would use less memory and possible faster execution. And to prefer float to double because of speed and memory size concerns. Today I rarely use a char for a loop counter and very rarely use a float, because I'm not usually worried about the memory size issue and any speed penalty is so unnoticeable I don't worry about it either. I will say that I don't write games, where speed is much more an issue. Also most of the applications I have been involved with, precision is much more an issue than either speed or memory usage.

Jim

This post has been edited by jimblumberg: 12 July 2012 - 08:27 PM

Was This Post Helpful? 1
  • +
  • -

#8 nuclearfroggy  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 82
  • View blog
  • Posts: 167
  • Joined: 04-August 08

Re: Use of Unsigned

Posted 13 July 2012 - 07:43 AM

Thanks for your insight, sometimes I probably focus too hard on the little details, but I'm determined to develop a consistent and clear style. I should probably look at joining an open source project where I can contribute and see lots of professional level code.
Was This Post Helpful? 0
  • +
  • -

#9 nazzie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 31-July 12

Re: Use of Unsigned

Posted 31 July 2012 - 04:46 AM

Finally a thread about unsigned int. I have read that its the same as uint, right?

my question is do i need to have a unique header file for the uint datatypes?

i have a function that I have to make a program for and it got me confused.

//! Converts a two-byte, 3-digit, BCD number into a binary value.
extern uint16_t BcdBin( const uint8_t* const pBcdBin);

Was This Post Helpful? 0
  • +
  • -

#10 jimblumberg  Icon User is offline

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,437
  • Joined: 25-December 09

Re: Use of Unsigned

Posted 31 July 2012 - 07:04 AM

No you don't need any special header to use unsigned types. However to use things like uint16_t, uint8_t, uinit32_t you will need to include the header that defines these types. In a C program you would need to include the <stdint.h> include file. Also note it is possible that your compiler doesn't support one or more of these implementation defined types, see this quote from the C11 standard:

Quote

These types are optional. However, if an implementation provides integer types with
widths of 8, 16, 32, or 64 bits, no padding bits, and (for the signed types) that have a
two’s complement representation, it shall define the corresponding typedef names.


Jim
Was This Post Helpful? 0
  • +
  • -

#11 antolyevich  Icon User is offline

  • New D.I.C Head

Reputation: -6
  • View blog
  • Posts: 12
  • Joined: 02-September 12

Re: Use of Unsigned

Posted 13 December 2012 - 12:19 PM

Personally I only use unsigned to indicate to myself that the variable should only be positive, and it just serves as an easy way for me to remember positiveness. Also, the consequences of mixing unsigned and negative are a good indicator if something is being passed incorrectly. So, for me, it makes little difference one way or the other.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1