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;
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
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
add a comment
|
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
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
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
add a comment
|
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
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
c
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
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
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
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
add a comment
|
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
add a comment
|
3 Answers
3
active
oldest
votes
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 $
add a comment
|
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
add a comment
|
the following proposed code:
- cleanly compiles
- performs the desired functionality
- eliminates confusion in the code layout
- eliminates unused
#define
statements - 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;
add a comment
|
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 $
add a comment
|
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 $
add a comment
|
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 $
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 $
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
add a comment
|
add a comment
|
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
add a comment
|
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
add a comment
|
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
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
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
add a comment
|
add a comment
|
the following proposed code:
- cleanly compiles
- performs the desired functionality
- eliminates confusion in the code layout
- eliminates unused
#define
statements - 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;
add a comment
|
the following proposed code:
- cleanly compiles
- performs the desired functionality
- eliminates confusion in the code layout
- eliminates unused
#define
statements - 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;
add a comment
|
the following proposed code:
- cleanly compiles
- performs the desired functionality
- eliminates confusion in the code layout
- eliminates unused
#define
statements - 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;
the following proposed code:
- cleanly compiles
- performs the desired functionality
- eliminates confusion in the code layout
- eliminates unused
#define
statements - 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;
answered Mar 28 at 18:10
user3629249user3629249
12.2k1 gold badge11 silver badges16 bronze badges
12.2k1 gold badge11 silver badges16 bronze badges
add a comment
|
add a comment
|
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