Valgrind: Invaid read/write of size 1 error in fscanf and struct double pointer












0














I got this error from Valgrind. I could bot find the solution to fix this.
When I try to free my allocations I get segmentation fault and these errors.



> ==10096== ERROR SUMMARY: 3 errors from 3 contexts (suppressed: 0 from 0)
==10096==
==10096== 1 errors in context 1 of 3:
==10096== Invalid write of size 1
==10096== at 0x40138C: readClientFile (client.c:39)
==10096== by 0x400C0B: main (main.c:15)
==10096== Address 0x3 is not stack'd, malloc'd or (recently) free'd
==10096==
==10096==
==10096== 1 errors in context 2 of 3:
==10096== Invalid read of size 8
==10096== at 0x401389: readClientFile (client.c:39)
==10096== by 0x400C0B: main (main.c:15)
==10096== Address 0x5203568 is 0 bytes after a block of size 40 alloc'd
==10096== at 0x4C2B9B5: calloc (vg_replace_malloc.c:711)
==10096== by 0x401304: readClientFile (client.c:29)
==10096== by 0x400C0B: main (main.c:15)
==10096==
==10096==
==10096== 1 errors in context 3 of 3:
==10096== Invalid read of size 8
==10096== at 0x401359: readClientFile (client.c:38)
==10096== by 0x400C0B: main (main.c:15)
==10096== Address 0x5203568 is 0 bytes after a block of size 40 alloc'd
==10096== at 0x4C2B9B5: calloc (vg_replace_malloc.c:711)
==10096== by 0x401304: readClientFile (client.c:29)
==10096== by 0x400C0B: main (main.c:15)


It says that problem is about my double pointer array of structures. Related lines are below.



29.    struct client** clientsArray =calloc(numberOfClients ,sizeof(struct client*));

for(i= 0 ; i<numberOfClients; i++){
clientsArray[i] = (struct client*)malloc(sizeof(struct client));

}
while(!feof(clientFP)){

38. fscanf(clientFP, "%s", clientsArray[counter]->clientID);
39. clientsArray[counter]->clientID[3]='';

fscanf(clientFP, "%s", clientsArray[counter]->clientIP);
clientsArray[counter]->clientIP[11]='';

fscanf(clientFP, "%s", clientsArray[counter]->clientMacAddress);
clientsArray[counter]->clientMacAddress[11]='';

counter++;

}


//Each line of the file is in this format: A 1.2.3.4 AAAAAAAAAA



MY CODE TO FREE CLIENTS ARRAY;



Firstly I freed the variables of each client object. And then I used this.



for(i = 0 ; i<numberOfClients ; i++){


free(clients[i]);
}
free(clients);


Can you please help me about what to change?









share|improve this question



























    0














    I got this error from Valgrind. I could bot find the solution to fix this.
    When I try to free my allocations I get segmentation fault and these errors.



    > ==10096== ERROR SUMMARY: 3 errors from 3 contexts (suppressed: 0 from 0)
    ==10096==
    ==10096== 1 errors in context 1 of 3:
    ==10096== Invalid write of size 1
    ==10096== at 0x40138C: readClientFile (client.c:39)
    ==10096== by 0x400C0B: main (main.c:15)
    ==10096== Address 0x3 is not stack'd, malloc'd or (recently) free'd
    ==10096==
    ==10096==
    ==10096== 1 errors in context 2 of 3:
    ==10096== Invalid read of size 8
    ==10096== at 0x401389: readClientFile (client.c:39)
    ==10096== by 0x400C0B: main (main.c:15)
    ==10096== Address 0x5203568 is 0 bytes after a block of size 40 alloc'd
    ==10096== at 0x4C2B9B5: calloc (vg_replace_malloc.c:711)
    ==10096== by 0x401304: readClientFile (client.c:29)
    ==10096== by 0x400C0B: main (main.c:15)
    ==10096==
    ==10096==
    ==10096== 1 errors in context 3 of 3:
    ==10096== Invalid read of size 8
    ==10096== at 0x401359: readClientFile (client.c:38)
    ==10096== by 0x400C0B: main (main.c:15)
    ==10096== Address 0x5203568 is 0 bytes after a block of size 40 alloc'd
    ==10096== at 0x4C2B9B5: calloc (vg_replace_malloc.c:711)
    ==10096== by 0x401304: readClientFile (client.c:29)
    ==10096== by 0x400C0B: main (main.c:15)


    It says that problem is about my double pointer array of structures. Related lines are below.



    29.    struct client** clientsArray =calloc(numberOfClients ,sizeof(struct client*));

    for(i= 0 ; i<numberOfClients; i++){
    clientsArray[i] = (struct client*)malloc(sizeof(struct client));

    }
    while(!feof(clientFP)){

    38. fscanf(clientFP, "%s", clientsArray[counter]->clientID);
    39. clientsArray[counter]->clientID[3]='';

    fscanf(clientFP, "%s", clientsArray[counter]->clientIP);
    clientsArray[counter]->clientIP[11]='';

    fscanf(clientFP, "%s", clientsArray[counter]->clientMacAddress);
    clientsArray[counter]->clientMacAddress[11]='';

    counter++;

    }


    //Each line of the file is in this format: A 1.2.3.4 AAAAAAAAAA



    MY CODE TO FREE CLIENTS ARRAY;



    Firstly I freed the variables of each client object. And then I used this.



    for(i = 0 ; i<numberOfClients ; i++){


    free(clients[i]);
    }
    free(clients);


    Can you please help me about what to change?









    share|improve this question

























      0












      0








      0







      I got this error from Valgrind. I could bot find the solution to fix this.
      When I try to free my allocations I get segmentation fault and these errors.



      > ==10096== ERROR SUMMARY: 3 errors from 3 contexts (suppressed: 0 from 0)
      ==10096==
      ==10096== 1 errors in context 1 of 3:
      ==10096== Invalid write of size 1
      ==10096== at 0x40138C: readClientFile (client.c:39)
      ==10096== by 0x400C0B: main (main.c:15)
      ==10096== Address 0x3 is not stack'd, malloc'd or (recently) free'd
      ==10096==
      ==10096==
      ==10096== 1 errors in context 2 of 3:
      ==10096== Invalid read of size 8
      ==10096== at 0x401389: readClientFile (client.c:39)
      ==10096== by 0x400C0B: main (main.c:15)
      ==10096== Address 0x5203568 is 0 bytes after a block of size 40 alloc'd
      ==10096== at 0x4C2B9B5: calloc (vg_replace_malloc.c:711)
      ==10096== by 0x401304: readClientFile (client.c:29)
      ==10096== by 0x400C0B: main (main.c:15)
      ==10096==
      ==10096==
      ==10096== 1 errors in context 3 of 3:
      ==10096== Invalid read of size 8
      ==10096== at 0x401359: readClientFile (client.c:38)
      ==10096== by 0x400C0B: main (main.c:15)
      ==10096== Address 0x5203568 is 0 bytes after a block of size 40 alloc'd
      ==10096== at 0x4C2B9B5: calloc (vg_replace_malloc.c:711)
      ==10096== by 0x401304: readClientFile (client.c:29)
      ==10096== by 0x400C0B: main (main.c:15)


      It says that problem is about my double pointer array of structures. Related lines are below.



      29.    struct client** clientsArray =calloc(numberOfClients ,sizeof(struct client*));

      for(i= 0 ; i<numberOfClients; i++){
      clientsArray[i] = (struct client*)malloc(sizeof(struct client));

      }
      while(!feof(clientFP)){

      38. fscanf(clientFP, "%s", clientsArray[counter]->clientID);
      39. clientsArray[counter]->clientID[3]='';

      fscanf(clientFP, "%s", clientsArray[counter]->clientIP);
      clientsArray[counter]->clientIP[11]='';

      fscanf(clientFP, "%s", clientsArray[counter]->clientMacAddress);
      clientsArray[counter]->clientMacAddress[11]='';

      counter++;

      }


      //Each line of the file is in this format: A 1.2.3.4 AAAAAAAAAA



      MY CODE TO FREE CLIENTS ARRAY;



      Firstly I freed the variables of each client object. And then I used this.



      for(i = 0 ; i<numberOfClients ; i++){


      free(clients[i]);
      }
      free(clients);


      Can you please help me about what to change?









      share|improve this question













      I got this error from Valgrind. I could bot find the solution to fix this.
      When I try to free my allocations I get segmentation fault and these errors.



      > ==10096== ERROR SUMMARY: 3 errors from 3 contexts (suppressed: 0 from 0)
      ==10096==
      ==10096== 1 errors in context 1 of 3:
      ==10096== Invalid write of size 1
      ==10096== at 0x40138C: readClientFile (client.c:39)
      ==10096== by 0x400C0B: main (main.c:15)
      ==10096== Address 0x3 is not stack'd, malloc'd or (recently) free'd
      ==10096==
      ==10096==
      ==10096== 1 errors in context 2 of 3:
      ==10096== Invalid read of size 8
      ==10096== at 0x401389: readClientFile (client.c:39)
      ==10096== by 0x400C0B: main (main.c:15)
      ==10096== Address 0x5203568 is 0 bytes after a block of size 40 alloc'd
      ==10096== at 0x4C2B9B5: calloc (vg_replace_malloc.c:711)
      ==10096== by 0x401304: readClientFile (client.c:29)
      ==10096== by 0x400C0B: main (main.c:15)
      ==10096==
      ==10096==
      ==10096== 1 errors in context 3 of 3:
      ==10096== Invalid read of size 8
      ==10096== at 0x401359: readClientFile (client.c:38)
      ==10096== by 0x400C0B: main (main.c:15)
      ==10096== Address 0x5203568 is 0 bytes after a block of size 40 alloc'd
      ==10096== at 0x4C2B9B5: calloc (vg_replace_malloc.c:711)
      ==10096== by 0x401304: readClientFile (client.c:29)
      ==10096== by 0x400C0B: main (main.c:15)


      It says that problem is about my double pointer array of structures. Related lines are below.



      29.    struct client** clientsArray =calloc(numberOfClients ,sizeof(struct client*));

      for(i= 0 ; i<numberOfClients; i++){
      clientsArray[i] = (struct client*)malloc(sizeof(struct client));

      }
      while(!feof(clientFP)){

      38. fscanf(clientFP, "%s", clientsArray[counter]->clientID);
      39. clientsArray[counter]->clientID[3]='';

      fscanf(clientFP, "%s", clientsArray[counter]->clientIP);
      clientsArray[counter]->clientIP[11]='';

      fscanf(clientFP, "%s", clientsArray[counter]->clientMacAddress);
      clientsArray[counter]->clientMacAddress[11]='';

      counter++;

      }


      //Each line of the file is in this format: A 1.2.3.4 AAAAAAAAAA



      MY CODE TO FREE CLIENTS ARRAY;



      Firstly I freed the variables of each client object. And then I used this.



      for(i = 0 ; i<numberOfClients ; i++){


      free(clients[i]);
      }
      free(clients);


      Can you please help me about what to change?






      pointers struct error-handling double valgrind






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 22 at 22:36









      ayseeee

      165




      165





























          active

          oldest

          votes











          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%2f53438719%2fvalgrind-invaid-read-write-of-size-1-error-in-fscanf-and-struct-double-pointer%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2f53438719%2fvalgrind-invaid-read-write-of-size-1-error-in-fscanf-and-struct-double-pointer%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

          Trompette piccolo

          Slow SSRS Report in dynamic grouping and multiple parameters

          Simon Yates (cyclisme)