Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > function

Reply
Thread Tools

function

 
 
Bill Cunningham
Guest
Posts: n/a
 
      08-11-2011
I have been looking for a function from ANSI C that would start out with
a buffer size of zero initialized and would grow to a certain size. I have
already been able to set aside a 32 byte type using malloc(). Could calloc()
be what I'm looking for? This buffer would be able to grow by a mechanism
that would include using fscanf to find the size of a file in bytes. So my
question is simply is there any functions or ways in c89 to fill a buffer to
a certain size before it's clear? int buffer[] won't work unless it's a
char.

Bill


 
Reply With Quote
 
 
 
 
lolzy@live.nl
Guest
Posts: n/a
 
      08-11-2011
On 11 aug, 11:04, "Bill Cunningham" <(E-Mail Removed)> wrote:
> * * I have been looking for a function from ANSI C that would start out with
> a buffer size of zero initialized and would grow to a certain size. I have
> already been able to set aside a 32 byte type using malloc(). Could calloc()
> be what I'm looking for? This buffer would be able to grow by a mechanism
> that would include using fscanf to find the size of a file in bytes. So my
> question is simply is there any functions or ways in c89 to fill a bufferto
> a certain size before it's clear? int buffer[] won't work unless it's a
> char.
>
> Bill


I'm sorry, I can't understand you... could you explain exactly what
you want to achieve?
 
Reply With Quote
 
 
 
 
Ben Bacarisse
Guest
Posts: n/a
 
      08-11-2011
"Bill Cunningham" <(E-Mail Removed)> writes:

> I have been looking for a function from ANSI C that would start out with
> a buffer size of zero initialized and would grow to a certain size. I have
> already been able to set aside a 32 byte type using malloc(). Could calloc()
> be what I'm looking for?


No.

> This buffer would be able to grow by a mechanism
> that would include using fscanf to find the size of a file in bytes. So my
> question is simply is there any functions or ways in c89 to fill a buffer to
> a certain size before it's clear?


Yes. There are "ways" in C89 to write almost any program -- it's a
general purpose language after all.

No standard function does what you want. Many programming languages can
do what you want in a single function call (or equivalent) but you seem
determined to butt your head up against, what is to you, C's impenetrable
syntax and library.

> int buffer[] won't work unless it's a char.


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

--
Ben.
 
Reply With Quote
 
Mark Bluemel
Guest
Posts: n/a
 
      08-11-2011
On 08/11/2011 10:53 AM, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> On 11 aug, 11:04, "Bill Cunningham"<(E-Mail Removed)> wrote:


[It really doesn't matter what he wrote]

> I'm sorry, I can't understand you... could you explain exactly what
> you want to achieve?


It seems unlikely. Bill's been around here spouting impenetrable
nonsense for quite some time, there's no indication that that will
change any time soon.

Some say that he is a troll, others that he has cognitive issues. (All I
know is he's not the Stig!)

Over the time I've been reading this newsgroup, there has been no sign
that Bill learns from any information given to him. Because of this,
many people have given up bothering with his posts.

I only see him in people's replies, as I have him kill-filed.
 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      08-11-2011
Gordon Burditt wrote:
>> I have been looking for a function from ANSI C that would start
>> out with a buffer size of zero initialized and would grow to a
>> certain size. I have

>
> A buffer is not of size zero. C doesn't have zero-sized objects.
> Even if it did, you can't initialize a zero-sized buffer without
> overflowing it.
>
>> already been able to set aside a 32 byte type using malloc(). Could
>> calloc()

>
> You can use realloc() to change the size of a malloc()ed buffer
> (upwards or downwards). You can use memset() to clear, for example,
> the newly-allocated part of the buffer (assuming the size grew).
> Or, your code can fill in the appropriate values.


Ok. That may be what I'm looking for.

> Beware that memset() is not appropriate to initialize struct elements
> that are pointers or floating-point types (if that's what you are
> going to put in the buffer) where all-bits-zero doesn't guarantee
> a specific value. A NULL pointer is not guaranteed to be represented
> as the bit pattern 0xdeadbeef even on machines where pointers are
> 32-bits.
>
>> be what I'm looking for? This buffer would be able to grow by a
>> mechanism that would include using fscanf to find the size of a file
>> in bytes. So my

>
> By the time you have found out the size of a file in bytes, the size
> of that file might have already changed. If you take one pass through
> the file to calculate its size, allocate that much memory, and then
> read the file again, you might end up overflowing the file or losing
> the last part of it if it grows between passes.


Oh ok I see I didn't know that.

> You can allocate an initial buffer, and keep growing the buffer
> (usually *NOT* by one byte at a time; this is horribly inefficient)
> with realloc() as needed until you've read the whole file. Some
> people like doubling the size of the buffer each time. Then,
> perhaps, you can shrink the buffer down to the actual size used.
> The file still might grow while or after you finish reading it.
>
>> question is simply is there any functions or ways in c89 to fill a
>> buffer to a certain size before it's clear?

>
> Filling a buffer does not change its size (unless you specifically
> code a call to realloc() in this situation). Changing the size of
> a buffer with realloc() does not "clear" it - the data in the minimum
> size of the buffer before and after the realloc() is preserved.
>
> This question does not make any sense. Please define the word
> "clear" as you use it here.


I meant clear"ed". A typo. As in void free(void *)

I don't think that "filled with zero
> bytes" nor "transparent; lacking color and colour" makes sense here.
> Neither is the question "clear".
>
>> int buffer[] won't work unless it's a
>> char.

>
> int buffer[] as a definition won't work even if <it> is a char,
> regardless of what <it> refers to. You have to specify a (nonzero)
> size.



 
Reply With Quote
 
Ralph Spitzner
Guest
Posts: n/a
 
      08-11-2011
Mark Bluemel wrote:
> On 08/11/2011 10:53 AM, (E-Mail Removed) wrote:

[whatever]
>
>
> I only see him in people's replies, as I have him kill-filed.


He's looking for a 'self growing linked list storage',
which unfortunately is (to my knowledge) not a part
of _any_ programming language that I know of

-rasp
 
Reply With Quote
 
John Gordon
Guest
Posts: n/a
 
      08-11-2011
In <4e439af9$0$7278$(E-Mail Removed)> "Bill Cunningham" <(E-Mail Removed)> writes:

> I have been looking for a function from ANSI C that would start out with
> a buffer size of zero initialized and would grow to a certain size. I have


If you want a buffer that grows, malloc and realloc sound like a perfect
fit.

--
John Gordon A is for Amy, who fell down the stairs
(E-Mail Removed) B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      08-11-2011
Ralph Spitzner <(E-Mail Removed)> writes:

> Mark Bluemel wrote:
>> On 08/11/2011 10:53 AM, (E-Mail Removed) wrote:

> [whatever]
>>
>>
>> I only see him in people's replies, as I have him kill-filed.

>
> He's looking for a 'self growing linked list storage',
> which unfortunately is (to my knowledge) not a part
> of _any_ programming language that I know of


Haskel does. Bill wants to read a file, and Haskell's getContents is
just that. Because it's value is a lazy list, it will grow as needed
and, for that matter, it will also shrink as needed.

--
Ben.
 
Reply With Quote
 
Malcolm McLean
Guest
Posts: n/a
 
      08-11-2011
On Aug 11, 12:04*pm, "Bill Cunningham" <(E-Mail Removed)> wrote:
> * * I have been looking for a function from ANSI C that would start out with
> a buffer size of zero initialized and would grow to a certain size. I have
> already been able to set aside a 32 byte type using malloc(). Could calloc()
> be what I'm looking for? This buffer would be able to grow by a mechanism
> that would include using fscanf to find the size of a file in bytes. So my
> question is simply is there any functions or ways in c89 to fill a bufferto
> a certain size before it's clear? int buffer[] won't work unless it's a
> char.
>

You need realloc().

Start with a null pointer and a buffer of size zero. Then grow by a
bit each time. I usually use the formula new_size = (size_t) (oldsize
* 1.1 + 100). This means that the buffer grows by 10% each time, plus
a bit, so you don't have too many reallocations when it is small, and
you don't take up too much memory when it is large.

realloc() won't initialise data for you. You'll have to do that by
hand. There ought to be a recalloc() which zero-initialises data, but
there isn't.


 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      08-11-2011
Malcolm McLean wrote:
> On Aug 11, 12:04 pm, "Bill Cunningham" <(E-Mail Removed)> wrote:
>> I have been looking for a function from ANSI C that would start out
>> with
>> a buffer size of zero initialized and would grow to a certain size.
>> I have already been able to set aside a 32 byte type using malloc().
>> Could calloc() be what I'm looking for? This buffer would be able to
>> grow by a mechanism that would include using fscanf to find the size
>> of a file in bytes. So my question is simply is there any functions
>> or ways in c89 to fill a buffer to a certain size before it's clear?
>> int buffer[] won't work unless it's a char.
>>

> You need realloc().
>
> Start with a null pointer and a buffer of size zero. Then grow by a
> bit each time. I usually use the formula new_size = (size_t) (oldsize
> * 1.1 + 100). This means that the buffer grows by 10% each time, plus
> a bit, so you don't have too many reallocations when it is small, and
> you don't take up too much memory when it is large.
>
> realloc() won't initialise data for you. You'll have to do that by
> hand. There ought to be a recalloc() which zero-initialises data, but
> there isn't.


Just what I need. Thanks very much.

Bill


 
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
Function versus pointer to function, in context of std::function,huh? Alf P. Steinbach C++ 10 07-27-2011 05:51 AM
Function pointer to void function and int function Giannis Papadopoulos C Programming 5 09-05-2005 09:06 PM
How override ALL function calls? (Is there a "function call function"?) seberino@spawar.navy.mil Python 2 08-01-2005 12:38 PM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM
Passing a C++ object's member function to a C function expecing a function pointer! James Vanns C++ 7 01-21-2004 02:39 AM



Advertisments