Thread: size_t or unsigned int

  1. #1
    Registered User
    Join Date
    Nov 2009
    Location
    GR
    Posts
    12

    size_t or unsigned int

    I wanted to know what's the diff between unsigned int and size_t. To make my point clear to you, I 've already tried to sizeof both types, but they both returned the same size. One more thing, I've noticed that lot's of programmers tend to declare arryas in size_t. What's the catch and the whole meaning behind this ?

    Thanks

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by Dr.Paneas View Post
    I wanted to know what's the diff between unsigned int and size_t. To make my point clear to you, I 've already tried to sizeof both types, but they both returned the same size. One more thing, I've noticed that lot's of programmers tend to declare arryas in size_t. What's the catch and the whole meaning behind this ?

    Thanks
    NOTE: unsigned int is NOT always the same as size_t.

    When you are using an system function that is supposed to use size_t always try to use size_t; only if it fails to work try "unsigned int" or "unsigned long int".

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Nov 2009
    Location
    GR
    Posts
    12
    Thanks for the quick reply Tim.

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    size_t is the type of the sizeof operator. It's used to measure the size of an entity in chars (usually bytes). It is supposed to be defined as an unsigned integer type (short (seems unlikely), standard, long, long long). To be portable you use size_t.

    It's also a mnemonic showing that the variable is being used to represent a size, as opposed to some other unsigned entity. malloc takes a size_t. strlen and many other functions in string.h return or receive it.

    The SIZE_MAX macro in stdint.h defines the maximum value of size_t, which is the maximum size in chars of any object since it's the maximum value that sizeof can return.

    C++ also has the size_type type defined for its standard containers. Many people use size_t instead of size_type, but obviously they're not guaranteed to be the same.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    To make my point clear to you, I 've already tried to sizeof both types, but they both returned the same size.
    Try building a as a 64-bit application.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. size_t
    By bos1234 in forum C Programming
    Replies: 1
    Last Post: 03-04-2011, 05:43 AM
  2. signed size_t/unsigned ptrdiff_t?
    By robwhit in forum C Programming
    Replies: 3
    Last Post: 09-26-2008, 11:34 PM
  3. convert from size_t to unsigned int
    By George2 in forum C Programming
    Replies: 3
    Last Post: 07-28-2007, 02:55 AM
  4. What is size_t?
    By Mikro in forum C++ Programming
    Replies: 2
    Last Post: 02-27-2005, 05:07 AM
  5. size_t
    By volk in forum C++ Programming
    Replies: 2
    Last Post: 07-04-2003, 09:06 PM