Segfault in fread()
I'm trying to read a BMP image (greyscales) with C, save values into an array, and convert this array to a string with values separated with a comma.
My program worked well under Windows 7 64-bit, but I had to move to Windows XP 32-bit because of library compatibility problems.
I have 1,750 images to read, and I want to store all of them in a single string.
When I launch my program it goes fine until the 509:th image, then I get a Segmentation Fault caused by fread(). Here's my code:
int i=0,j,k,num,len,length,l;
unsigned char *Buffer;
FILE *fp;
char *string,*finalstring;
char *query;
char tmp2[5],tmp[3];
query = (char *)malloc(sizeof(char)*200000000);
string = (char *)malloc(sizeof(char)*101376);
Buffer = (unsigned char *)malloc(sizeof(unsigned char)*26368);
BITMAPFILEHEADER bMapFileHeader;
BITMAPINFOHEADER bMapInfoHeader;
length = 0;
for (k =1;k<1751;k++)
strcpy(link,"imagepath");
//here just indexing the images from 0000 to 1750
sprintf(tmp2,"%.4d",k);
strcat(link,tmp2);
strcat(link,".bmp");
fp = fopen(link, "rb");
num = fread(&bMapFileHeader,sizeof(BITMAPFILEHEADER),1,fp);
num = fread(&bMapInfoHeader,sizeof(BITMAPINFOHEADER),1,fp);
//seek beginning of data in bitmap
fseek(fp,54,SEEK_SET);
//read in bitmap file to data
fread(Buffer,26368,1,fp);
l=0;
for(i=1024;i<26368;i++)
itoa(Buffer[i],tmp,10);
len = strlen(tmp);
memcpy(string+l,tmp,len);
memcpy(string+l+len,",",1);
l = l+len+1;
memcpy(query,"",1);
memcpy(string,"",1);
printf("%in",k);
Thanks
c segmentation-fault fread
add a comment |
I'm trying to read a BMP image (greyscales) with C, save values into an array, and convert this array to a string with values separated with a comma.
My program worked well under Windows 7 64-bit, but I had to move to Windows XP 32-bit because of library compatibility problems.
I have 1,750 images to read, and I want to store all of them in a single string.
When I launch my program it goes fine until the 509:th image, then I get a Segmentation Fault caused by fread(). Here's my code:
int i=0,j,k,num,len,length,l;
unsigned char *Buffer;
FILE *fp;
char *string,*finalstring;
char *query;
char tmp2[5],tmp[3];
query = (char *)malloc(sizeof(char)*200000000);
string = (char *)malloc(sizeof(char)*101376);
Buffer = (unsigned char *)malloc(sizeof(unsigned char)*26368);
BITMAPFILEHEADER bMapFileHeader;
BITMAPINFOHEADER bMapInfoHeader;
length = 0;
for (k =1;k<1751;k++)
strcpy(link,"imagepath");
//here just indexing the images from 0000 to 1750
sprintf(tmp2,"%.4d",k);
strcat(link,tmp2);
strcat(link,".bmp");
fp = fopen(link, "rb");
num = fread(&bMapFileHeader,sizeof(BITMAPFILEHEADER),1,fp);
num = fread(&bMapInfoHeader,sizeof(BITMAPINFOHEADER),1,fp);
//seek beginning of data in bitmap
fseek(fp,54,SEEK_SET);
//read in bitmap file to data
fread(Buffer,26368,1,fp);
l=0;
for(i=1024;i<26368;i++)
itoa(Buffer[i],tmp,10);
len = strlen(tmp);
memcpy(string+l,tmp,len);
memcpy(string+l+len,",",1);
l = l+len+1;
memcpy(query,"",1);
memcpy(string,"",1);
printf("%in",k);
Thanks
c segmentation-fault fread
add a comment |
I'm trying to read a BMP image (greyscales) with C, save values into an array, and convert this array to a string with values separated with a comma.
My program worked well under Windows 7 64-bit, but I had to move to Windows XP 32-bit because of library compatibility problems.
I have 1,750 images to read, and I want to store all of them in a single string.
When I launch my program it goes fine until the 509:th image, then I get a Segmentation Fault caused by fread(). Here's my code:
int i=0,j,k,num,len,length,l;
unsigned char *Buffer;
FILE *fp;
char *string,*finalstring;
char *query;
char tmp2[5],tmp[3];
query = (char *)malloc(sizeof(char)*200000000);
string = (char *)malloc(sizeof(char)*101376);
Buffer = (unsigned char *)malloc(sizeof(unsigned char)*26368);
BITMAPFILEHEADER bMapFileHeader;
BITMAPINFOHEADER bMapInfoHeader;
length = 0;
for (k =1;k<1751;k++)
strcpy(link,"imagepath");
//here just indexing the images from 0000 to 1750
sprintf(tmp2,"%.4d",k);
strcat(link,tmp2);
strcat(link,".bmp");
fp = fopen(link, "rb");
num = fread(&bMapFileHeader,sizeof(BITMAPFILEHEADER),1,fp);
num = fread(&bMapInfoHeader,sizeof(BITMAPINFOHEADER),1,fp);
//seek beginning of data in bitmap
fseek(fp,54,SEEK_SET);
//read in bitmap file to data
fread(Buffer,26368,1,fp);
l=0;
for(i=1024;i<26368;i++)
itoa(Buffer[i],tmp,10);
len = strlen(tmp);
memcpy(string+l,tmp,len);
memcpy(string+l+len,",",1);
l = l+len+1;
memcpy(query,"",1);
memcpy(string,"",1);
printf("%in",k);
Thanks
c segmentation-fault fread
I'm trying to read a BMP image (greyscales) with C, save values into an array, and convert this array to a string with values separated with a comma.
My program worked well under Windows 7 64-bit, but I had to move to Windows XP 32-bit because of library compatibility problems.
I have 1,750 images to read, and I want to store all of them in a single string.
When I launch my program it goes fine until the 509:th image, then I get a Segmentation Fault caused by fread(). Here's my code:
int i=0,j,k,num,len,length,l;
unsigned char *Buffer;
FILE *fp;
char *string,*finalstring;
char *query;
char tmp2[5],tmp[3];
query = (char *)malloc(sizeof(char)*200000000);
string = (char *)malloc(sizeof(char)*101376);
Buffer = (unsigned char *)malloc(sizeof(unsigned char)*26368);
BITMAPFILEHEADER bMapFileHeader;
BITMAPINFOHEADER bMapInfoHeader;
length = 0;
for (k =1;k<1751;k++)
strcpy(link,"imagepath");
//here just indexing the images from 0000 to 1750
sprintf(tmp2,"%.4d",k);
strcat(link,tmp2);
strcat(link,".bmp");
fp = fopen(link, "rb");
num = fread(&bMapFileHeader,sizeof(BITMAPFILEHEADER),1,fp);
num = fread(&bMapInfoHeader,sizeof(BITMAPINFOHEADER),1,fp);
//seek beginning of data in bitmap
fseek(fp,54,SEEK_SET);
//read in bitmap file to data
fread(Buffer,26368,1,fp);
l=0;
for(i=1024;i<26368;i++)
itoa(Buffer[i],tmp,10);
len = strlen(tmp);
memcpy(string+l,tmp,len);
memcpy(string+l+len,",",1);
l = l+len+1;
memcpy(query,"",1);
memcpy(string,"",1);
printf("%in",k);
Thanks
c segmentation-fault fread
c segmentation-fault fread
edited Mar 24 '11 at 11:22
unwind
324k52398529
324k52398529
asked Mar 24 '11 at 10:39
MehdiMehdi
407618
407618
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
Make it tmp[4];
for three digits and the terminating 0.
Also: where is the fclose
? I suspect that you're running out of file handles.
Check, whether fp != 0
.
Thank you very much , it actually solved my problem , i didnt know that thing with running out of file handles , i thought that fp would be initialized every time i open an image.adding fclose(fp) at the end of the k Loop solved my problem :)
– Mehdi
Mar 24 '11 at 14:00
add a comment |
Where did you get 101376
from? Each of your bytes take up at most 5 characters as a decimal number with comma (e.g. -127,
), 5*26368
is 131840
.
add a comment |
Get rid of the casts in malloc
calls. And #include <stdlib.h>
.
What's the output of this program, in both the 64-bit and 32-bit systems you're using?
#include <stdio.h>
int main(void)
printf("sizeof (int) is %dn", (int)(sizeof (int)));
printf("sizeof (int*) is %dn", (int)(sizeof (int*)));
return 0;
add a comment |
Run your program in the debugger.
Set a breakpoint at the call to
fread -- make it conditional on
k==507 (this will stop it when you
expect the fread to be successful).When the program hits the
breakpoint, examine the variables
and check what is about to be passed
to fread. The first one or two times
you hit the breakpoint, the values
will be good.Then on the 509th time, you will
probably see bogus values being passed
to fread. Figure out where those
bogus values are coming from --
possibly set a conditional
breakpoint on the variable being set
to whatever the bogus value is.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f5417872%2fsegfault-in-fread%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
Make it tmp[4];
for three digits and the terminating 0.
Also: where is the fclose
? I suspect that you're running out of file handles.
Check, whether fp != 0
.
Thank you very much , it actually solved my problem , i didnt know that thing with running out of file handles , i thought that fp would be initialized every time i open an image.adding fclose(fp) at the end of the k Loop solved my problem :)
– Mehdi
Mar 24 '11 at 14:00
add a comment |
Make it tmp[4];
for three digits and the terminating 0.
Also: where is the fclose
? I suspect that you're running out of file handles.
Check, whether fp != 0
.
Thank you very much , it actually solved my problem , i didnt know that thing with running out of file handles , i thought that fp would be initialized every time i open an image.adding fclose(fp) at the end of the k Loop solved my problem :)
– Mehdi
Mar 24 '11 at 14:00
add a comment |
Make it tmp[4];
for three digits and the terminating 0.
Also: where is the fclose
? I suspect that you're running out of file handles.
Check, whether fp != 0
.
Make it tmp[4];
for three digits and the terminating 0.
Also: where is the fclose
? I suspect that you're running out of file handles.
Check, whether fp != 0
.
edited Nov 15 '18 at 6:07
Community♦
11
11
answered Mar 24 '11 at 10:49
HenrikHenrik
21.3k53485
21.3k53485
Thank you very much , it actually solved my problem , i didnt know that thing with running out of file handles , i thought that fp would be initialized every time i open an image.adding fclose(fp) at the end of the k Loop solved my problem :)
– Mehdi
Mar 24 '11 at 14:00
add a comment |
Thank you very much , it actually solved my problem , i didnt know that thing with running out of file handles , i thought that fp would be initialized every time i open an image.adding fclose(fp) at the end of the k Loop solved my problem :)
– Mehdi
Mar 24 '11 at 14:00
Thank you very much , it actually solved my problem , i didnt know that thing with running out of file handles , i thought that fp would be initialized every time i open an image.adding fclose(fp) at the end of the k Loop solved my problem :)
– Mehdi
Mar 24 '11 at 14:00
Thank you very much , it actually solved my problem , i didnt know that thing with running out of file handles , i thought that fp would be initialized every time i open an image.adding fclose(fp) at the end of the k Loop solved my problem :)
– Mehdi
Mar 24 '11 at 14:00
add a comment |
Where did you get 101376
from? Each of your bytes take up at most 5 characters as a decimal number with comma (e.g. -127,
), 5*26368
is 131840
.
add a comment |
Where did you get 101376
from? Each of your bytes take up at most 5 characters as a decimal number with comma (e.g. -127,
), 5*26368
is 131840
.
add a comment |
Where did you get 101376
from? Each of your bytes take up at most 5 characters as a decimal number with comma (e.g. -127,
), 5*26368
is 131840
.
Where did you get 101376
from? Each of your bytes take up at most 5 characters as a decimal number with comma (e.g. -127,
), 5*26368
is 131840
.
answered Mar 24 '11 at 10:44
ErikErik
68.2k9173174
68.2k9173174
add a comment |
add a comment |
Get rid of the casts in malloc
calls. And #include <stdlib.h>
.
What's the output of this program, in both the 64-bit and 32-bit systems you're using?
#include <stdio.h>
int main(void)
printf("sizeof (int) is %dn", (int)(sizeof (int)));
printf("sizeof (int*) is %dn", (int)(sizeof (int*)));
return 0;
add a comment |
Get rid of the casts in malloc
calls. And #include <stdlib.h>
.
What's the output of this program, in both the 64-bit and 32-bit systems you're using?
#include <stdio.h>
int main(void)
printf("sizeof (int) is %dn", (int)(sizeof (int)));
printf("sizeof (int*) is %dn", (int)(sizeof (int*)));
return 0;
add a comment |
Get rid of the casts in malloc
calls. And #include <stdlib.h>
.
What's the output of this program, in both the 64-bit and 32-bit systems you're using?
#include <stdio.h>
int main(void)
printf("sizeof (int) is %dn", (int)(sizeof (int)));
printf("sizeof (int*) is %dn", (int)(sizeof (int*)));
return 0;
Get rid of the casts in malloc
calls. And #include <stdlib.h>
.
What's the output of this program, in both the 64-bit and 32-bit systems you're using?
#include <stdio.h>
int main(void)
printf("sizeof (int) is %dn", (int)(sizeof (int)));
printf("sizeof (int*) is %dn", (int)(sizeof (int*)));
return 0;
answered Mar 24 '11 at 11:11
pmgpmg
84.7k999170
84.7k999170
add a comment |
add a comment |
Run your program in the debugger.
Set a breakpoint at the call to
fread -- make it conditional on
k==507 (this will stop it when you
expect the fread to be successful).When the program hits the
breakpoint, examine the variables
and check what is about to be passed
to fread. The first one or two times
you hit the breakpoint, the values
will be good.Then on the 509th time, you will
probably see bogus values being passed
to fread. Figure out where those
bogus values are coming from --
possibly set a conditional
breakpoint on the variable being set
to whatever the bogus value is.
add a comment |
Run your program in the debugger.
Set a breakpoint at the call to
fread -- make it conditional on
k==507 (this will stop it when you
expect the fread to be successful).When the program hits the
breakpoint, examine the variables
and check what is about to be passed
to fread. The first one or two times
you hit the breakpoint, the values
will be good.Then on the 509th time, you will
probably see bogus values being passed
to fread. Figure out where those
bogus values are coming from --
possibly set a conditional
breakpoint on the variable being set
to whatever the bogus value is.
add a comment |
Run your program in the debugger.
Set a breakpoint at the call to
fread -- make it conditional on
k==507 (this will stop it when you
expect the fread to be successful).When the program hits the
breakpoint, examine the variables
and check what is about to be passed
to fread. The first one or two times
you hit the breakpoint, the values
will be good.Then on the 509th time, you will
probably see bogus values being passed
to fread. Figure out where those
bogus values are coming from --
possibly set a conditional
breakpoint on the variable being set
to whatever the bogus value is.
Run your program in the debugger.
Set a breakpoint at the call to
fread -- make it conditional on
k==507 (this will stop it when you
expect the fread to be successful).When the program hits the
breakpoint, examine the variables
and check what is about to be passed
to fread. The first one or two times
you hit the breakpoint, the values
will be good.Then on the 509th time, you will
probably see bogus values being passed
to fread. Figure out where those
bogus values are coming from --
possibly set a conditional
breakpoint on the variable being set
to whatever the bogus value is.
answered Mar 24 '11 at 12:56
bstpierrebstpierre
20.5k145598
20.5k145598
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f5417872%2fsegfault-in-fread%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown