Thread: size_t vs plain int

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    87

    size_t vs plain int

    Just to clarify:

    we use size_t when we deal w/ size and we want to be able to express largest size for a variable like in a calculation or data structure so the size is ALWAYS guaranteed to be able to represent 2^32 distinct positive integers (since int is typically 4B and size_t is typedef for unsigned int so no neg representations.) We should stick to size_t for portability (b/c of size issues across different machines where size is too little or something) and naturally like in loops to access indices so
    Code:
    for ( size_t i = 0; i < size; ++i )
      cout << list[i] << " , ";
    VS

    we use int when we want to have negatives so the 2^32 distinct values is halved, so half of the values are neg, the other pos.

    Also to clarify, the largest base-2 representation of a 32-bit size_t would be: 11111111111111111111111111111111 so whatever that is in base-10. But w/ the case of int (b/c it is signed), we largest base-10 value is half what that binary is (32 1's).

    Am I correct?
    Last edited by monkey_c_monkey; 07-11-2012 at 03:19 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. plain tree traversal
    By kmel in forum C++ Programming
    Replies: 2
    Last Post: 02-18-2008, 12:15 PM
  2. file i/o and plain old bad luck
    By rokenrol in forum C Programming
    Replies: 7
    Last Post: 07-09-2006, 02:16 PM
  3. plain pong
    By lambs4 in forum Game Programming
    Replies: 16
    Last Post: 10-10-2004, 01:49 PM
  4. Plain Window!
    By Shakespeare in forum Windows Programming
    Replies: 1
    Last Post: 01-20-2002, 12:35 PM
  5. Tabs in a plain Dialog
    By jinx in forum Windows Programming
    Replies: 0
    Last Post: 12-01-2001, 06:29 PM