Talk About Network



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 > Issue with memo...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 6 Topic 26093 of 26198
Post > Topic >>

Issue with memory allocation and file reading

by paragon.john@[EMAIL PROTECTED] May 6, 2008 at 12:09 PM

Hello all,

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>

int main()
{
     int data_size = 1024*1024;
     u_char *data_buf;
     FILE *data_file;
     int data_num;

     data_buf = (unsigned char *) valloc(data_size);
     printf("Memory allocated.\n");
     data_file = fopen("data_file","rb");
     printf("File opened.");
     data_num = fread(data_buf, sizeof(u_char), data_size, data_file);
     printf("File read.");
     return 0;
}

Thanks for your help!




 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 May 14 6:51:16 CDT 2008.