Runtime error in Ideone but works fine in codeblocks - c++

I tested this code on codeblocks and it is working fine but when i run this on Ideone i get runtime error and SIGSEGV error on OJ. I read online that SIGSEGV error is caused due to restricted memory access. But if it is doing so why isn't codeblocks complaining......
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
int t, i, j, k, x, temp;
int num1[10000], num2[10000];
char c;
bool f = true;
cin >> t;
while (t--)
{
for (i = 0; i < 10000; i++)
{
num1[i] = 0;
num2[i] = 0;
}
i = 0;
j = 0;
while (!isspace(c = getchar()))
num1[i++] = c - '0';
while (!isspace(c = getchar()))
num2[j++] = c - '0';
x = i > j ? i : j;
temp = 0;
for (k = 0; k < x; k++)
{
i = temp + num1[k] + num2[k];
if (f)
{
if (i % 10 != 0)
{
f = false;
cout << i % 10;
}
}
else cout << i % 10;
temp = i / 10;
}
if (temp != 0)
cout << temp;
cout << endl;
f = true;
}
return 0;
}

This:
while (!isspace(c = getchar()))
will keep going until it finds a space character. If there's no space after the final input value, then getchar() will return EOF, which you'll truncate to char and pass to isspace, giving undefined behaviour. The chances are that isspace will return false, and you'll loop forever, off the end of your fixed size array, until you reach unaddressable memory and cause a segmentation fault.
Always check input before using it. You'll also have big problems if there's no input, since you don't check whether t was successfully read.

Related

Cannot find bounds of current function while debugging

Hello guys can anyone tell me how do i fix this problem?When i debug in codeblocks and press the next line button it says "Cannot find bounds of current function".How do i fix this?here in the while loop i wanted to test out the debugger.
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define pb push_back
#define vint vector<int>
#define all(v) v.begin(), v.end()
int main()
{
int n, i, in, x, sum1 = 0, sum2, t = 0;
cin >> n;
vint a;
a.pb(0);
for (i = 0; i < n; i++) {
cin >> in;
a.pb(in);
sum1 = sum1 + a[i];
}
in = 1;
n = n + 1;
while (in != 5) { // i want to debug from here.
sum2 = sum1 + in;
for (i = 0; i < n + 1; i++) {
if (i == n + 1) {
i = -1;
continue;
}
sum2--;
if (sum2 == 0) {
break;
}
}
if (i != 0) {
t++;
}
in++;
}
cout << t << endl;
return 0;
}
That usually means that there is no debug info found. Do you try to debug a release project maybe? For code/symbols to be present during debugging, you need to use a debug build.

Process returned -1073741819 (0xC0000005) (why though??)

So i was solving my homework, and i did this piece of code which is supposed to find the biggest difference beetween two prime numbers in the interval of [a,b], and i got "Process returned -1073741819 (0xC0000005)"
#include <iostream>
#include <vector>
#include <bitset>
using namespace std;
bitset <10000000>v;
int main()
{
for (int i = 2; i < 10000000; i++)
{
if (v[i] == 0)
{
for (int j = i * i; j < 10000000; j += i)
v[j] = 1;
}
}
int n, a, b, maxi = 0, mini = 0, smax = 0;
cin >> a >> b;
int poz = a;
while (v[poz] == 1)
poz++;
int prev = poz;
poz++;
while (v[poz] == 1 && poz < b)
poz++;
if (poz == b && v[b] == 1)
{
cout << -1; return 0;
}
int next = poz;
poz++;
while (poz <= b)
{
if (next - prev > smax)
{
smax = next - prev;
maxi = next;
mini = prev;
}
if (v[poz] == 0)
{
prev = next;
next = poz;
}
poz++;
}
cout << mini << " " << maxi;
return 0;
}
i expected 43 with 47
In your initialisation loop when i is 46349, i*i is 2,148,229,801, this is larger than fits inside a signed 32-bit integer so evaluates to -2,146,737,495. v[j] then causes the crash.
You should either modify your code to use a larger data type or set the limit for i to sqrt(10000000) rather than 10000000.
I'm guessing that i*i overflows when i is large, leading to a negative value for j and an access violation on v[j]=1;.
You have a potential segmentation fault on line v[j]=1; where j may exceed 10000000.
Please check your boundaries.

2d vector c++ use in Longest Common Substring

I am working on codechef practice problem in which I have to find longest common substring. ( Its practice problem so don't down vote )
Following wiki and some resources online I got the algorithm
https://en.wikipedia.org/wiki/Longest_common_substring_problem
After understanding I wrote the algorithm in c++ , but its compiling but not running successfully . Throwing error while assigning value to vector matrix .
An invalid parameter was passed to a function that considers invalid parameters fatal.
1) Whats wrong with my LCSubstring function
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int max(int a, int b) {
return a > b ? a : b;
}
int LCSubString(string str1 , string str2) {
// create 2d matrix
vector<vector<int>> matrix;
int maxlength = 0;
for (int i = 0; i <= str1.length(); i++) {
for (int j = 0; j <= str2.length(); j++) {
if (i == 0 && j == 0) {
matrix[i][j] = 0;
continue;
}
if (str1[i - 1] == str2[j - 1]) {
matrix[i][j] = matrix[i - 1][j - 1] + 1;
maxlength = max(maxlength, matrix[i][j]);
}
else {
matrix[i][j] = 0;
}
}
}
return maxlength;
}
int main()
{
int t;
int count = 0;
string str;
int len;
cin >> t;
while (t--) {
cin>> str;
len = LCSubString(str, "chef");
if (len >= 2) {
count++;
}
}
cout << count << endl;
return 0;
}
You did not properly initialize std::vector<std::vector<int>> matrix. They do not automatically resize when you access them with [], you need to set the proper size beforehand, like this:
vector<vector<int>> matrix(str.length()+1);
for (int i = 0; i <= str1.length(); ++i)
matrix[i] = vector<int>(str2.length()+1, 0);
Additionally, you are not properly checking for i == 0 or j == 0. If only one of them is zero then the first if in your loop won't hit and you subsequently try to read string[-1]. I don't know whether it would be correct for your algorithm, but try using logical or (||) instead of and (&&).

C++ - Finding x Factor of a math expression

I'm trying to get the x factor of an input math expression, but it seems buggy.
Here is my code:
#include<iostream>
#include<math.h>
using namespace std;
int main(){
char a[100];
int res = 0; // final answer
int temp = 0; // factor of the x we are focused on - temporarily
int pn = 0; // power of 10 - used for converting digits to number
int conv; // used for conversion of characters to int
cout<< "Enter a: ";
cin>> a; //input expression
for(int i=0; i<100; i++){
// checking if the character is x - then get the factor
if(a[i]=='x'){
for(int j=1; j<=i+1; j++){
conv = a[i-j] - '0'; // conversion
if(conv>=0 && conv<=9){ // check if the character is a number
temp = temp + conv*pow(10, pn); // temporary factor - using power of 10 to convert digit to number
pn++; // increasing the power
}
else{
if(a[i-j]=='-'){
temp = -temp; // check if the sign is - or +
}
break;
}
if(i-j==0){
break;
}
}
res = res+temp; // adding the x factor to other ones
pn = 0;
temp = 0;
}
}
cout<< res;
return 0;
}
It doesn't work for some inputs, for example:
100x+3x gives 102 and 3x+3997x-4000x gives -1
But works for 130x and 150x!
Is there a problem with my code, or is there an easier way to do this?
I think you're not parsing the +. There may be some other problem I can't see in the original code, probably not. Here's the X-File:
int main(){
char a[100];
int res(0);
cout << "Enter a: ";
cin >> a;
for(int i = 0; i < 100; i++){
if(a[i] == 'x'){
int temp(0);
int pn(0);
for(int j = 1; j <= i; j++){
int conv = a[i - j] - '0';
if(conv >= 0 && conv <= 9){
temp += conv*pow(10, pn);
pn++;
} else if(a[i - j] == '-') {
temp = -temp;
break;
} else if(a[i - j] == '+') {
break;
} else {
// what to do...
}
}
res += temp;
}
}
cout << res;
return 0;
}

The 3n+1 solution overflows in C++ VS2010

I'm trying to solve the 3n+1 problem using VS2010 c++,in small inputs it works well,but when it reaches 113383 it overflows.
Here is the problem link.
This is the code I'm using to solve this problem :
#include <iostream>
using namespace std;
int main(void) {
while (!cin.eof()) {
int i, j, maxCycle = 0, tmaxCycle = 0;
cin >> i >> j;
for (int x = i; x <= j; x++) {
int n = x;
tmaxCycle = 0;
while (n != 1) {
if ((float)(n/2) != (n/2.0)) {
n = 3*n + 1;
}
else {
n /= 2;
}
tmaxCycle += 1;
if (n < 0) {
int blah = 0; //just for the breakpoint
}
}
tmaxCycle += 1;
if (tmaxCycle > maxCycle) {
maxCycle = tmaxCycle;
}
}
cout << i << "\t" << j << "\t" << maxCycle << endl;
}
system("pause");
}
I made a breakpoint at line 15,and in this point values overflows
n=-1812855948
Use 64-bit unsigned integers. If those overflow, use a bignum library like the GNU Multiple Precision Library. Bignums give you unlimited precision and size.
This
if((float)(n/2)!=(n/2.0))
produces incorrect results, long before int overflows. Change it to
if ( n & 1)