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

                                SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

                                은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현