Page 2 of 2 FirstFirst 12
Results 11 to 18 of 18

Thread: [C++] When should I use size_t?

  1. #11
    Join Date
    Aug 2007
    Location
    Novocastria, Australia
    Beans
    751
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: [C++] When should I use size_t?

    Quote Originally Posted by dribeas View Post
    Now the rationale: whenever you ask for the n-th element, that has to be compared against the size of the list. If you index and size types are different (size or signed-ness) you can run into trouble. As an example, assume that for some implementation with size_t being unsigned int (32bits) you create a list/vector with 2^31 + 1 elements. If you used signed ints as indexes you could not be able to reach the last element with a positive index.
    That's sort of what I was thinking... That does make sense really. Position in the list is kinda related to size.

    I would ask my proff for my programming class directly, but 3 day weekend, so I wont see him til Tuesday.

  2. #12
    Join Date
    Apr 2007
    Location
    (X,Y,Z) = (0,0,0)
    Beans
    3,715

    Re: [C++] When should I use size_t?

    Though it seems you have solved your problem by finding an alternative. For you to know:

    size_t is just an unsigned int.

    Why should you use it instead of int when referring to sizes or array indices? To avoid having negative sizes or positions and subsequent illegal memory access... By using size_t you don't have to check if the variable is > 0 because it will always be by definition.

    But, size_t can be treacherous. If you happen to assign, e.g -2 to a size_t variable, the number will be substracted from the size_t's limit and you'll probably get a big positive number that will also result in illegal memory access...

    size_t may also be useful when you need to use integer's memory space at full. You surely know that if in your platform (signed) int is 4-bytes long, you'll be able to use 2-byte long positives and 2-byte long negatives (aprox.). With size_t, unsigned int, you can use 4-byte long positive numbers, specially useful when you certainly know that some variable will never hit a negative value.

  3. #13
    Join Date
    Aug 2008
    Location
    Madrid
    Beans
    52

    Re: [C++] When should I use size_t?

    Quote Originally Posted by nvteighen View Post
    size_t may also be useful when you need to use integer's memory space at full. You surely know that if in your platform (signed) int is 4-bytes long, you'll be able to use 2-byte long positives and 2-byte long negatives (aprox.). With size_t, unsigned int, you can use 4-byte long positive numbers, specially useful when you certainly know that some variable will never hit a negative value.
    Could you elaborate?

    As far as I know, if an int is 4-bytes long, that is, 32-bit long, you can use 31-bit positives and 31-bit negatives OR 32-bit unsigneds.

    What do you mean by 2-byte long positives and 2-byte long negatives.

  4. #14
    Join Date
    Apr 2007
    Location
    (X,Y,Z) = (0,0,0)
    Beans
    3,715

    Re: [C++] When should I use size_t?

    Quote Originally Posted by mujambee View Post
    Could you elaborate?

    As far as I know, if an int is 4-bytes long, that is, 32-bit long, you can use 31-bit positives and 31-bit negatives OR 32-bit unsigneds.

    What do you mean by 2-byte long positives and 2-byte long negatives.
    EDIT: Oh man... now I realize what was my mistake.. yeah... 31 bits = 2 bytes... great! Sorry.

    It's because, to represent negative values, you usually use the Two's complement (see http://en.wikipedia.org/wiki/Two%27s_complement).

    What really happens is that, when you have a signed data type, you have to hold the sign somewhere. Conventionally, the first bit is used to represent the sign: 0 is +, 1 is -. So, in a (signed char) value, your number will have to fit in 31-bits, so the real upper limit is reduced to the half... to both sides of zero. In a unsigned value, all 32-bits are used to represent the value.
    Last edited by nvteighen; August 23rd, 2008 at 04:48 PM.

  5. #15
    Join Date
    Aug 2008
    Location
    Madrid
    Beans
    52

    Re: [C++] When should I use size_t?

    Yes, I already knew that.

    What puzzles me is "2-byte longs". What do you mean by 2 bytes? 31 bits is not 2 bytes, is almost 4!!

  6. #16
    Join Date
    Apr 2007
    Location
    (X,Y,Z) = (0,0,0)
    Beans
    3,715

    Re: [C++] When should I use size_t?

    Quote Originally Posted by mujambee View Post
    Yes, I already knew that.

    What puzzles me is "2-byte longs". What do you mean by 2 bytes? 31 bits is not 2 bytes, is almost 4!!
    See my edit above. It just means I'm stupid today

  7. #17
    Join Date
    Aug 2008
    Location
    Madrid
    Beans
    52

    Re: [C++] When should I use size_t?


  8. #18
    Join Date
    Jul 2008
    Location
    Dublin, Ireland
    Beans
    633
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: [C++] When should I use size_t?

    Quote Originally Posted by nvteighen View Post
    size_t is just an unsigned int.
    ...
    But, size_t can be treacherous. If you happen to assign, e.g -2 to a size_t variable, the number will be substracted from the size_t's limit and you'll probably get a big positive number that will also result in illegal memory access...
    size_t is not required to be unsigned int, but rather an unsigned integer type. Well, that is taking things to the limit: in most cases it is unsigned int.

    Sadly enough assignment of a negative integer into an unsigned type does not fire warnings... But nevertheless, you only need to care for numbers greater than some limit, which you have to care about in both cases.

Page 2 of 2 FirstFirst 12

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •