Thread: New string functions

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654

    New string functions

    I took the liberty of creating some new string functions for C.
    The goal with these was:
    - Safety. No buffer overflows.
    - If there's not enough room in the buffer to complete the operation, return the space needed.
    - The ability to use C++-like strings. It supports dynamically allocated strings that will handle memory management to make sure there's enough room for operations.
    - Performance. Instead of using the old C-style way of using null-terminators for getting length, it keeps track of the length itself, leading to speedups (albeit slightly more memory use).
    - Slight C++-compatibility (such as constructors, destructors and an iostream overload). Nothing more.

    So here is the code:
    Snip: Deleted; new code below.

    And now for criticism, please.
    The functions can handle non-dynamic strings, in which case it will fail if no space is available. The 3rd parameter allows the caller to acquire the needed space for the operation to complete.
    The strstr and strchr functions returns the position from the beginning of the string that the match was found. This is because returning a char* pointer to a dynamic string could lead to disastrous results. Therefore, to protect against mistakes, the position is returned and a char* pointer could be acquired from the original string, if required.
    Last edited by Elysia; 03-28-2009 at 04:46 AM. Reason: Saving space for new code
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  3. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  4. Badly designed n string functions?
    By anonytmouse in forum C Programming
    Replies: 3
    Last Post: 11-01-2003, 06:16 AM
  5. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM