LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-06-2014, 11:51 AM   #1
ron7000
Member
 
Registered: Nov 2007
Location: CT
Posts: 248

Rep: Reputation: 26
fread fwrite problem in C program, segmentation fault


i wrote a C (not c++) code to convert a binary image file from one format to another. i am getting a segmentation fault, and it's caused by an fwrite that i believe is pushing my pointer outside of the memory bounds that i malloc'd.

am i using the fread and fwrite properly? I assumed because i typedef 'd stuff the sizing and pointer arithmetic should be correct but i get the feeling i overlooked or am not understanding something. here is the relevant portions of code.

Code:
typedef unsigned short int         type_Image;
type_Image *image;

num_subframes = 4;

/* y_size actually 512, there are 4 subframes of data concatenated */
image_size = 640 * 2048 * 2;  /* x_size * y_size * bytes_per_pixel */

image = ( type_Image * ) malloc( image_size );

new_size = 640*512*2;   /* x_size * y_size * bytes_per_pixel */

/*
   image_size = 2,621,440
   new_size = 655,360
*/

for ( f = 0; f < num_frames; f++ )
{
   fread( image, image_size, 1, fp );

   for ( s = 0; s < num_subframes; s++ )
   {
      /* split subframes into separate files */
      fwrite( image + (s * new_size), new_size, 1, fp[s] );
   }
}
problem is... num_subframes is 4 so s goes from 0..3.
When s == 3, the fwrite results in segmentation fault.
This is on the first time, when f == 0.
Why?

Last edited by ron7000; 10-06-2014 at 11:53 AM.
 
Old 10-06-2014, 12:06 PM   #2
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 982
Blog Entries: 2

Rep: Reputation: 244Reputation: 244Reputation: 244
Is fp[] actually an array of the right size [0..3] ?
 
Old 10-06-2014, 12:19 PM   #3
ron7000
Member
 
Registered: Nov 2007
Location: CT
Posts: 248

Original Poster
Rep: Reputation: 26
yeah the file pointer fp[] array is sized properly.

i figured it out. i misread my documents described how the image data is stored.
it's 2 bytes per pixel. somewhere i read the pixel data value is of type unsigned short integer, which is coincidentally 2 bytes.
I changed type_Image to "typedef char type_Image" and now things work.
I think since the original typedef of short int for type_Image is technically 2 bytes, then i say my image size is X * Y * 2, I was double bookkeeping. and i definitely would be reading in too much data eventually.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Segmentation fault in C program why_so_serious Linux - Newbie 6 10-25-2013 01:22 AM
fread and fwrite errors with mapped memory justaguy_75ae Programming 2 09-08-2009 01:44 PM
best way of using fread and fwrite rohanak Programming 1 05-02-2008 07:26 AM
Simple C++ Program: Program Compiles But Won't Run (Segmentation Fault) violagirl23 Programming 3 01-09-2008 12:09 AM
C programming fread and fwrite using structures exvor Programming 4 09-26-2006 08:56 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:46 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration