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 2 of 6 Topic 26093 of 26972
Post > Topic >>

Re: Issue with memory allocation and file reading

by jt@[EMAIL PROTECTED] (Jens Thoms Toerring) May 6, 2008 at 07:33 PM

paragon.john@[EMAIL PROTECTED]
 wrote:
> I am trying to read a file into some allocated memory as part of a
> program and I am running into a problem.  The program gets a
> segmentation fault during fread.  I have previously used this code on
> 64-bit RHEL4 without any problems but I am not having this issue on 32-
> bit RHEL5.  I have simplified the code to it's most basic form and I
> am still seeing the issue.  Below is a full program that causes the
> error.  If anybody knows what may be causing the problem, help would
> be greatly appreciated....

> #include <stdio.h>
> #include <fcntl.h>

<fcntl.h> doesn't seem to be needed at all.

> 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? An int doesn't need to be
able to hold more than 16 bits, which wouldn't be enough.

>      u_char *data_buf;

What's wrong with plain old 'unsigned char'?

>      FILE *data_file;
>      int data_num;

Also this value should be a 'size_t', that's the type
that fread() returns.

>      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>. By using the cast in front of
valloc() you silenced the compiler but you didn't solve
the underlying problem: since there's no declaration the
compiler assumes that valloc() returns an int an will treat
the return value accordingly - it may store it somewhere
where an int but not a pointer fits (or, if you're on a
machine with dedicated data and address registers, the
return value gets passed back via a address register but
the caller, expecting an int, i.e. data, tries to pull it
from a data register). And this crippled or just random
value is then converted back into a pointer which doesn't
point to the memory that was allocated...

So never cast the return value of malloc() (and other
functions) unless you have a very good reason. While you
can silence the compiler that way you just keep it from
giving you valuable hints.

>      printf("Memory allocated.\n");

You can't be sure since you didn't check the return value
of valloc().

>      data_file = fopen("data_file","rb");
>      printf("File opened.");

That's also something you can't be sure about since you
also don't test the return value of fopen().

>      data_num = fread(data_buf, sizeof(u_char), data_size, data_file);
>      printf("File read.");

And again it would make sense to check the return value of
fread().

>      return 0;
> }
                            Regards, Jens
-- 
  \   Jens Thoms Toerring  ___      jt@[EMAIL PROTECTED]
   \__________________________      http://toerring.de
 




 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 Fri Jul 25 16:14:13 CDT 2008.