Thread: how to find out if a string is full?

  1. #1
    Registered User Tiago's Avatar
    Join Date
    Oct 2009
    Location
    Lisbon, Portugal
    Posts
    28

    how to find out if a string is full?

    Hi,

    Is it possible to find out if a chars string is full or half full?

    I want to use realloc after a malloc, but only if the chars string is almost full...

    TY

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What exactly does it mean for a string to be full or half full?

    EDIT:
    Quote Originally Posted by Tiago
    I want to use realloc after a malloc, but only if the chars string is almost full...
    Keep track of the number of characters in use (size) and the number of characters for which space is allocated (capacity). When the size is close enough to the capacity, increase the capacity.
    Last edited by laserlight; 05-25-2010 at 11:25 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User Tiago's Avatar
    Join Date
    Oct 2009
    Location
    Lisbon, Portugal
    Posts
    28
    Quote Originally Posted by laserlight View Post
    What exactly does it mean for a string to be full or half full?
    The size of a string is static, unless we change it... what I mean by full is with all the spaces or almost all the spaces occupied by non '\0' chars ...

  4. #4
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    Count characters until \0 is reached, and compare the count to the size of the string?

  5. #5
    Registered User Tiago's Avatar
    Join Date
    Oct 2009
    Location
    Lisbon, Portugal
    Posts
    28
    Quote Originally Posted by KBriggs View Post
    Count characters until \0 is reached, and compare the count to the size of the string?
    I believe it will spend too much time... is there any other way to get, for example, the penultimate character of a string faster?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Tiago
    I believe it will spend too much time... is there any other way to get, for example, the penultimate character of a string faster?
    As I said: keep track of the size and capacity.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    Well yes, you can reference it directly, but then you are ignoring the possibility that there is a \0 in the array somewhere before that character and that the one you are looking at is garbage.

    Or just do like laserlight said and keep track of any changes you make to the string as you go.

  8. #8
    Registered User Tiago's Avatar
    Join Date
    Oct 2009
    Location
    Lisbon, Portugal
    Posts
    28
    Quote Originally Posted by laserlight View Post
    As I said: keep track of the size and capacity.
    I had already thought of that, just wanted to see if there was an alternative... I'll do it that way...

    thank you both...

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    If you're not tracking the size already then there's a good chance that you're already in danger of a buffer overflow, or data truncation.
    It may go from 40% full to 10% overflowing in a single concatenation. To make sure that doesn't happen, you have to check that the initial length plus the length to concatenate is less than the current size. If not, you need to grow first.
    You can't just rely on checking the length after each concatenation.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help for my assigment
    By cloverdagreat in forum C Programming
    Replies: 16
    Last Post: 11-21-2009, 12:18 PM
  2. Unable to compare string with 'getter' returned string.
    By Swerve in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2009, 05:56 PM
  3. find a string based on the location of another string
    By rlilley in forum C Programming
    Replies: 3
    Last Post: 02-19-2009, 12:29 PM
  4. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  5. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 12:47 PM