Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Best Way to Handle Unknown Data Sizes?

Reply
Thread Tools

Best Way to Handle Unknown Data Sizes?

 
 
Mike Copeland
Guest
Posts: n/a
 
      04-13-2012
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

 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      04-13-2012
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
 
Reply With Quote
 
 
 
 
copx
Guest
Posts: n/a
 
      04-13-2012
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




 
Reply With Quote
 
Jorgen Grahn
Guest
Posts: n/a
 
      04-13-2012
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 .
 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      04-13-2012
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'


 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      04-14-2012
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Best practices for unknown data static/dynamic allocation origin riccardo C Programming 4 11-01-2010 07:23 AM
how does printf handle an unknown number of arguments InuY4sha C Programming 6 02-06-2009 12:12 PM
PERL OOP newbie question: What is the best way to handle array data? lovellj@mcmaster.ca Perl Misc 4 05-04-2007 04:53 AM
Assign an unknown value to an unknown variable Vincent Arnoux Ruby 1 08-11-2006 06:12 PM
What's the best way to handle showing/editing this data? Alan Silver ASP .Net 4 02-16-2005 06:23 PM



Advertisments