Like Tree2Likes
  • 2 Post By CornedBee

size_t or unsigned int

This is a discussion on size_t or unsigned int within the C++ Programming forums, part of the General Programming Boards category; I wanted to know what's the diff between unsigned int and size_t. To make my point clear to you, I ...

  1. #1
    Registered User Dr.Paneas's Avatar
    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
    2,769
    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.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  3. #3
    Registered User Dr.Paneas's Avatar
    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,893
    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.
    Salem and stahta01 like this.
    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-27-2008, 12:34 AM
  3. convert from size_t to unsigned int
    By George2 in forum C Programming
    Replies: 3
    Last Post: 07-28-2007, 03: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, 10:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21