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 5 of 6 Topic 26093 of 26955
Post > Topic >>

Re: Issue with memory allocation and file reading

by CBFalconer <cbfalconer@[EMAIL PROTECTED] > May 6, 2008 at 08:17 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>

There is no such header file in standard C.
> 
> int main()

Better would be "int main(void)

> {
>      int data_size = 1024*1024;

There is no guarantee that an int can hold this number.  Use a
long, or possibly a size_t.

>      u_char *data_buf;

There is no such type as u_char.

>      FILE *data_file;
>      int data_num;
> 
>      data_buf = (unsigned char *) valloc(data_size);

There is no such function as valloc.  If you meant malloc, don't
cast the result, which hides the error of failing to "#include
<stdlib.h>".  The size parameter should be a size_t type.
>      printf("Memory allocated.\n");

How do you know?

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

How do you know?

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

How do you know?

>      return 0;
> }

-- 
 [mail]: Chuck F (cbfalconer at maineline dot net) 
 [page]: <http://cbfalconer.home.att.net>
            Try the download section.


** Posted from http://www.teranews.com
**
 




 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 Thu Jul 24 2:15:39 CDT 2008.