Best Way to Handle Unknown Data Sizes?

Discussion in 'C++' started by Mike Copeland, Apr 13, 2012.

  1. I have a number of functions similar to the code below that are
    starting to cause problems when incoming data exceeds the limit (e.g.
    1024) imposed by the code. Rather than select another arbitrary limit
    and have the problem occur later when larger data appears, I'd like to
    fix the issue the right way. I'm unsure how to do this...
    Please advise. TIA

    char* TextFunctions::removeCR(char *s) // Remove C/R from data string
    {
    static char w[1024];
    int n;
    char *p;

    strcpy (w, s);
    n = strlen(w);
    p = strrchr(w, char(10));
    if(p) w[p-w] = '\0';

    return w;
    } // remove CR
     
    Mike Copeland, Apr 13, 2012
    #1
    1. Advertising

  2. Mike Copeland

    Ian Collins Guest

    On 04/14/12 10:03 AM, Mike Copeland wrote:
    > I have a number of functions similar to the code below that are
    > starting to cause problems when incoming data exceeds the limit (e.g.
    > 1024) imposed by the code. Rather than select another arbitrary limit
    > and have the problem occur later when larger data appears, I'd like to
    > fix the issue the right way. I'm unsure how to do this...
    > Please advise. TIA
    >
    > char* TextFunctions::removeCR(char *s) // Remove C/R from data string
    > {
    > static char w[1024];
    > int n;
    > char *p;
    >
    > strcpy (w, s);
    > n = strlen(w);
    > p = strrchr(w, char(10));
    > if(p) w[p-w] = '\0';
    >
    > return w;
    > } // remove CR


    Use C++ strings.

    --
    Ian Collins
     
    Ian Collins, Apr 13, 2012
    #2
    1. Advertising

  3. Mike Copeland

    copx Guest

    On 14.04.2012 00:03, Mike Copeland wrote:
    > I have a number of functions similar to the code below that are
    > starting to cause problems when incoming data exceeds the limit (e.g.
    > 1024) imposed by the code. Rather than select another arbitrary limit
    > and have the problem occur later when larger data appears, I'd like to
    > fix the issue the right way. I'm unsure how to do this...
    > Please advise. TIA
    >
    > char* TextFunctions::removeCR(char *s) // Remove C/R from data string
    > {
    > static char w[1024];
    > int n;
    > char *p;
    >
    > strcpy (w, s);
    > n = strlen(w);
    > p = strrchr(w, char(10));
    > if(p) w[p-w] = '\0';
    >
    > return w;
    > } // remove CR


    That's C-style code, not really idiomatic C++. Plus, it sounds and looks
    like homework.

    But well, I will give you a few pointers (pun intended):

    In C-style code you would dynamically allocate, reallocate, and
    eventually free memory as needed. The related functions are: malloc(),
    realloc(), and free() -- do a web search

    In "modern C++" you are supposed to use the container types in the STL.
    Web search for: std::string and std::vector
     
    copx, Apr 13, 2012
    #3
  4. Mike Copeland

    Jorgen Grahn Guest

    On Fri, 2012-04-13, Mike Copeland wrote:
    > I have a number of functions similar to the code below that are
    > starting to cause problems when incoming data exceeds the limit (e.g.
    > 1024) imposed by the code. Rather than select another arbitrary limit
    > and have the problem occur later when larger data appears, I'd like to
    > fix the issue the right way. I'm unsure how to do this...
    > Please advise. TIA
    >
    > char* TextFunctions::removeCR(char *s) // Remove C/R from data string
    > {
    > static char w[1024];
    > int n;
    > char *p;
    >
    > strcpy (w, s);
    > n = strlen(w);
    > p = strrchr(w, char(10));


    Something (apart from the function's design) is wrong here -- ASCII CR
    (not "C/R") is decimal 13. LF is 10.

    And if you mean C++ '\n', say '\n' instead of hardcoding a numeric
    value.

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
     
    Jorgen Grahn, Apr 13, 2012
    #4
  5. Mike Copeland

    red floyd Guest

    On 4/13/2012 3:42 PM, Jorgen Grahn wrote:
    > On Fri, 2012-04-13, Mike Copeland wrote:
    >> I have a number of functions similar to the code below that are
    >> starting to cause problems when incoming data exceeds the limit (e.g.
    >> 1024) imposed by the code. Rather than select another arbitrary limit
    >> and have the problem occur later when larger data appears, I'd like to
    >> fix the issue the right way. I'm unsure how to do this...
    >> Please advise. TIA
    >>
    >> char* TextFunctions::removeCR(char *s) // Remove C/R from data string
    >> {
    >> static char w[1024];
    >> int n;
    >> char *p;
    >>
    >> strcpy (w, s);
    >> n = strlen(w);
    >> p = strrchr(w, char(10));

    >
    > Something (apart from the function's design) is wrong here -- ASCII CR
    > (not "C/R") is decimal 13. LF is 10.
    >
    > And if you mean C++ '\n', say '\n' instead of hardcoding a numeric
    > value.
    >

    And if he really means CR, then he should use '\r'
     
    red floyd, Apr 14, 2012
    #5
  6. Mike Copeland

    Ian Collins Guest

    Re: Best Way to Handle Unknown Data Sizes? 

    On 04/14/12 12:16 PM, @Jeff-Relf.Me wrote:
    > [ Reposting; this one uses free(), not realloc() ]
    >
    > char* Upâ‹…Toâ‹…LF( char *Bâ° ) {
    > // From (Bâ°), return a string up to,
    > // but not including, any LineFeed ( 10 ).
    > //
    > // If possible, rewrite this using a temporary heap;
    > // so the heap can be destroyed,
    > // instead of calling free() and/or realloc().
    >
    > static int Sz⋅Buff = 1024 ; static char *B¹ ; char *LF; int Len;
    >
    > if ( LF = strchr( Bâ°, 10 ), !LF ) // Do nothing.
    > return Bâ° ;
    >
    > if ( Len = LF - Bâ° + 1, Len > Szâ‹…Buff )
    > // Free the old buffer, alloc a larger one.
    > Sz⋅Buff = Len, free( B¹ ), B¹ = 0 ;
    >
    > if ( !B¹ ) // Create the buffer.
    > B¹ = (char*)malloc( Sz⋅Buff );
    >
    > sprintf( B¹, "%.*s", --Len, BⰠ); return B¹; }


    What is this nonsense?

    --
    Ian Collins
     
    Ian Collins, Apr 14, 2012
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alan Silver
    Replies:
    4
    Views:
    342
    Alan Silver
    Feb 16, 2005
  2. ramu
    Replies:
    2
    Views:
    354
    Dann Corbit
    Nov 16, 2009
  3. riccardo
    Replies:
    4
    Views:
    338
    Malcolm McLean
    Nov 1, 2010
  4. Vincent Arnoux
    Replies:
    1
    Views:
    255
    Arnaud Bergeron
    Aug 11, 2006
  5. Replies:
    4
    Views:
    179
    Joe Smith
    May 4, 2007
Loading...

Share This Page