Thread: Trying to get to the bottom of a myclass::size_type

  1. #1
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446

    Trying to get to the bottom of a myclass::size_type

    I have a class PDCstr that stores a null-terminated array of chtypes. A chtype is a 32bit formated character that can be represented in the screen. I use this class as a helper class for PDCurses (a portable curses implementation) daily usage.

    PDCstr, among other things, defines two subscript operator overloads and an observer function:
    Code:
    chtype& operator[] (const size_type idx) { return chtstr_[idx]; }
    const chtype& operator[] (const size_type idx) const { return chtstr_[idx]; }
    size_type size() const { return str_.size() );
    chtstr_ and str_ are defined as:
    Code:
    private:
        std::string str_;
        boost::shared_array<chtype> chtstr_;
    size_type above is currently being defined as:
    Code:
    public:
        typedef size_t size_type;
    I'm not sure if my typedef is the correct one anymore. I'm divided between what I have now and these two:
    Code:
    typedef std::string::size_type size_type;
    
    typedef std::ptrdiff_t size_type //boost way (and then assert as boost does)
    As a final note, I'm using the std::string (which is initialized from a constructor parameter) to build the null-terminated array of 32bit characters. That is, the array is a character-by-character 32bit representation of the 8bit std::string.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I wouldn't use ptrdiff_t. I've heard there are some problems with it, not the least that it's usually a 16-bit data type which cannot represent all possible values. ptrdiff_t is the type generated when you subtract two pointers (in C at least), but even there there are some problems, because the difference between two pointers can be greater than 2^16. I'd avoid ptrdiff_t if at all possible. But if Boost uses it, then, well . . .

    I would probably go with
    Code:
    typedef std::string::size_type size_type;
    if you know that std::strings will be used, otherwise just std::size_t.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by dwks View Post
    I wouldn't use ptrdiff_t. I've heard there are some problems with it, not the least that it's usually a 16-bit data type which cannot represent all possible values. ptrdiff_t is the type generated when you subtract two pointers (in C at least), but even there there are some problems, because the difference between two pointers can be greater than 2^16. I'd avoid ptrdiff_t if at all possible. But if Boost uses it, then, well . . .
    It's funny how many supposed "expert" sites out there tell people to use ptrdiff_t because "an int isn't guaranteed to be able to hold the difference between any two ARBITRARY pointers." Funny, because a ptrdiff_t isn't guaranteed to be able to do that either.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    How did you figure out that ptrdiff_t isn't good enough?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Interesting reading, robatino.

    I'll change to string::size_type, thanks dwks.

    > I'd avoid ptrdiff_t if at all possible. But if Boost uses it, then, well . . .

    I think boost uses it just so that it can implement an underflow assertion (< 0). I won't really need that. Contrary to boost shared_array, I will also be coding an at() function where proper range checking can then be done.

    Thanks again. string::size_type it is.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to make opengl draw in lighter colors ?
    By jabka in forum Game Programming
    Replies: 2
    Last Post: 12-17-2007, 06:12 AM
  2. Forced moves trouble!!
    By Zishaan in forum Game Programming
    Replies: 0
    Last Post: 03-27-2007, 06:57 PM
  3. OpenGL, loading BMP Textures?
    By Zeusbwr in forum Game Programming
    Replies: 12
    Last Post: 12-09-2004, 05:16 PM
  4. Odd 3D Invis Objects?
    By Zeusbwr in forum Game Programming
    Replies: 4
    Last Post: 12-07-2004, 07:01 PM
  5. Strip at bottom of HTML table
    By sean in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 05-17-2003, 07:27 AM