Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C > Re: Issue with ...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 6 of 6 Topic 26093 of 26821
Post > Topic >>

Re: Issue with memory allocation and file reading

by Peter Nilsson <airia@[EMAIL PROTECTED] > May 6, 2008 at 08:02 PM

Jens Thoms Toerring wrote:
> paragon.john@[EMAIL PROTECTED]
 wrote:
> > #include <stdio.h>
> > #include <fcntl.h>
>
> <fcntl.h> doesn't seem to be needed at all.

It's not a standard C header either.

> > int main()
> > {
> >      int data_size = 1024*1024;
>
> Why not make that a 'size_t' so that you can be sure that
> the value actually fits in?

There is no guarantee that 1048576 will fit into a size_t.

> An int doesn't need to be able to hold more than 16 bits,
> which wouldn't be enough.

Since 1024 is just as int, then 1024*1024 can overflow,
irrespective of what you assign it to.

> >      data_buf = (unsigned char *) valloc(data_size);
>
> Rather likely the problem is here. First of all, valloc()
> isn't a standard C function - and even the Linux man page
> describes it as "obsolete", so why don't you use malloc()?
>
> But the real problem is rather likely that there's no
> declaration for the valloc() function in scope since you
> didn't include <stdlib.h>.

Note that a mere delcaration is not sufficient. You may
need a prototype, especially if you're passing an int
argument for a size_t parameter.

<snip>
> So never cast the return value of malloc() (and other
> functions) unless you have a very good reason.

Prevention is better than cure. Requiring prototypes to
be in scope is a better suggestion IMO. Sadly, the option
actually breaks implementation conformance, although
I find the cost to be well worth it.

<snip>

--
Peter
 




 6 Posts in Topic:
Issue with memory allocation and file reading
paragon.john@[EMAIL PROTE  2008-05-06 12:09:56 
Re: Issue with memory allocation and file reading
jt@[EMAIL PROTECTED] (Je  2008-05-06 19:33:46 
Re: Issue with memory allocation and file reading
viza <tom.viza@[EMAIL   2008-05-06 13:24:35 
Re: Issue with memory allocation and file reading
paragon.john@[EMAIL PROTE  2008-05-06 13:35:15 
Re: Issue with memory allocation and file reading
CBFalconer <cbfalconer  2008-05-06 20:17:41 
Re: Issue with memory allocation and file reading
Peter Nilsson <airia@[  2008-05-06 20:02:06 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Wed Jul 9 0:42:36 CDT 2008.