Why do i receive segmentation fault for any MAXLEN <10000? [closed]With arrays, why is it the case that a[5] == 5[a]?What is a segmentation fault?C - Malloc issue (maybe something else)fill shape with CWhy are elementwise additions much faster in separate loops than in a combined loop?C: printf() and putchar() questionsWhy does the C preprocessor interpret the word “linux” as the constant “1”?Segmentation fault from a function that is not called at allConfused understanding a passage about char and int types from K&R's “The C Programming Language”Strcpy() Segmentation Fault

How can an attacker use robots.txt?

Does wetting a beer glass change the foam characteristics?

practicality of 30 year fix mortgage at 55 years of age

Which place in our solar system is the most fit for terraforming?

Hiking with a mule or two?

Safe to use 220V electric clothes dryer when building has been bridged down to 110V?

Why does (inf + 0j)*1 evaluate to inf + nanj?

Can you cast Dispel Magic on a Shadow Monk's Silence?

Reorder a matrix, twice

Does Sitecore have support for Sitecore products in containers?

Is it possible to encode a message in such a way that can only be read by someone or something capable of seeing into the very near future?

Is there any iPhone SE out there with 3D Touch?

What exactly did this mechanic sabotage on the American Airlines 737, and how dangerous was it?

To what extent is it worthwhile to report check fraud / refund scams?

Is there a way to hide HTML source code yet keeping it effective?

word frequency from file using partial match

What is the meaning of "heutig" in this sentence?

To change trains = cambiare treno?

extracting sublists

Why are there two fundamental laws of logic?

Is the mass of paint relevant in rocket design?

Does "as soon as" imply simultaneity?

Does HTTP HSTS protect a domain from a bad-actor publically-trusted-CA issing a illegitimate valid certificate?

Safely hang a mirror that does not have hooks



Why do i receive segmentation fault for any MAXLEN


With arrays, why is it the case that a[5] == 5[a]?What is a segmentation fault?C - Malloc issue (maybe something else)fill shape with CWhy are elementwise additions much faster in separate loops than in a combined loop?C: printf() and putchar() questionsWhy does the C preprocessor interpret the word “linux” as the constant “1”?Segmentation fault from a function that is not called at allConfused understanding a passage about char and int types from K&R's “The C Programming Language”Strcpy() Segmentation Fault






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








-2















this the 1-17 problem from kernighan & ritchie, it 's quite a simple problem but when i tested with the value MAXLEN=1000 the complier displays segmentation fault and i dont quite understand why because 1000 seems like quite a resonable value , it's seems to work 10000 but why?



#include <stdio.h>
#define MINL 80
#define MAXLEN 1000
int get_line (char s[] , int lim )

int c,i,j;
for (i=0; (c=getchar())!=EOF && c!='n';i++)

if (i<lim-2)

s[j]=c;
++j;

if (c=='n')


s[j]=c;
++i;
++j;

s[j]='';

return i;


int main ()

int len;
char line[MAXLEN];
while ((len=get_line(line,MAXLEN))>0)

if (len>MAXLEN)

printf("%s",line);




return 0;










share|improve this question
















closed as off-topic by Antti Haapala, Petter Friberg, M--, Pearly Spencer, Paul Roub Mar 28 at 18:20


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – Antti Haapala, Petter Friberg, M--, Pearly Spencer, Paul Roub
If this question can be reworded to fit the rules in the help center, please edit the question.












  • 7





    j is uninitialized.

    – Eugene Sh.
    Mar 28 at 17:02











  • as said by @EugeneSh. j is uninitialized, and this is a sign : you do not need it ^^ I put an answer with proposal fixing the problem

    – bruno
    Mar 28 at 17:34

















-2















this the 1-17 problem from kernighan & ritchie, it 's quite a simple problem but when i tested with the value MAXLEN=1000 the complier displays segmentation fault and i dont quite understand why because 1000 seems like quite a resonable value , it's seems to work 10000 but why?



#include <stdio.h>
#define MINL 80
#define MAXLEN 1000
int get_line (char s[] , int lim )

int c,i,j;
for (i=0; (c=getchar())!=EOF && c!='n';i++)

if (i<lim-2)

s[j]=c;
++j;

if (c=='n')


s[j]=c;
++i;
++j;

s[j]='';

return i;


int main ()

int len;
char line[MAXLEN];
while ((len=get_line(line,MAXLEN))>0)

if (len>MAXLEN)

printf("%s",line);




return 0;










share|improve this question
















closed as off-topic by Antti Haapala, Petter Friberg, M--, Pearly Spencer, Paul Roub Mar 28 at 18:20


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – Antti Haapala, Petter Friberg, M--, Pearly Spencer, Paul Roub
If this question can be reworded to fit the rules in the help center, please edit the question.












  • 7





    j is uninitialized.

    – Eugene Sh.
    Mar 28 at 17:02











  • as said by @EugeneSh. j is uninitialized, and this is a sign : you do not need it ^^ I put an answer with proposal fixing the problem

    – bruno
    Mar 28 at 17:34













-2












-2








-2








this the 1-17 problem from kernighan & ritchie, it 's quite a simple problem but when i tested with the value MAXLEN=1000 the complier displays segmentation fault and i dont quite understand why because 1000 seems like quite a resonable value , it's seems to work 10000 but why?



#include <stdio.h>
#define MINL 80
#define MAXLEN 1000
int get_line (char s[] , int lim )

int c,i,j;
for (i=0; (c=getchar())!=EOF && c!='n';i++)

if (i<lim-2)

s[j]=c;
++j;

if (c=='n')


s[j]=c;
++i;
++j;

s[j]='';

return i;


int main ()

int len;
char line[MAXLEN];
while ((len=get_line(line,MAXLEN))>0)

if (len>MAXLEN)

printf("%s",line);




return 0;










share|improve this question
















this the 1-17 problem from kernighan & ritchie, it 's quite a simple problem but when i tested with the value MAXLEN=1000 the complier displays segmentation fault and i dont quite understand why because 1000 seems like quite a resonable value , it's seems to work 10000 but why?



#include <stdio.h>
#define MINL 80
#define MAXLEN 1000
int get_line (char s[] , int lim )

int c,i,j;
for (i=0; (c=getchar())!=EOF && c!='n';i++)

if (i<lim-2)

s[j]=c;
++j;

if (c=='n')


s[j]=c;
++i;
++j;

s[j]='';

return i;


int main ()

int len;
char line[MAXLEN];
while ((len=get_line(line,MAXLEN))>0)

if (len>MAXLEN)

printf("%s",line);




return 0;







c






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 28 at 17:03







eftimo

















asked Mar 28 at 17:01









eftimoeftimo

285 bronze badges




285 bronze badges





closed as off-topic by Antti Haapala, Petter Friberg, M--, Pearly Spencer, Paul Roub Mar 28 at 18:20


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – Antti Haapala, Petter Friberg, M--, Pearly Spencer, Paul Roub
If this question can be reworded to fit the rules in the help center, please edit the question.









closed as off-topic by Antti Haapala, Petter Friberg, M--, Pearly Spencer, Paul Roub Mar 28 at 18:20


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – Antti Haapala, Petter Friberg, M--, Pearly Spencer, Paul Roub
If this question can be reworded to fit the rules in the help center, please edit the question.







closed as off-topic by Antti Haapala, Petter Friberg, M--, Pearly Spencer, Paul Roub Mar 28 at 18:20


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – Antti Haapala, Petter Friberg, M--, Pearly Spencer, Paul Roub
If this question can be reworded to fit the rules in the help center, please edit the question.







  • 7





    j is uninitialized.

    – Eugene Sh.
    Mar 28 at 17:02











  • as said by @EugeneSh. j is uninitialized, and this is a sign : you do not need it ^^ I put an answer with proposal fixing the problem

    – bruno
    Mar 28 at 17:34












  • 7





    j is uninitialized.

    – Eugene Sh.
    Mar 28 at 17:02











  • as said by @EugeneSh. j is uninitialized, and this is a sign : you do not need it ^^ I put an answer with proposal fixing the problem

    – bruno
    Mar 28 at 17:34







7




7





j is uninitialized.

– Eugene Sh.
Mar 28 at 17:02





j is uninitialized.

– Eugene Sh.
Mar 28 at 17:02













as said by @EugeneSh. j is uninitialized, and this is a sign : you do not need it ^^ I put an answer with proposal fixing the problem

– bruno
Mar 28 at 17:34





as said by @EugeneSh. j is uninitialized, and this is a sign : you do not need it ^^ I put an answer with proposal fixing the problem

– bruno
Mar 28 at 17:34












3 Answers
3






active

oldest

votes


















0
















As said in a remark your variable j is non initialized, so when you set s[j] the behavior is undefined and in your case you have a segmentation fault because you write out of the array.



In fact you do not need that variable :



#include <stdio.h>

#define MAXLEN 10

int get_line (char s[] , int lim )

int c, i = 0;

lim -= 1;

while ((c=getchar()) != EOF)

if (i < lim)

s[i] = c;


i += 1;

if (c == 'n')
break;


s[(i < lim) ? i : lim] = '';

return i;


int main ()

int len;
char line[MAXLEN];

while ((len = get_line(line,MAXLEN)) > 0)

/*if (len>MAXLEN)*/

printf("%d '%s'n", len, line);



return 0;



I changed the value of MAXLEN to easily test the case the input string is greater, and also the printf to see the result in all cases



Compilation and execution :



pi@raspberrypi:/tmp $ gcc -pedantic -Wextra -Wall g.c
pi@raspberrypi:/tmp $ ./a.out
aze
4 'aze
'
123456789
10 '123456789'
12345678
9 '12345678
'
1234567890
11 '123456789'
^C


To test the EOF case :



pi@raspberrypi:/tmp $ echo aze | ./a.out
4 'aze
'
pi@raspberrypi:/tmp $





share|improve this answer


































    0
















    You should turn on warnings for your compiler. It would show you that j is uninitialized:



    /home/user/main.c:11:15: warning: variable 'j' is uninitialized when used here [-Wuninitialized]
    s[j]=c;
    ^
    /home/user/main.c:6:14: note: initialize the variable 'j' to silence this warning
    int c,i,j;
    ^
    = 0





    share|improve this answer
































      0
















      the following proposed code:



      1. cleanly compiles

      2. performs the desired functionality

      3. eliminates confusion in the code layout

      4. eliminates unused #define statements

      5. eliminates unneeded variables

      And now, the proposed code:



      #include <stdio.h>


      #define MAXLEN 1000


      int get_line (char s[] , int lim )

      int i;

      for ( i=0; i<(lim - 2); i++ )

      int c = getchar();

      if( c == EOF )

      break;


      s[i] = (char)c;

      if( c =='n' )

      break;



      s[i] = '';

      return i;



      int main ( void )

      int len;
      char line[MAXLEN];
      while ((len=get_line(line,MAXLEN))>0)

      printf("%s",line);


      return 0;






      share|improve this answer

































        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        0
















        As said in a remark your variable j is non initialized, so when you set s[j] the behavior is undefined and in your case you have a segmentation fault because you write out of the array.



        In fact you do not need that variable :



        #include <stdio.h>

        #define MAXLEN 10

        int get_line (char s[] , int lim )

        int c, i = 0;

        lim -= 1;

        while ((c=getchar()) != EOF)

        if (i < lim)

        s[i] = c;


        i += 1;

        if (c == 'n')
        break;


        s[(i < lim) ? i : lim] = '';

        return i;


        int main ()

        int len;
        char line[MAXLEN];

        while ((len = get_line(line,MAXLEN)) > 0)

        /*if (len>MAXLEN)*/

        printf("%d '%s'n", len, line);



        return 0;



        I changed the value of MAXLEN to easily test the case the input string is greater, and also the printf to see the result in all cases



        Compilation and execution :



        pi@raspberrypi:/tmp $ gcc -pedantic -Wextra -Wall g.c
        pi@raspberrypi:/tmp $ ./a.out
        aze
        4 'aze
        '
        123456789
        10 '123456789'
        12345678
        9 '12345678
        '
        1234567890
        11 '123456789'
        ^C


        To test the EOF case :



        pi@raspberrypi:/tmp $ echo aze | ./a.out
        4 'aze
        '
        pi@raspberrypi:/tmp $





        share|improve this answer































          0
















          As said in a remark your variable j is non initialized, so when you set s[j] the behavior is undefined and in your case you have a segmentation fault because you write out of the array.



          In fact you do not need that variable :



          #include <stdio.h>

          #define MAXLEN 10

          int get_line (char s[] , int lim )

          int c, i = 0;

          lim -= 1;

          while ((c=getchar()) != EOF)

          if (i < lim)

          s[i] = c;


          i += 1;

          if (c == 'n')
          break;


          s[(i < lim) ? i : lim] = '';

          return i;


          int main ()

          int len;
          char line[MAXLEN];

          while ((len = get_line(line,MAXLEN)) > 0)

          /*if (len>MAXLEN)*/

          printf("%d '%s'n", len, line);



          return 0;



          I changed the value of MAXLEN to easily test the case the input string is greater, and also the printf to see the result in all cases



          Compilation and execution :



          pi@raspberrypi:/tmp $ gcc -pedantic -Wextra -Wall g.c
          pi@raspberrypi:/tmp $ ./a.out
          aze
          4 'aze
          '
          123456789
          10 '123456789'
          12345678
          9 '12345678
          '
          1234567890
          11 '123456789'
          ^C


          To test the EOF case :



          pi@raspberrypi:/tmp $ echo aze | ./a.out
          4 'aze
          '
          pi@raspberrypi:/tmp $





          share|improve this answer





























            0














            0










            0









            As said in a remark your variable j is non initialized, so when you set s[j] the behavior is undefined and in your case you have a segmentation fault because you write out of the array.



            In fact you do not need that variable :



            #include <stdio.h>

            #define MAXLEN 10

            int get_line (char s[] , int lim )

            int c, i = 0;

            lim -= 1;

            while ((c=getchar()) != EOF)

            if (i < lim)

            s[i] = c;


            i += 1;

            if (c == 'n')
            break;


            s[(i < lim) ? i : lim] = '';

            return i;


            int main ()

            int len;
            char line[MAXLEN];

            while ((len = get_line(line,MAXLEN)) > 0)

            /*if (len>MAXLEN)*/

            printf("%d '%s'n", len, line);



            return 0;



            I changed the value of MAXLEN to easily test the case the input string is greater, and also the printf to see the result in all cases



            Compilation and execution :



            pi@raspberrypi:/tmp $ gcc -pedantic -Wextra -Wall g.c
            pi@raspberrypi:/tmp $ ./a.out
            aze
            4 'aze
            '
            123456789
            10 '123456789'
            12345678
            9 '12345678
            '
            1234567890
            11 '123456789'
            ^C


            To test the EOF case :



            pi@raspberrypi:/tmp $ echo aze | ./a.out
            4 'aze
            '
            pi@raspberrypi:/tmp $





            share|improve this answer















            As said in a remark your variable j is non initialized, so when you set s[j] the behavior is undefined and in your case you have a segmentation fault because you write out of the array.



            In fact you do not need that variable :



            #include <stdio.h>

            #define MAXLEN 10

            int get_line (char s[] , int lim )

            int c, i = 0;

            lim -= 1;

            while ((c=getchar()) != EOF)

            if (i < lim)

            s[i] = c;


            i += 1;

            if (c == 'n')
            break;


            s[(i < lim) ? i : lim] = '';

            return i;


            int main ()

            int len;
            char line[MAXLEN];

            while ((len = get_line(line,MAXLEN)) > 0)

            /*if (len>MAXLEN)*/

            printf("%d '%s'n", len, line);



            return 0;



            I changed the value of MAXLEN to easily test the case the input string is greater, and also the printf to see the result in all cases



            Compilation and execution :



            pi@raspberrypi:/tmp $ gcc -pedantic -Wextra -Wall g.c
            pi@raspberrypi:/tmp $ ./a.out
            aze
            4 'aze
            '
            123456789
            10 '123456789'
            12345678
            9 '12345678
            '
            1234567890
            11 '123456789'
            ^C


            To test the EOF case :



            pi@raspberrypi:/tmp $ echo aze | ./a.out
            4 'aze
            '
            pi@raspberrypi:/tmp $






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Mar 28 at 17:29

























            answered Mar 28 at 17:21









            brunobruno

            19.3k3 gold badges16 silver badges28 bronze badges




            19.3k3 gold badges16 silver badges28 bronze badges


























                0
















                You should turn on warnings for your compiler. It would show you that j is uninitialized:



                /home/user/main.c:11:15: warning: variable 'j' is uninitialized when used here [-Wuninitialized]
                s[j]=c;
                ^
                /home/user/main.c:6:14: note: initialize the variable 'j' to silence this warning
                int c,i,j;
                ^
                = 0





                share|improve this answer





























                  0
















                  You should turn on warnings for your compiler. It would show you that j is uninitialized:



                  /home/user/main.c:11:15: warning: variable 'j' is uninitialized when used here [-Wuninitialized]
                  s[j]=c;
                  ^
                  /home/user/main.c:6:14: note: initialize the variable 'j' to silence this warning
                  int c,i,j;
                  ^
                  = 0





                  share|improve this answer



























                    0














                    0










                    0









                    You should turn on warnings for your compiler. It would show you that j is uninitialized:



                    /home/user/main.c:11:15: warning: variable 'j' is uninitialized when used here [-Wuninitialized]
                    s[j]=c;
                    ^
                    /home/user/main.c:6:14: note: initialize the variable 'j' to silence this warning
                    int c,i,j;
                    ^
                    = 0





                    share|improve this answer













                    You should turn on warnings for your compiler. It would show you that j is uninitialized:



                    /home/user/main.c:11:15: warning: variable 'j' is uninitialized when used here [-Wuninitialized]
                    s[j]=c;
                    ^
                    /home/user/main.c:6:14: note: initialize the variable 'j' to silence this warning
                    int c,i,j;
                    ^
                    = 0






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Mar 28 at 17:21









                    David CullenDavid Cullen

                    7,6752 gold badges27 silver badges47 bronze badges




                    7,6752 gold badges27 silver badges47 bronze badges
























                        0
















                        the following proposed code:



                        1. cleanly compiles

                        2. performs the desired functionality

                        3. eliminates confusion in the code layout

                        4. eliminates unused #define statements

                        5. eliminates unneeded variables

                        And now, the proposed code:



                        #include <stdio.h>


                        #define MAXLEN 1000


                        int get_line (char s[] , int lim )

                        int i;

                        for ( i=0; i<(lim - 2); i++ )

                        int c = getchar();

                        if( c == EOF )

                        break;


                        s[i] = (char)c;

                        if( c =='n' )

                        break;



                        s[i] = '';

                        return i;



                        int main ( void )

                        int len;
                        char line[MAXLEN];
                        while ((len=get_line(line,MAXLEN))>0)

                        printf("%s",line);


                        return 0;






                        share|improve this answer





























                          0
















                          the following proposed code:



                          1. cleanly compiles

                          2. performs the desired functionality

                          3. eliminates confusion in the code layout

                          4. eliminates unused #define statements

                          5. eliminates unneeded variables

                          And now, the proposed code:



                          #include <stdio.h>


                          #define MAXLEN 1000


                          int get_line (char s[] , int lim )

                          int i;

                          for ( i=0; i<(lim - 2); i++ )

                          int c = getchar();

                          if( c == EOF )

                          break;


                          s[i] = (char)c;

                          if( c =='n' )

                          break;



                          s[i] = '';

                          return i;



                          int main ( void )

                          int len;
                          char line[MAXLEN];
                          while ((len=get_line(line,MAXLEN))>0)

                          printf("%s",line);


                          return 0;






                          share|improve this answer



























                            0














                            0










                            0









                            the following proposed code:



                            1. cleanly compiles

                            2. performs the desired functionality

                            3. eliminates confusion in the code layout

                            4. eliminates unused #define statements

                            5. eliminates unneeded variables

                            And now, the proposed code:



                            #include <stdio.h>


                            #define MAXLEN 1000


                            int get_line (char s[] , int lim )

                            int i;

                            for ( i=0; i<(lim - 2); i++ )

                            int c = getchar();

                            if( c == EOF )

                            break;


                            s[i] = (char)c;

                            if( c =='n' )

                            break;



                            s[i] = '';

                            return i;



                            int main ( void )

                            int len;
                            char line[MAXLEN];
                            while ((len=get_line(line,MAXLEN))>0)

                            printf("%s",line);


                            return 0;






                            share|improve this answer













                            the following proposed code:



                            1. cleanly compiles

                            2. performs the desired functionality

                            3. eliminates confusion in the code layout

                            4. eliminates unused #define statements

                            5. eliminates unneeded variables

                            And now, the proposed code:



                            #include <stdio.h>


                            #define MAXLEN 1000


                            int get_line (char s[] , int lim )

                            int i;

                            for ( i=0; i<(lim - 2); i++ )

                            int c = getchar();

                            if( c == EOF )

                            break;


                            s[i] = (char)c;

                            if( c =='n' )

                            break;



                            s[i] = '';

                            return i;



                            int main ( void )

                            int len;
                            char line[MAXLEN];
                            while ((len=get_line(line,MAXLEN))>0)

                            printf("%s",line);


                            return 0;







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Mar 28 at 18:10









                            user3629249user3629249

                            12.2k1 gold badge11 silver badges16 bronze badges




                            12.2k1 gold badge11 silver badges16 bronze badges
















                                Popular posts from this blog

                                Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                                Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                                Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript