LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 07-31-2023, 08:06 AM   #1
mravenca
LQ Newbie
 
Registered: Dec 2009
Posts: 11

Rep: Reputation: 0
Post fwrite - segmentation fault


Hello, I am working on a testing task.

In the task I am using this code that I have written:

Code:
void HDD_Write(UINT16 lintSector,UINT32 lintHddOffset, UINT8 * lintData, UINT32 lintDataLen)
{
	printf("HDD_Write(): lintSector=%u lintHddOffset=%u lintDataLen=%u\n",lintSector,lintHddOffset,lintDataLen);
	//HDD_Write(AllocatedSector, HDD_SectorSize-sizeof(psel->NextElement), psel->NextElement, sizeof(psel->NextElement));
	FILE *ptr;
	ptr = fopen("HDD.bin","r+b");
	if(ptr!=NULL)
	{
		printf("HDD_Write(): Debug1\n");
		fflush(stdout);
		printf("fseek ..");
		fflush(stdout);
		fseek(ptr, lintSector*HDD_SectorSize+lintHddOffset, SEEK_SET );
		printf("ok fwrite..");
		fflush(stdout);
		//--- segfault when called for second time
		size_t res = fwrite(lintData,sizeof(lintData[0]),lintDataLen,ptr);
		//fwrite(lintData,1,lintDataLen,ptr);
		//----------------------
	
		printf("ok\n");
		fflush(stdout);
	}
	else printf("HDD_Write(): ptr is NULL\n");
	fclose(ptr);
	
	printf("HDD_Write() complete. \n\n");
	
}
The function is then called two times.
First for a block of 1022 bytes, which is performed successfully, then for a block of 2 bytes, which ends with segmentation fault.

I am trying to find the error cause for a few days now.. Can you give me a hint?
 
Old 07-31-2023, 08:40 AM   #2
Projectile
Member
 
Registered: Jun 2023
Posts: 72
Blog Entries: 1

Rep: Reputation: 13
Maybe lintData becomes NULL before the function call?

In this case you should use a debugger such as gdb or whatever is available to you if you use an IDE.
I'd advise further but I myself haven't learned how to use gdb yet.

https://gcc.gnu.org/bugs/segfault.html
 
  


Reply

Tags
segmentation fault



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
fread fwrite problem in C program, segmentation fault ron7000 Programming 2 10-06-2014 12:19 PM
fwrite in another fwrite? ati Programming 2 05-11-2006 02:25 AM
what could cause this fwrite() to seg fault? hedpe Programming 11 02-12-2006 10:02 PM
fwrite() doe not move pointer, why? realos Programming 4 12-08-2005 12:48 PM
yast segmentation fault, system freezing - nvidia driver at fault? BaltikaTroika SUSE / openSUSE 2 12-02-2005 09:34 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 06:27 AM.

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