Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | John Gordon <gordon@panix.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: malloc |
| Date | 2011-05-02 15:31 +0000 |
| Organization | PANIX Public Access Internet and UNIX, NYC |
| Message-ID | <ipmior$po4$1@reader1.panix.com> (permalink) |
| References | <4dbce936$0$8355$bbae4d71@news.suddenlink.net> <4dbcfa24$0$8384$bbae4d71@news.suddenlink.net> |
In <4dbcfa24$0$8384$bbae4d71@news.suddenlink.net> "Bill Cunningham" <nospam@nspam.invalid> writes:
> Let me see if I can make this easier to understand.
> fread(p,sizeof(int),20,fp);
> Ok nothing will go above 20 size_t's but if only 15 size_t's are filled I
> want to return 5 to memory. Is there a way to do that?
Allocate a suitably large buffer with malloc().
Call fread() to put stuff in the buffer.
If the buffer ends up being too large, resize it with realloc().
I noticed a problem with your original code:
> char p[20];
> t = fread(p, sizeof(int), 20, fp);
p is declared as an array of 20 characters, however you're trying to stuff
20 ints into it, which is too much. If you know you will put ints into p,
declare it that way.
--
John Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
malloc "Bill Cunningham" <nospam@nspam.invalid> - 2011-05-01 01:01 -0400
Re: malloc "Bill Cunningham" <nospam@nspam.invalid> - 2011-05-01 01:03 -0400
Re: malloc luser- -droog <mijoryx@yahoo.com> - 2011-04-30 22:31 -0700
Re: malloc "Bill Cunningham" <nospam@nspam.invalid> - 2011-05-01 02:15 -0400
Re: malloc "BartC" <bc@freeuk.com> - 2011-05-01 13:52 +0100
Re: malloc luser- -droog <mijoryx@yahoo.com> - 2011-05-02 00:05 -0700
Re: malloc crisgoogle <crisd@telus.net> - 2011-05-02 17:43 -0700
Re: malloc Ian Collins <ian-news@hotmail.com> - 2011-05-01 17:35 +1200
Re: malloc "Bill Cunningham" <nospam@nspam.invalid> - 2011-05-01 02:14 -0400
Re: malloc Mickey Mouse <fake@email.com> - 2011-05-01 00:18 -0700
Re: malloc "Bill Cunningham" <nospam@nspam.invalid> - 2011-05-01 04:07 -0400
Re: malloc Barry Schwarz <schwarzb@dqel.com> - 2011-05-01 10:31 -0700
Re: malloc "Bill Cunningham" <nospam@nspam.invalid> - 2011-05-02 16:55 -0400
Re: malloc "Bill Cunningham" <nospam@nspam.invalid> - 2011-05-02 23:51 -0400
Re: malloc "Bill Cunningham" <nospam@nspam.invalid> - 2011-05-02 16:58 -0400
Re: malloc Mickey Mouse <fake@email.com> - 2011-05-02 14:34 -0700
Re: malloc Ian Collins <ian-news@hotmail.com> - 2011-05-03 09:55 +1200
Re: malloc "Bill Cunningham" <nospam@nspam.invalid> - 2011-05-03 10:20 -0400
Re: malloc Angel <angel+news@spamcop.net> - 2011-05-03 14:55 +0000
Re: malloc "Bill Cunningham" <nospam@nspam.invalid> - 2011-05-03 15:34 -0400
Re: malloc Angel <angel+news@spamcop.net> - 2011-05-03 20:56 +0000
Re: malloc "Bill Cunningham" <nospam@nspam.invalid> - 2011-05-03 11:29 -0400
Re: malloc Seebs <usenet-nospam@seebs.net> - 2011-05-02 22:18 +0000
Re: malloc "Default User" <defaultuserbr@yahoo.com> - 2011-05-04 12:20 -0500
Re: malloc Keith Thompson <kst-u@mib.org> - 2011-05-04 10:52 -0700
Re: malloc Joe Wright <joewwright@comcast.net> - 2011-05-04 14:57 -0400
Re: malloc "Bill Cunningham" <nospam@nspam.invalid> - 2011-05-04 18:53 -0400
Re: malloc Angel <angel+news@spamcop.net> - 2011-05-05 08:12 +0000
Re: malloc John Gordon <gordon@panix.com> - 2011-05-02 15:31 +0000
Re: malloc "osmium" <r124c4u102@comcast.net> - 2011-05-01 09:56 -0500
Re: malloc "Bill Cunningham" <nospam@nspam.invalid> - 2011-05-01 18:20 -0400
Re: malloc "Bill Cunningham" <nospam@nspam.invalid> - 2011-05-02 21:38 -0400
Re: malloc "osmium" <r124c4u102@comcast.net> - 2011-05-03 09:02 -0500
Re: malloc "Bill Cunningham" <nospam@nspam.invalid> - 2011-05-03 15:21 -0400
Re: malloc "osmium" <r124c4u102@comcast.net> - 2011-05-03 14:35 -0500
Re: malloc "Bill Cunningham" <nospam@nspam.invalid> - 2011-05-03 18:43 -0400
Re: malloc Tim Rentsch <txr@alumni.caltech.edu> - 2011-05-01 12:51 -0700
Re: malloc "Bill Cunningham" <nospam@nspam.invalid> - 2011-05-02 23:49 -0400
Re: malloc Ian Collins <ian-news@hotmail.com> - 2011-05-03 13:35 +1200
Re: malloc "Bill Cunningham" <nospam@nspam.invalid> - 2011-05-03 11:27 -0400
Re: malloc Ian Collins <ian-news@hotmail.com> - 2011-05-04 07:54 +1200
Re: malloc Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-05-03 21:50 +0100
csiph-web