Segfault in fread()










1















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










share|improve this question




























    1















    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










    share|improve this question


























      1












      1








      1








      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










      share|improve this question
















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 24 '11 at 11:22









      unwind

      324k52398529




      324k52398529










      asked Mar 24 '11 at 10:39









      MehdiMehdi

      407618




      407618






















          4 Answers
          4






          active

          oldest

          votes


















          1














          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.






          share|improve this answer

























          • 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



















          1














          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.






          share|improve this answer






























            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;






            share|improve this answer






























              0














              1. Run your program in the debugger.


              2. 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).


              3. 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.


              4. 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.






              share|improve this answer























                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
                );



                );













                draft saved

                draft discarded


















                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









                1














                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.






                share|improve this answer

























                • 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
















                1














                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.






                share|improve this answer

























                • 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














                1












                1








                1







                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.






                share|improve this answer















                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.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                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


















                • 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














                1














                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.






                share|improve this answer



























                  1














                  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.






                  share|improve this answer

























                    1












                    1








                    1







                    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.






                    share|improve this answer













                    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.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Mar 24 '11 at 10:44









                    ErikErik

                    68.2k9173174




                    68.2k9173174





















                        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;






                        share|improve this answer



























                          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;






                          share|improve this answer

























                            0












                            0








                            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;






                            share|improve this answer













                            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;







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Mar 24 '11 at 11:11









                            pmgpmg

                            84.7k999170




                            84.7k999170





















                                0














                                1. Run your program in the debugger.


                                2. 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).


                                3. 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.


                                4. 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.






                                share|improve this answer



























                                  0














                                  1. Run your program in the debugger.


                                  2. 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).


                                  3. 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.


                                  4. 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.






                                  share|improve this answer

























                                    0












                                    0








                                    0







                                    1. Run your program in the debugger.


                                    2. 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).


                                    3. 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.


                                    4. 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.






                                    share|improve this answer













                                    1. Run your program in the debugger.


                                    2. 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).


                                    3. 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.


                                    4. 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.







                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Mar 24 '11 at 12:56









                                    bstpierrebstpierre

                                    20.5k145598




                                    20.5k145598



























                                        draft saved

                                        draft discarded
















































                                        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.




                                        draft saved


                                        draft discarded














                                        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





















































                                        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







                                        Popular posts from this blog

                                        How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

                                        Syphilis

                                        Darth Vader #20