Small Problems with Bubble Sort Method












-3















My visual studio always trows different exepctions.(Bubble sort.exe has triggered a breakpoint.)
sometimes on line 6 :(arr_2 = (int *)malloc(size);
Sometimes on lines where is free (arr) and free(arr_2);



int bubble(int size, int * arr) {
for (int i = 0; i < size; i++) {
printf("array[%d] = %d n", i, arr[i]);
}
int * arr_2;
arr_2 = (int *)malloc(size);

for (int i = 0; i < size; i++) {
arr_2[i] = arr[i];
}
for (int i = 0; i < size; i++) {
printf("2array_2[%d] = %d n", i, arr_2[i]);
}
int numb;
for (int i = 0; i < size; i++) {
if (arr[i] > arr[i + 1]) {
numb = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = numb;
}

}
free(arr_2);
return 0;
};
int main(){
char size[100];
printf("Hello! Please enter the size of the array that will be sorted with
bubble sort method n Maximum size is 100 n");
gets_s(size);
int size_1 = atoi(size);
int * arr;
arr = (int *)malloc(size_1);
for (int i = 0; i < size_1; i++) {
arr[i] = rand();
}
bubble(size_1, arr);


free(arr);
return 0;
}









share|improve this question




















  • 1





    Welcome to StackOverflow! Please edit your title to better describe your problem. You additionally do not need to list the language (it's covered by the tags), and it doesn't need to be all uppercase. Editing your code to fix the indentation will additionally help readers.

    – AndyG
    Nov 15 '18 at 14:56








  • 1





    this is either c, or c++ trying very hard to look like c. why are you using malloc and free ?

    – user463035818
    Nov 15 '18 at 14:57








  • 2





    I think you need a read of this ericlippert.com/2014/03/05/how-to-debug-small-programs The error with malloc will be easy to see when you put a breakpoint there

    – UKMonkey
    Nov 15 '18 at 14:59











  • i find it a bit sad that c++ has to argue against using c stuff so often. I dont know any c, but I am pretty sure it is a language almost as beautiful as c++, there is just no valid reason to use malloc and free in your code if it is c++

    – user463035818
    Nov 15 '18 at 14:59













  • If you have problems with malloc() and free(), you don't have small problems, you have BIG problems. Storage management is one of the most terrible things for entry-level programmers (and the others as well). As this is tagged C++, why not std::vector? It makes things much easier (no malloc()/free() nor new/delete needed).

    – Scheff
    Nov 15 '18 at 15:00


















-3















My visual studio always trows different exepctions.(Bubble sort.exe has triggered a breakpoint.)
sometimes on line 6 :(arr_2 = (int *)malloc(size);
Sometimes on lines where is free (arr) and free(arr_2);



int bubble(int size, int * arr) {
for (int i = 0; i < size; i++) {
printf("array[%d] = %d n", i, arr[i]);
}
int * arr_2;
arr_2 = (int *)malloc(size);

for (int i = 0; i < size; i++) {
arr_2[i] = arr[i];
}
for (int i = 0; i < size; i++) {
printf("2array_2[%d] = %d n", i, arr_2[i]);
}
int numb;
for (int i = 0; i < size; i++) {
if (arr[i] > arr[i + 1]) {
numb = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = numb;
}

}
free(arr_2);
return 0;
};
int main(){
char size[100];
printf("Hello! Please enter the size of the array that will be sorted with
bubble sort method n Maximum size is 100 n");
gets_s(size);
int size_1 = atoi(size);
int * arr;
arr = (int *)malloc(size_1);
for (int i = 0; i < size_1; i++) {
arr[i] = rand();
}
bubble(size_1, arr);


free(arr);
return 0;
}









share|improve this question




















  • 1





    Welcome to StackOverflow! Please edit your title to better describe your problem. You additionally do not need to list the language (it's covered by the tags), and it doesn't need to be all uppercase. Editing your code to fix the indentation will additionally help readers.

    – AndyG
    Nov 15 '18 at 14:56








  • 1





    this is either c, or c++ trying very hard to look like c. why are you using malloc and free ?

    – user463035818
    Nov 15 '18 at 14:57








  • 2





    I think you need a read of this ericlippert.com/2014/03/05/how-to-debug-small-programs The error with malloc will be easy to see when you put a breakpoint there

    – UKMonkey
    Nov 15 '18 at 14:59











  • i find it a bit sad that c++ has to argue against using c stuff so often. I dont know any c, but I am pretty sure it is a language almost as beautiful as c++, there is just no valid reason to use malloc and free in your code if it is c++

    – user463035818
    Nov 15 '18 at 14:59













  • If you have problems with malloc() and free(), you don't have small problems, you have BIG problems. Storage management is one of the most terrible things for entry-level programmers (and the others as well). As this is tagged C++, why not std::vector? It makes things much easier (no malloc()/free() nor new/delete needed).

    – Scheff
    Nov 15 '18 at 15:00
















-3












-3








-3








My visual studio always trows different exepctions.(Bubble sort.exe has triggered a breakpoint.)
sometimes on line 6 :(arr_2 = (int *)malloc(size);
Sometimes on lines where is free (arr) and free(arr_2);



int bubble(int size, int * arr) {
for (int i = 0; i < size; i++) {
printf("array[%d] = %d n", i, arr[i]);
}
int * arr_2;
arr_2 = (int *)malloc(size);

for (int i = 0; i < size; i++) {
arr_2[i] = arr[i];
}
for (int i = 0; i < size; i++) {
printf("2array_2[%d] = %d n", i, arr_2[i]);
}
int numb;
for (int i = 0; i < size; i++) {
if (arr[i] > arr[i + 1]) {
numb = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = numb;
}

}
free(arr_2);
return 0;
};
int main(){
char size[100];
printf("Hello! Please enter the size of the array that will be sorted with
bubble sort method n Maximum size is 100 n");
gets_s(size);
int size_1 = atoi(size);
int * arr;
arr = (int *)malloc(size_1);
for (int i = 0; i < size_1; i++) {
arr[i] = rand();
}
bubble(size_1, arr);


free(arr);
return 0;
}









share|improve this question
















My visual studio always trows different exepctions.(Bubble sort.exe has triggered a breakpoint.)
sometimes on line 6 :(arr_2 = (int *)malloc(size);
Sometimes on lines where is free (arr) and free(arr_2);



int bubble(int size, int * arr) {
for (int i = 0; i < size; i++) {
printf("array[%d] = %d n", i, arr[i]);
}
int * arr_2;
arr_2 = (int *)malloc(size);

for (int i = 0; i < size; i++) {
arr_2[i] = arr[i];
}
for (int i = 0; i < size; i++) {
printf("2array_2[%d] = %d n", i, arr_2[i]);
}
int numb;
for (int i = 0; i < size; i++) {
if (arr[i] > arr[i + 1]) {
numb = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = numb;
}

}
free(arr_2);
return 0;
};
int main(){
char size[100];
printf("Hello! Please enter the size of the array that will be sorted with
bubble sort method n Maximum size is 100 n");
gets_s(size);
int size_1 = atoi(size);
int * arr;
arr = (int *)malloc(size_1);
for (int i = 0; i < size_1; i++) {
arr[i] = rand();
}
bubble(size_1, arr);


free(arr);
return 0;
}






c malloc






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 17:11







Andres Gorohhov

















asked Nov 15 '18 at 14:55









Andres GorohhovAndres Gorohhov

1




1








  • 1





    Welcome to StackOverflow! Please edit your title to better describe your problem. You additionally do not need to list the language (it's covered by the tags), and it doesn't need to be all uppercase. Editing your code to fix the indentation will additionally help readers.

    – AndyG
    Nov 15 '18 at 14:56








  • 1





    this is either c, or c++ trying very hard to look like c. why are you using malloc and free ?

    – user463035818
    Nov 15 '18 at 14:57








  • 2





    I think you need a read of this ericlippert.com/2014/03/05/how-to-debug-small-programs The error with malloc will be easy to see when you put a breakpoint there

    – UKMonkey
    Nov 15 '18 at 14:59











  • i find it a bit sad that c++ has to argue against using c stuff so often. I dont know any c, but I am pretty sure it is a language almost as beautiful as c++, there is just no valid reason to use malloc and free in your code if it is c++

    – user463035818
    Nov 15 '18 at 14:59













  • If you have problems with malloc() and free(), you don't have small problems, you have BIG problems. Storage management is one of the most terrible things for entry-level programmers (and the others as well). As this is tagged C++, why not std::vector? It makes things much easier (no malloc()/free() nor new/delete needed).

    – Scheff
    Nov 15 '18 at 15:00
















  • 1





    Welcome to StackOverflow! Please edit your title to better describe your problem. You additionally do not need to list the language (it's covered by the tags), and it doesn't need to be all uppercase. Editing your code to fix the indentation will additionally help readers.

    – AndyG
    Nov 15 '18 at 14:56








  • 1





    this is either c, or c++ trying very hard to look like c. why are you using malloc and free ?

    – user463035818
    Nov 15 '18 at 14:57








  • 2





    I think you need a read of this ericlippert.com/2014/03/05/how-to-debug-small-programs The error with malloc will be easy to see when you put a breakpoint there

    – UKMonkey
    Nov 15 '18 at 14:59











  • i find it a bit sad that c++ has to argue against using c stuff so often. I dont know any c, but I am pretty sure it is a language almost as beautiful as c++, there is just no valid reason to use malloc and free in your code if it is c++

    – user463035818
    Nov 15 '18 at 14:59













  • If you have problems with malloc() and free(), you don't have small problems, you have BIG problems. Storage management is one of the most terrible things for entry-level programmers (and the others as well). As this is tagged C++, why not std::vector? It makes things much easier (no malloc()/free() nor new/delete needed).

    – Scheff
    Nov 15 '18 at 15:00










1




1





Welcome to StackOverflow! Please edit your title to better describe your problem. You additionally do not need to list the language (it's covered by the tags), and it doesn't need to be all uppercase. Editing your code to fix the indentation will additionally help readers.

– AndyG
Nov 15 '18 at 14:56







Welcome to StackOverflow! Please edit your title to better describe your problem. You additionally do not need to list the language (it's covered by the tags), and it doesn't need to be all uppercase. Editing your code to fix the indentation will additionally help readers.

– AndyG
Nov 15 '18 at 14:56






1




1





this is either c, or c++ trying very hard to look like c. why are you using malloc and free ?

– user463035818
Nov 15 '18 at 14:57







this is either c, or c++ trying very hard to look like c. why are you using malloc and free ?

– user463035818
Nov 15 '18 at 14:57






2




2





I think you need a read of this ericlippert.com/2014/03/05/how-to-debug-small-programs The error with malloc will be easy to see when you put a breakpoint there

– UKMonkey
Nov 15 '18 at 14:59





I think you need a read of this ericlippert.com/2014/03/05/how-to-debug-small-programs The error with malloc will be easy to see when you put a breakpoint there

– UKMonkey
Nov 15 '18 at 14:59













i find it a bit sad that c++ has to argue against using c stuff so often. I dont know any c, but I am pretty sure it is a language almost as beautiful as c++, there is just no valid reason to use malloc and free in your code if it is c++

– user463035818
Nov 15 '18 at 14:59







i find it a bit sad that c++ has to argue against using c stuff so often. I dont know any c, but I am pretty sure it is a language almost as beautiful as c++, there is just no valid reason to use malloc and free in your code if it is c++

– user463035818
Nov 15 '18 at 14:59















If you have problems with malloc() and free(), you don't have small problems, you have BIG problems. Storage management is one of the most terrible things for entry-level programmers (and the others as well). As this is tagged C++, why not std::vector? It makes things much easier (no malloc()/free() nor new/delete needed).

– Scheff
Nov 15 '18 at 15:00







If you have problems with malloc() and free(), you don't have small problems, you have BIG problems. Storage management is one of the most terrible things for entry-level programmers (and the others as well). As this is tagged C++, why not std::vector? It makes things much easier (no malloc()/free() nor new/delete needed).

– Scheff
Nov 15 '18 at 15:00














2 Answers
2






active

oldest

votes


















0














Usу something like this:



int bubble(int size, int * arr)
{
int numb;
for(int i = 0; i < size - 1; i++)
for(int j = 0;j < size - i - 1; j++)
if(arr[j] > arr[j + 1])
{
numb = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = numb;
}

return 0;
};


// using
int main()
{
char size[100];
printf("Hello! Please enter the size of the array that will be sorted with bubble sort method n Maximum size is 100 n");
gets_s(size);
int size_1 = atoi(size);

int * arr = new int[size_1];

for(int i = 0; i < size_1; i++)
arr[i] = rand();
bubble(size_1, arr);
delete arr;
}


You don't need a copy of your input array.






share|improve this answer































    0














    When you reserve memory with malloc you should convert to size in bytes.
    Use malloc( number_of_elements * sizeof( element ) ).



    int size_1 = atoi(size);
    int * arr;
    arr = (int *)malloc(size_1 * sizeof( int ) );
    for (int i = 0; i < size_1; i++) {
    arr[i] = rand();
    }





    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%2f53322156%2fsmall-problems-with-bubble-sort-method%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      Usу something like this:



      int bubble(int size, int * arr)
      {
      int numb;
      for(int i = 0; i < size - 1; i++)
      for(int j = 0;j < size - i - 1; j++)
      if(arr[j] > arr[j + 1])
      {
      numb = arr[j];
      arr[j] = arr[j + 1];
      arr[j + 1] = numb;
      }

      return 0;
      };


      // using
      int main()
      {
      char size[100];
      printf("Hello! Please enter the size of the array that will be sorted with bubble sort method n Maximum size is 100 n");
      gets_s(size);
      int size_1 = atoi(size);

      int * arr = new int[size_1];

      for(int i = 0; i < size_1; i++)
      arr[i] = rand();
      bubble(size_1, arr);
      delete arr;
      }


      You don't need a copy of your input array.






      share|improve this answer




























        0














        Usу something like this:



        int bubble(int size, int * arr)
        {
        int numb;
        for(int i = 0; i < size - 1; i++)
        for(int j = 0;j < size - i - 1; j++)
        if(arr[j] > arr[j + 1])
        {
        numb = arr[j];
        arr[j] = arr[j + 1];
        arr[j + 1] = numb;
        }

        return 0;
        };


        // using
        int main()
        {
        char size[100];
        printf("Hello! Please enter the size of the array that will be sorted with bubble sort method n Maximum size is 100 n");
        gets_s(size);
        int size_1 = atoi(size);

        int * arr = new int[size_1];

        for(int i = 0; i < size_1; i++)
        arr[i] = rand();
        bubble(size_1, arr);
        delete arr;
        }


        You don't need a copy of your input array.






        share|improve this answer


























          0












          0








          0







          Usу something like this:



          int bubble(int size, int * arr)
          {
          int numb;
          for(int i = 0; i < size - 1; i++)
          for(int j = 0;j < size - i - 1; j++)
          if(arr[j] > arr[j + 1])
          {
          numb = arr[j];
          arr[j] = arr[j + 1];
          arr[j + 1] = numb;
          }

          return 0;
          };


          // using
          int main()
          {
          char size[100];
          printf("Hello! Please enter the size of the array that will be sorted with bubble sort method n Maximum size is 100 n");
          gets_s(size);
          int size_1 = atoi(size);

          int * arr = new int[size_1];

          for(int i = 0; i < size_1; i++)
          arr[i] = rand();
          bubble(size_1, arr);
          delete arr;
          }


          You don't need a copy of your input array.






          share|improve this answer













          Usу something like this:



          int bubble(int size, int * arr)
          {
          int numb;
          for(int i = 0; i < size - 1; i++)
          for(int j = 0;j < size - i - 1; j++)
          if(arr[j] > arr[j + 1])
          {
          numb = arr[j];
          arr[j] = arr[j + 1];
          arr[j + 1] = numb;
          }

          return 0;
          };


          // using
          int main()
          {
          char size[100];
          printf("Hello! Please enter the size of the array that will be sorted with bubble sort method n Maximum size is 100 n");
          gets_s(size);
          int size_1 = atoi(size);

          int * arr = new int[size_1];

          for(int i = 0; i < size_1; i++)
          arr[i] = rand();
          bubble(size_1, arr);
          delete arr;
          }


          You don't need a copy of your input array.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 15 '18 at 15:11









          snake_stylesnake_style

          1,170410




          1,170410

























              0














              When you reserve memory with malloc you should convert to size in bytes.
              Use malloc( number_of_elements * sizeof( element ) ).



              int size_1 = atoi(size);
              int * arr;
              arr = (int *)malloc(size_1 * sizeof( int ) );
              for (int i = 0; i < size_1; i++) {
              arr[i] = rand();
              }





              share|improve this answer




























                0














                When you reserve memory with malloc you should convert to size in bytes.
                Use malloc( number_of_elements * sizeof( element ) ).



                int size_1 = atoi(size);
                int * arr;
                arr = (int *)malloc(size_1 * sizeof( int ) );
                for (int i = 0; i < size_1; i++) {
                arr[i] = rand();
                }





                share|improve this answer


























                  0












                  0








                  0







                  When you reserve memory with malloc you should convert to size in bytes.
                  Use malloc( number_of_elements * sizeof( element ) ).



                  int size_1 = atoi(size);
                  int * arr;
                  arr = (int *)malloc(size_1 * sizeof( int ) );
                  for (int i = 0; i < size_1; i++) {
                  arr[i] = rand();
                  }





                  share|improve this answer













                  When you reserve memory with malloc you should convert to size in bytes.
                  Use malloc( number_of_elements * sizeof( element ) ).



                  int size_1 = atoi(size);
                  int * arr;
                  arr = (int *)malloc(size_1 * sizeof( int ) );
                  for (int i = 0; i < size_1; i++) {
                  arr[i] = rand();
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 15 '18 at 15:11









                  Pablo AlegrePablo Alegre

                  393




                  393






























                      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%2f53322156%2fsmall-problems-with-bubble-sort-method%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







                      這個網誌中的熱門文章

                      Academy of Television Arts & Sciences

                      L'Équipe

                      1995 France bombings