Can't find the problem after debugging the code





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















When debugging the following code, The debugger point to the line(The line that I commented it out in the last function "printResults" below), But couldn't figure out what is the problem and why the debugger point to that line.



#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>

#define BUFFER_SIZE 256


void swap(char** a, char** b) {
char* temp = *a;
*a = *b;
*b = temp;
}

char* getLongestString(char** strings, int size) {
char* max = strings[0];
for (int i = 1; i < size; i++) {
if (strlen(max) < strlen(strings[i])) {
max = strings[i];
}
}
return max;
}

void sortStrings(char** strings, int size) {
bool changed = true;
while (changed) {
changed = false;
for (int i = 0; i < size - 1; i++) {
if (strcmp(strings[i], strings[i + 1]) >= 0) {
swap(&strings[i], &strings[i + 1]);
changed = true;
}
}
}
}

int readSize() {
int size = 0;
printf("Enter number of strings:n");
scanf("%d", &size);
return size;
}

void printResults(char** words, int size) {
char* longest = getLongestString(words, size);
printf("The longest word is: %sn", longest);
sortStrings(words, size);
//printf("The maximal word lexicographically is: %sn", words[size]);
printf("The minimal word lexicographically is: %sn", words[0]);
}









share|improve this question


















  • 1





    Please edit your post and post your entire code. We need to see how printResults is called. At a guess, if size is the number of elements in words, then words[size] is one beyond the end of the array [and it could have a random value that causes a segfault]. You'd need words[size - 1] instead

    – Craig Estey
    Nov 26 '18 at 20:59


















0















When debugging the following code, The debugger point to the line(The line that I commented it out in the last function "printResults" below), But couldn't figure out what is the problem and why the debugger point to that line.



#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>

#define BUFFER_SIZE 256


void swap(char** a, char** b) {
char* temp = *a;
*a = *b;
*b = temp;
}

char* getLongestString(char** strings, int size) {
char* max = strings[0];
for (int i = 1; i < size; i++) {
if (strlen(max) < strlen(strings[i])) {
max = strings[i];
}
}
return max;
}

void sortStrings(char** strings, int size) {
bool changed = true;
while (changed) {
changed = false;
for (int i = 0; i < size - 1; i++) {
if (strcmp(strings[i], strings[i + 1]) >= 0) {
swap(&strings[i], &strings[i + 1]);
changed = true;
}
}
}
}

int readSize() {
int size = 0;
printf("Enter number of strings:n");
scanf("%d", &size);
return size;
}

void printResults(char** words, int size) {
char* longest = getLongestString(words, size);
printf("The longest word is: %sn", longest);
sortStrings(words, size);
//printf("The maximal word lexicographically is: %sn", words[size]);
printf("The minimal word lexicographically is: %sn", words[0]);
}









share|improve this question


















  • 1





    Please edit your post and post your entire code. We need to see how printResults is called. At a guess, if size is the number of elements in words, then words[size] is one beyond the end of the array [and it could have a random value that causes a segfault]. You'd need words[size - 1] instead

    – Craig Estey
    Nov 26 '18 at 20:59














0












0








0








When debugging the following code, The debugger point to the line(The line that I commented it out in the last function "printResults" below), But couldn't figure out what is the problem and why the debugger point to that line.



#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>

#define BUFFER_SIZE 256


void swap(char** a, char** b) {
char* temp = *a;
*a = *b;
*b = temp;
}

char* getLongestString(char** strings, int size) {
char* max = strings[0];
for (int i = 1; i < size; i++) {
if (strlen(max) < strlen(strings[i])) {
max = strings[i];
}
}
return max;
}

void sortStrings(char** strings, int size) {
bool changed = true;
while (changed) {
changed = false;
for (int i = 0; i < size - 1; i++) {
if (strcmp(strings[i], strings[i + 1]) >= 0) {
swap(&strings[i], &strings[i + 1]);
changed = true;
}
}
}
}

int readSize() {
int size = 0;
printf("Enter number of strings:n");
scanf("%d", &size);
return size;
}

void printResults(char** words, int size) {
char* longest = getLongestString(words, size);
printf("The longest word is: %sn", longest);
sortStrings(words, size);
//printf("The maximal word lexicographically is: %sn", words[size]);
printf("The minimal word lexicographically is: %sn", words[0]);
}









share|improve this question














When debugging the following code, The debugger point to the line(The line that I commented it out in the last function "printResults" below), But couldn't figure out what is the problem and why the debugger point to that line.



#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>

#define BUFFER_SIZE 256


void swap(char** a, char** b) {
char* temp = *a;
*a = *b;
*b = temp;
}

char* getLongestString(char** strings, int size) {
char* max = strings[0];
for (int i = 1; i < size; i++) {
if (strlen(max) < strlen(strings[i])) {
max = strings[i];
}
}
return max;
}

void sortStrings(char** strings, int size) {
bool changed = true;
while (changed) {
changed = false;
for (int i = 0; i < size - 1; i++) {
if (strcmp(strings[i], strings[i + 1]) >= 0) {
swap(&strings[i], &strings[i + 1]);
changed = true;
}
}
}
}

int readSize() {
int size = 0;
printf("Enter number of strings:n");
scanf("%d", &size);
return size;
}

void printResults(char** words, int size) {
char* longest = getLongestString(words, size);
printf("The longest word is: %sn", longest);
sortStrings(words, size);
//printf("The maximal word lexicographically is: %sn", words[size]);
printf("The minimal word lexicographically is: %sn", words[0]);
}






c function debugging






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 26 '18 at 20:49









D.AD.A

83




83








  • 1





    Please edit your post and post your entire code. We need to see how printResults is called. At a guess, if size is the number of elements in words, then words[size] is one beyond the end of the array [and it could have a random value that causes a segfault]. You'd need words[size - 1] instead

    – Craig Estey
    Nov 26 '18 at 20:59














  • 1





    Please edit your post and post your entire code. We need to see how printResults is called. At a guess, if size is the number of elements in words, then words[size] is one beyond the end of the array [and it could have a random value that causes a segfault]. You'd need words[size - 1] instead

    – Craig Estey
    Nov 26 '18 at 20:59








1




1





Please edit your post and post your entire code. We need to see how printResults is called. At a guess, if size is the number of elements in words, then words[size] is one beyond the end of the array [and it could have a random value that causes a segfault]. You'd need words[size - 1] instead

– Craig Estey
Nov 26 '18 at 20:59





Please edit your post and post your entire code. We need to see how printResults is called. At a guess, if size is the number of elements in words, then words[size] is one beyond the end of the array [and it could have a random value that causes a segfault]. You'd need words[size - 1] instead

– Craig Estey
Nov 26 '18 at 20:59












2 Answers
2






active

oldest

votes


















0














Think that the deceleration of array when you declare an array you do not count index 0 as first element
but when you using the address of that data first index get's in consideration for example,lets declare an array



int x[5];


by doing that you are telling the compiler to allocate 5 available integer memory address.So you will get an allocated area and your size will be 5.
Lets assume the compiler gives you 5 address starting from 0x20000580--0x20000584
by writing



x[0] = data0;// you are writing data to  the address of 0x20000580-> data0             
x[1]= data1;// 0x20000581-> data1
...
x[4]= data4 //0x20000584->data4


so in your case when you try to write x[size] which is x[5] you are trying to access
an un-allocated area and this cause hard fault
interrupt and you will face unhand-led exception.






share|improve this answer































    0














    Take a look at this line



    printf("The maximal word lexicographically is: %sn", words[size]);
    ^^^^^^^^^^^


    the last valid element in an array is at index size-1 but you index the array using size. In other words - an out of bounds access.



    So change to



    printf("The maximal word lexicographically is: %sn", words[size - 1]);
    ^^^^^^^^^^^^^^^


    BTW:



    This line



    if (strcmp(strings[i], strings[i + 1]) >= 0) {


    seems to open up for an endless loop in case two strings are identical. You probably don't want >= but only >






    share|improve this answer


























    • When I modifiy this and run the program in debugger(using gdb in bsah) then enter the "bt" command, I get "No Stack"....!!!

      – D.A
      Nov 26 '18 at 21:54











    • @D.A you haven't shown how you call the function and how you define the array. So there can be a bug in that code. So you need to post that code as well. Here is a working example. ideone.com/OPYzRL

      – 4386427
      Nov 27 '18 at 6:00












    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%2f53488828%2fcant-find-the-problem-after-debugging-the-code%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














    Think that the deceleration of array when you declare an array you do not count index 0 as first element
    but when you using the address of that data first index get's in consideration for example,lets declare an array



    int x[5];


    by doing that you are telling the compiler to allocate 5 available integer memory address.So you will get an allocated area and your size will be 5.
    Lets assume the compiler gives you 5 address starting from 0x20000580--0x20000584
    by writing



    x[0] = data0;// you are writing data to  the address of 0x20000580-> data0             
    x[1]= data1;// 0x20000581-> data1
    ...
    x[4]= data4 //0x20000584->data4


    so in your case when you try to write x[size] which is x[5] you are trying to access
    an un-allocated area and this cause hard fault
    interrupt and you will face unhand-led exception.






    share|improve this answer




























      0














      Think that the deceleration of array when you declare an array you do not count index 0 as first element
      but when you using the address of that data first index get's in consideration for example,lets declare an array



      int x[5];


      by doing that you are telling the compiler to allocate 5 available integer memory address.So you will get an allocated area and your size will be 5.
      Lets assume the compiler gives you 5 address starting from 0x20000580--0x20000584
      by writing



      x[0] = data0;// you are writing data to  the address of 0x20000580-> data0             
      x[1]= data1;// 0x20000581-> data1
      ...
      x[4]= data4 //0x20000584->data4


      so in your case when you try to write x[size] which is x[5] you are trying to access
      an un-allocated area and this cause hard fault
      interrupt and you will face unhand-led exception.






      share|improve this answer


























        0












        0








        0







        Think that the deceleration of array when you declare an array you do not count index 0 as first element
        but when you using the address of that data first index get's in consideration for example,lets declare an array



        int x[5];


        by doing that you are telling the compiler to allocate 5 available integer memory address.So you will get an allocated area and your size will be 5.
        Lets assume the compiler gives you 5 address starting from 0x20000580--0x20000584
        by writing



        x[0] = data0;// you are writing data to  the address of 0x20000580-> data0             
        x[1]= data1;// 0x20000581-> data1
        ...
        x[4]= data4 //0x20000584->data4


        so in your case when you try to write x[size] which is x[5] you are trying to access
        an un-allocated area and this cause hard fault
        interrupt and you will face unhand-led exception.






        share|improve this answer













        Think that the deceleration of array when you declare an array you do not count index 0 as first element
        but when you using the address of that data first index get's in consideration for example,lets declare an array



        int x[5];


        by doing that you are telling the compiler to allocate 5 available integer memory address.So you will get an allocated area and your size will be 5.
        Lets assume the compiler gives you 5 address starting from 0x20000580--0x20000584
        by writing



        x[0] = data0;// you are writing data to  the address of 0x20000580-> data0             
        x[1]= data1;// 0x20000581-> data1
        ...
        x[4]= data4 //0x20000584->data4


        so in your case when you try to write x[size] which is x[5] you are trying to access
        an un-allocated area and this cause hard fault
        interrupt and you will face unhand-led exception.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 26 '18 at 21:36









        Cagri CandanCagri Candan

        6219




        6219

























            0














            Take a look at this line



            printf("The maximal word lexicographically is: %sn", words[size]);
            ^^^^^^^^^^^


            the last valid element in an array is at index size-1 but you index the array using size. In other words - an out of bounds access.



            So change to



            printf("The maximal word lexicographically is: %sn", words[size - 1]);
            ^^^^^^^^^^^^^^^


            BTW:



            This line



            if (strcmp(strings[i], strings[i + 1]) >= 0) {


            seems to open up for an endless loop in case two strings are identical. You probably don't want >= but only >






            share|improve this answer


























            • When I modifiy this and run the program in debugger(using gdb in bsah) then enter the "bt" command, I get "No Stack"....!!!

              – D.A
              Nov 26 '18 at 21:54











            • @D.A you haven't shown how you call the function and how you define the array. So there can be a bug in that code. So you need to post that code as well. Here is a working example. ideone.com/OPYzRL

              – 4386427
              Nov 27 '18 at 6:00
















            0














            Take a look at this line



            printf("The maximal word lexicographically is: %sn", words[size]);
            ^^^^^^^^^^^


            the last valid element in an array is at index size-1 but you index the array using size. In other words - an out of bounds access.



            So change to



            printf("The maximal word lexicographically is: %sn", words[size - 1]);
            ^^^^^^^^^^^^^^^


            BTW:



            This line



            if (strcmp(strings[i], strings[i + 1]) >= 0) {


            seems to open up for an endless loop in case two strings are identical. You probably don't want >= but only >






            share|improve this answer


























            • When I modifiy this and run the program in debugger(using gdb in bsah) then enter the "bt" command, I get "No Stack"....!!!

              – D.A
              Nov 26 '18 at 21:54











            • @D.A you haven't shown how you call the function and how you define the array. So there can be a bug in that code. So you need to post that code as well. Here is a working example. ideone.com/OPYzRL

              – 4386427
              Nov 27 '18 at 6:00














            0












            0








            0







            Take a look at this line



            printf("The maximal word lexicographically is: %sn", words[size]);
            ^^^^^^^^^^^


            the last valid element in an array is at index size-1 but you index the array using size. In other words - an out of bounds access.



            So change to



            printf("The maximal word lexicographically is: %sn", words[size - 1]);
            ^^^^^^^^^^^^^^^


            BTW:



            This line



            if (strcmp(strings[i], strings[i + 1]) >= 0) {


            seems to open up for an endless loop in case two strings are identical. You probably don't want >= but only >






            share|improve this answer















            Take a look at this line



            printf("The maximal word lexicographically is: %sn", words[size]);
            ^^^^^^^^^^^


            the last valid element in an array is at index size-1 but you index the array using size. In other words - an out of bounds access.



            So change to



            printf("The maximal word lexicographically is: %sn", words[size - 1]);
            ^^^^^^^^^^^^^^^


            BTW:



            This line



            if (strcmp(strings[i], strings[i + 1]) >= 0) {


            seems to open up for an endless loop in case two strings are identical. You probably don't want >= but only >







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 27 '18 at 14:39

























            answered Nov 26 '18 at 20:58









            43864274386427

            22.2k31846




            22.2k31846













            • When I modifiy this and run the program in debugger(using gdb in bsah) then enter the "bt" command, I get "No Stack"....!!!

              – D.A
              Nov 26 '18 at 21:54











            • @D.A you haven't shown how you call the function and how you define the array. So there can be a bug in that code. So you need to post that code as well. Here is a working example. ideone.com/OPYzRL

              – 4386427
              Nov 27 '18 at 6:00



















            • When I modifiy this and run the program in debugger(using gdb in bsah) then enter the "bt" command, I get "No Stack"....!!!

              – D.A
              Nov 26 '18 at 21:54











            • @D.A you haven't shown how you call the function and how you define the array. So there can be a bug in that code. So you need to post that code as well. Here is a working example. ideone.com/OPYzRL

              – 4386427
              Nov 27 '18 at 6:00

















            When I modifiy this and run the program in debugger(using gdb in bsah) then enter the "bt" command, I get "No Stack"....!!!

            – D.A
            Nov 26 '18 at 21:54





            When I modifiy this and run the program in debugger(using gdb in bsah) then enter the "bt" command, I get "No Stack"....!!!

            – D.A
            Nov 26 '18 at 21:54













            @D.A you haven't shown how you call the function and how you define the array. So there can be a bug in that code. So you need to post that code as well. Here is a working example. ideone.com/OPYzRL

            – 4386427
            Nov 27 '18 at 6:00





            @D.A you haven't shown how you call the function and how you define the array. So there can be a bug in that code. So you need to post that code as well. Here is a working example. ideone.com/OPYzRL

            – 4386427
            Nov 27 '18 at 6:00


















            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%2f53488828%2fcant-find-the-problem-after-debugging-the-code%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

            To store a contact into the json file from server.js file using a class in NodeJS

            Redirect URL with Chrome Remote Debugging Android Devices

            Dieringhausen