Velocity Reviews > function

# function

Bill Cunningham
Guest
Posts: n/a

 08-14-2011
Ben Bacarisse wrote:

> What an odd statement! Fortunately I don't need to know what you mean
> by it.

Brain fart! As usual sometimes I don't know what I've talking about.

Bill

Malcolm McLean
Guest
Posts: n/a

 08-15-2011
On Aug 13, 9:32*am, "io_x" <(E-Mail Removed)> wrote:
>
> why not 2x the buffer each time,
> and realloc to actual size at end for the input?
> it mess the malloc memory?
>

That's not too bad a strategy.

The problem is that if your buffer is very close to the system-imposed
limit, doubling may take it over that limit when in fact there are
enough resources to run your algorithm. Increasing by 10% is less
likely to do that.

Bill Cunningham
Guest
Posts: n/a

 08-28-2011
Malcolm McLean wrote:

> That's not too bad a strategy.
>
> The problem is that if your buffer is very close to the system-imposed
> limit, doubling may take it over that limit when in fact there are
> enough resources to run your algorithm. Increasing by 10% is less
> likely to do that.

Can you show me an example of code using malloc realloc and fread to use
this type of buffer?

Bill

Malcolm McLean
Guest
Posts: n/a

 08-29-2011
On Aug 28, 11:11*am, "Bill Cunningham" <(E-Mail Removed)> wrote:
> Malcolm McLean wrote:
>
> * * Can you show me an example of code using malloc realloc and freadto use
> this type of buffer?
>
> Bill

/*
assume that int is 32 bit, and that endianness is native. End of
file = end of data
fp - open file
N - return for number of integers read
Out of memory: returns 0, N = -1;
IO error: returns 0, N = -2;

Note: untested code. Might have bugs in it.
*/
{
int *temp;
int bufsize = 100;
int x;

goto outofmemory;
while(fread(&x, 4, 1, fp) == 1)
{
{
buffsize = bufsize + bufsize/10;
temp = realloc(answer, bufsize * 4);
if(!temp)
goto outofmemory;
}
}
if(ferror(fp))
outofmeomory:
*N = -1;
return 0;
*N = -2;
return 0;
}

Nobody
Guest
Posts: n/a

 08-29-2011
On Mon, 29 Aug 2011 05:00:38 -0500, Gordon Burditt wrote:

> Use of 16-bit or 32-bit chars might also be appropriate if the native
> character set is Unicode.

Not really. The question isn't the size of a "character", but the size of
the smallest "unit" of data which you wish to handle, as you can't have
any object smaller than a "char".

Which tends to mean that 16/32-bit "char" is reserved for highly
specialised architectures which don't need to deal with file formats,
network protocols, etc.

Bill Cunningham
Guest
Posts: n/a

 08-30-2011
Malcolm McLean wrote:
> /*
> read integers from a file
> assume that int is 32 bit, and that endianness is native. End of
> file = end of data
> fp - open file
> N - return for number of integers read
> Out of memory: returns 0, N = -1;
> IO error: returns 0, N = -2;
>
> Note: untested code. Might have bugs in it.
> */
> int *readintsfromfile(FILE *fp, int *N)
> {
> int *temp;
> int bufsize = 100;
> int x;
>
> answer = malloc(bufsize * 4);
> goto outofmemory;
> while(fread(&x, 4, 1, fp) == 1)
> {
> {
> buffsize = bufsize + bufsize/10;
> temp = realloc(answer, bufsize * 4);
> if(!temp)
> goto outofmemory;
> }
> }
> if(ferror(fp))
> outofmeomory:
> *N = -1;
> return 0;
> *N = -2;
> return 0;
> }

Excellent. Myself I might have used switch instead of goto or better yet
maybe confused myself with a string of if ,else if, and else statements. In
binary transfer would it be alright to use char* anyway? Transfer one byte
at 512, 1024 groups.

Bill

Ben Bacarisse
Guest
Posts: n/a

 08-30-2011
"Bill Cunningham" <(E-Mail Removed)> writes:

> Malcolm McLean wrote:
>> /*
>> read integers from a file
>> assume that int is 32 bit, and that endianness is native. End of
>> file = end of data
>> fp - open file
>> N - return for number of integers read
>> Out of memory: returns 0, N = -1;
>> IO error: returns 0, N = -2;
>>
>> Note: untested code. Might have bugs in it.
>> */
>> int *readintsfromfile(FILE *fp, int *N)
>> {
>> int *temp;
>> int bufsize = 100;
>> int x;
>>
>> answer = malloc(bufsize * 4);
>> goto outofmemory;
>> while(fread(&x, 4, 1, fp) == 1)
>> {
>> {
>> buffsize = bufsize + bufsize/10;
>> temp = realloc(answer, bufsize * 4);
>> if(!temp)
>> goto outofmemory;
>> }
>> }
>> if(ferror(fp))
>> outofmeomory:
>> *N = -1;
>> return 0;
>> *N = -2;
>> return 0;
>> }

>
> Excellent. Myself I might have used switch instead of goto [...]

How?

<snip>
--
Ben.

Bill Cunningham
Guest
Posts: n/a

 08-30-2011
Bill Cunningham wrote:
> Malcolm McLean wrote:
>> /*
>> read integers from a file
>> assume that int is 32 bit, and that endianness is native. End of
>> file = end of data
>> fp - open file
>> N - return for number of integers read
>> Out of memory: returns 0, N = -1;
>> IO error: returns 0, N = -2;
>>
>> Note: untested code. Might have bugs in it.
>> */
>> int *readintsfromfile(FILE *fp, int *N)
>> {
>> int *temp;
>> int bufsize = 100;
>> int x;
>>
>> answer = malloc(bufsize * 4);
>> goto outofmemory;
>> while(fread(&x, 4, 1, fp) == 1)
>> {
>> {
>> buffsize = bufsize + bufsize/10;
>> temp = realloc(answer, bufsize * 4);
>> if(!temp)
>> goto outofmemory;
>> }
>> }
>> if(ferror(fp))
>> outofmeomory:
>> *N = -1;
>> return 0;
>> *N = -2;
>> return 0;
>> }

>
> Excellent. Myself I might have used switch instead of goto or
> better yet maybe confused myself with a string of if ,else if, and
> else statements. In binary transfer would it be alright to use char*
> anyway? Transfer one byte at 512, 1024 groups.
>
> Bill

Thing is. that's using a doubling and not 10%.

Bill

Bill Cunningham
Guest
Posts: n/a

 08-30-2011
Ben Bacarisse wrote:
> "Bill Cunningham" <(E-Mail Removed)> writes:
>
>> Malcolm McLean wrote:
>>> /*
>>> read integers from a file
>>> assume that int is 32 bit, and that endianness is native. End of
>>> file = end of data
>>> fp - open file
>>> N - return for number of integers read
>>> Out of memory: returns 0, N = -1;
>>> IO error: returns 0, N = -2;
>>>
>>> Note: untested code. Might have bugs in it.
>>> */
>>> int *readintsfromfile(FILE *fp, int *N)
>>> {
>>> int *temp;
>>> int bufsize = 100;
>>> int x;
>>>
>>> answer = malloc(bufsize * 4);
>>> goto outofmemory;
>>> while(fread(&x, 4, 1, fp) == 1)
>>> {
>>> {
>>> buffsize = bufsize + bufsize/10;
>>> temp = realloc(answer, bufsize * 4);
>>> if(!temp)
>>> goto outofmemory;
>>> }
>>> }
>>> if(ferror(fp))
>>> outofmeomory:
>>> *N = -1;
>>> return 0;
>>> *N = -2;
>>> return 0;
>>> }

>>
>> Excellent. Myself I might have used switch instead of goto [...]

>
> How?
>
> <snip>

That would involve coding a different style and not leaving all the
labels for the end of the code and making things more confusing by having to
whack up code and end large chunks with break; constantly. As I said it
would probably look like a mess. Better left the way it is.

Bill

Malcolm McLean
Guest
Posts: n/a

 08-30-2011
On Aug 30, 3:43*am, "Bill Cunningham" <(E-Mail Removed)> wrote:
>
> * * Excellent. Myself I might have used switch instead of goto or better yet
> maybe confused myself with a string of if ,else if, and else statements. In
> binary transfer would it be alright to use char* anyway? Transfer one byte
> at 512, 1024 groups.
>

Yes. Normally it's easier to read binary data as unsigned chars using
fgetc(). However you can use fread.
--
Website
http://www.malcolmmclean.site11.com/www