Code works in Geany IDE, but segmentation fault in HackerRanks. What's the technical reason for this?
It's a simple program that initializes 3 data types and takes 3 inputs, an int, a double, and a string. Then perform addition on the numbers and prints out the "concatenated" string (doesn't actually have to be concatenated). It works in Geany IDE running on a VM of Ubuntu 16.04, but gets a segmentation fault in HackerRank. Why would this occur?
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int i = 4;
double d = 4.0;
char s = "HackerRank ";
int j, cnt = 0;
double e;
char c[100];
char ch;
scanf(" %d", &j);
scanf(" %lf", &e);
ch = getchar();
while ((ch = getchar()) != 'n'){
c[cnt++] = ch;
}
c[cnt] = '';
printf("%dn", i+j);
printf("%.1lfn", d+e);
printf("%s%sn", s, c);
return 0;
}
c
|
show 7 more comments
It's a simple program that initializes 3 data types and takes 3 inputs, an int, a double, and a string. Then perform addition on the numbers and prints out the "concatenated" string (doesn't actually have to be concatenated). It works in Geany IDE running on a VM of Ubuntu 16.04, but gets a segmentation fault in HackerRank. Why would this occur?
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int i = 4;
double d = 4.0;
char s = "HackerRank ";
int j, cnt = 0;
double e;
char c[100];
char ch;
scanf(" %d", &j);
scanf(" %lf", &e);
ch = getchar();
while ((ch = getchar()) != 'n'){
c[cnt++] = ch;
}
c[cnt] = '';
printf("%dn", i+j);
printf("%.1lfn", d+e);
printf("%s%sn", s, c);
return 0;
}
c
1
I bet more than 100 chars in the input. And you write outside the bounds
– P__J__
Jan 1 at 20:03
1
Are you sure a newline will terminate the input? Check forEOF
as well, but you must changechar ch;
toint ch;
I would usewhile ((ch = getchar()) != 'n' && ch != EOF)
Without a newline, you'll break the array.
– Weather Vane
Jan 1 at 20:04
1
I mean "exceed the bounds of the arrayc
" since without a newline the loop continues until you break something.
– Weather Vane
Jan 1 at 20:09
2
Usually these challenge sites redirect a file to the input of your program. A text file does not have to end with a newline.
– Weather Vane
Jan 1 at 20:10
2
Is it trial and error to check how their files are formatted No, you should carefully read the assignment and implement exactly what it says. If it says your program should read a string, it should read a string. Not a string shorter than 100 characters. Not a string shorter than 1000000 characters. A string.
– n.m.
Jan 1 at 20:27
|
show 7 more comments
It's a simple program that initializes 3 data types and takes 3 inputs, an int, a double, and a string. Then perform addition on the numbers and prints out the "concatenated" string (doesn't actually have to be concatenated). It works in Geany IDE running on a VM of Ubuntu 16.04, but gets a segmentation fault in HackerRank. Why would this occur?
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int i = 4;
double d = 4.0;
char s = "HackerRank ";
int j, cnt = 0;
double e;
char c[100];
char ch;
scanf(" %d", &j);
scanf(" %lf", &e);
ch = getchar();
while ((ch = getchar()) != 'n'){
c[cnt++] = ch;
}
c[cnt] = '';
printf("%dn", i+j);
printf("%.1lfn", d+e);
printf("%s%sn", s, c);
return 0;
}
c
It's a simple program that initializes 3 data types and takes 3 inputs, an int, a double, and a string. Then perform addition on the numbers and prints out the "concatenated" string (doesn't actually have to be concatenated). It works in Geany IDE running on a VM of Ubuntu 16.04, but gets a segmentation fault in HackerRank. Why would this occur?
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int i = 4;
double d = 4.0;
char s = "HackerRank ";
int j, cnt = 0;
double e;
char c[100];
char ch;
scanf(" %d", &j);
scanf(" %lf", &e);
ch = getchar();
while ((ch = getchar()) != 'n'){
c[cnt++] = ch;
}
c[cnt] = '';
printf("%dn", i+j);
printf("%.1lfn", d+e);
printf("%s%sn", s, c);
return 0;
}
c
c
asked Jan 1 at 19:58
brunshtebrunshte
236
236
1
I bet more than 100 chars in the input. And you write outside the bounds
– P__J__
Jan 1 at 20:03
1
Are you sure a newline will terminate the input? Check forEOF
as well, but you must changechar ch;
toint ch;
I would usewhile ((ch = getchar()) != 'n' && ch != EOF)
Without a newline, you'll break the array.
– Weather Vane
Jan 1 at 20:04
1
I mean "exceed the bounds of the arrayc
" since without a newline the loop continues until you break something.
– Weather Vane
Jan 1 at 20:09
2
Usually these challenge sites redirect a file to the input of your program. A text file does not have to end with a newline.
– Weather Vane
Jan 1 at 20:10
2
Is it trial and error to check how their files are formatted No, you should carefully read the assignment and implement exactly what it says. If it says your program should read a string, it should read a string. Not a string shorter than 100 characters. Not a string shorter than 1000000 characters. A string.
– n.m.
Jan 1 at 20:27
|
show 7 more comments
1
I bet more than 100 chars in the input. And you write outside the bounds
– P__J__
Jan 1 at 20:03
1
Are you sure a newline will terminate the input? Check forEOF
as well, but you must changechar ch;
toint ch;
I would usewhile ((ch = getchar()) != 'n' && ch != EOF)
Without a newline, you'll break the array.
– Weather Vane
Jan 1 at 20:04
1
I mean "exceed the bounds of the arrayc
" since without a newline the loop continues until you break something.
– Weather Vane
Jan 1 at 20:09
2
Usually these challenge sites redirect a file to the input of your program. A text file does not have to end with a newline.
– Weather Vane
Jan 1 at 20:10
2
Is it trial and error to check how their files are formatted No, you should carefully read the assignment and implement exactly what it says. If it says your program should read a string, it should read a string. Not a string shorter than 100 characters. Not a string shorter than 1000000 characters. A string.
– n.m.
Jan 1 at 20:27
1
1
I bet more than 100 chars in the input. And you write outside the bounds
– P__J__
Jan 1 at 20:03
I bet more than 100 chars in the input. And you write outside the bounds
– P__J__
Jan 1 at 20:03
1
1
Are you sure a newline will terminate the input? Check for
EOF
as well, but you must change char ch;
to int ch;
I would use while ((ch = getchar()) != 'n' && ch != EOF)
Without a newline, you'll break the array.– Weather Vane
Jan 1 at 20:04
Are you sure a newline will terminate the input? Check for
EOF
as well, but you must change char ch;
to int ch;
I would use while ((ch = getchar()) != 'n' && ch != EOF)
Without a newline, you'll break the array.– Weather Vane
Jan 1 at 20:04
1
1
I mean "exceed the bounds of the array
c
" since without a newline the loop continues until you break something.– Weather Vane
Jan 1 at 20:09
I mean "exceed the bounds of the array
c
" since without a newline the loop continues until you break something.– Weather Vane
Jan 1 at 20:09
2
2
Usually these challenge sites redirect a file to the input of your program. A text file does not have to end with a newline.
– Weather Vane
Jan 1 at 20:10
Usually these challenge sites redirect a file to the input of your program. A text file does not have to end with a newline.
– Weather Vane
Jan 1 at 20:10
2
2
Is it trial and error to check how their files are formatted No, you should carefully read the assignment and implement exactly what it says. If it says your program should read a string, it should read a string. Not a string shorter than 100 characters. Not a string shorter than 1000000 characters. A string.
– n.m.
Jan 1 at 20:27
Is it trial and error to check how their files are formatted No, you should carefully read the assignment and implement exactly what it says. If it says your program should read a string, it should read a string. Not a string shorter than 100 characters. Not a string shorter than 1000000 characters. A string.
– n.m.
Jan 1 at 20:27
|
show 7 more comments
0
active
oldest
votes
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53998521%2fcode-works-in-geany-ide-but-segmentation-fault-in-hackerranks-whats-the-techn%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53998521%2fcode-works-in-geany-ide-but-segmentation-fault-in-hackerranks-whats-the-techn%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
1
I bet more than 100 chars in the input. And you write outside the bounds
– P__J__
Jan 1 at 20:03
1
Are you sure a newline will terminate the input? Check for
EOF
as well, but you must changechar ch;
toint ch;
I would usewhile ((ch = getchar()) != 'n' && ch != EOF)
Without a newline, you'll break the array.– Weather Vane
Jan 1 at 20:04
1
I mean "exceed the bounds of the array
c
" since without a newline the loop continues until you break something.– Weather Vane
Jan 1 at 20:09
2
Usually these challenge sites redirect a file to the input of your program. A text file does not have to end with a newline.
– Weather Vane
Jan 1 at 20:10
2
Is it trial and error to check how their files are formatted No, you should carefully read the assignment and implement exactly what it says. If it says your program should read a string, it should read a string. Not a string shorter than 100 characters. Not a string shorter than 1000000 characters. A string.
– n.m.
Jan 1 at 20:27