Cannot find bounds of current function while debugging - c++

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.

Related

Debug Assertion Fail (vector subscript out of range)

I found that my result.push_back(make_pair(a[i], b[j]));, which
causing this error but i dont know why (i don't even access vector<pair<int,int>>result;)
#include<iostream>
#include<vector>
#include<algorithm>
#include<math.h>
#include<utility>
using namespace std;
void input(int n,vector<int>&a) {
int temps;
for (int i = 0; i < n; i++) {
cin >> temps;
a.push_back(temps);
}
}
int main() {
//input
long n, m;
cin >> n; //6
vector<int>a, b;
input(n, a); //{2 5 4 1 7 5}
cin >> m; //7
input(m, b); //{2 3 1 3 2 4 6}
//algorithm
long max = *max_element(a.begin(), a.end()) + *max_element(b.begin(), b.end());
long min = *min_element(a.begin(), a.end()) + *min_element(b.begin(), b.end());
vector<pair<int, int>>result;
int possible = max, plate = 0;
for (int check = max; check >= min; check--) {
int j = 0, i = 0, plate2 = 0;
for (; i < a.size(); i++) {
if (a[i] >= check) {}
else {
if (j > b.size() - 1) { break; }
if (a[i] + b[j] >= check) {
j++; plate2++;
result.push_back(make_pair(a[i], b[j]));
}
else {
i--; j++;
}
}
}
if (i > a.size() - 1) { possible = check; plate = plate2; break; }
}
cout << possible << " " << plate << endl; //5 3
return 0;
}
if you remove the line result.push_back(make_pair(a[i],b[j]);, there is no error message anymore, so i think i'm not access wrong a[i] and b[j] elements
if (j > b.size() - 1) { break; } //(1)
if (a[i] + b[j] >= check) { //(2)
j++; plate2++; // HERE IS YOUR PROBLEM (3)
result.push_back(make_pair(a[i], b[j])); //(4)
Assume that j == b.size()-1 at the beginning. The if (j > b.size() - 1) clause is false, so the loop does not break. It continues with (2), which is okay. In (3) you add +1 to j, so now j == b.size(). In (4) you try to access b[j], which is now b[b.size()], which is invalid, as indizes start at 0.
IOW: You tried to assure that j never points beyond the number of valid elements, but then you increment j after that test and access invalid memory.

Cygwin creates stackdump file, takes long to compile (C++)

I am trying to compile the following problem (downloads.cpp) in C++ using Cygwin:
#include <fstream>
#include <algorithm>
#include <cstdio>
#include <climits>
using namespace std;
int main() {
int N, ft = 0, bw = 0;
ifstream input;
input.open("downloads.in");
input >> N;
pair <int, int> S_T[N+1];
S_T[N+1].first = INT_MAX;
S_T[N+1].second = INT_MAX;
int FT[N];
fill(FT, FT + sizeof(FT), 0);
for (int i = 0; i < N; ++i) {
input >> S_T[i].first >> S_T[i].second;
}
input.close();
sort(S_T, S_T + N);
for (int i = 0; i < N; ++i)
{
if (S_T[i+1].second != 0) {
for (int j = i+1; j < N; ++j) {
S_T[j].second -= S_T[i].second;
}
S_T[i+1].first += S_T[i].first;
S_T[i+1].second *= S_T[i-1].second / S_T[i].second;
} else if (S_T[i+1].first != INT_MAX && S_T[i+1].second != INT_MAX) {
S_T[i+1].first += S_T[i].first;
S_T[i+1].second *= S_T[i-1].second / S_T[i].second;
}
FT[i] = S_T[i].second;
}
for (int i = 0; i < N; ++i)
ft += FT[i];
ofstream output;
output.open("downloads.out");
output << ft << endl;
output.close();
return 0;
}
And I get the following error (with varying 5-digit numbers after "downloads"):
0 [main] downloads 76188 cygwin_exception::open_stackdumpfile: Dumping stack trace to downloads.exe.stackdump
[Finished in 6.5s]
Additionally, the program takes significantly longer than the usual one second to compile, and Cygwin seems to create a .exe.stackdump file with the same name as my program in the same file.
I have already checked for Cygwin opening background processes, and have found none. Anyone know why this could be happening?

LRU c++ program

I've been working on a program in one of my college classes. I have been having trouble with the implementation of my LRU code as it is not displaying any errors or anything, but compiles. There are two parts. The main that we input the values into, which we then specify which algorithm we want to use to find page faults. I know the main works, along with the FIFO algorithm, but I'm not getting anything with my LRU code (It compiles and "runs" but displays nothing as if I did not click to use the algorithm). Can anyone help me figure out what is wrong?
main.cpp
#include <iostream>
#include <string>
//#include "fifo.cpp"
#include "lru.cpp"
//#include "optimal.cpp"
using namespace std;
int main() {
// List of different variables
string pagestring;
int fs,pn[50], n;
// Prompt for page references
cout<<"Virtual Memory Simulation\nBy blah\n----------\nEnter the number of pages : " << endl;
cin >> n;
cout<<"\n-------------\nPlease enter a list of page numbers separated by commas.\n"<< endl;
cin>>pagestring;
// algorithm to use
char algo;
while (true) {
// Prompt algorithm to use
cout<<"----------\nPlease select an algorithm to use.\n\n1: First-In-First-Out (FIFO)\n2: Least-Recently-Used (LRU)\n3: Optimal\n0: Quit\n"<<endl;
cin>>algo;
if (algo == '1') {
//fifo(pagestring);
}
else if (algo == '2'){
LRU_Execute(pagestring, n);
}
else if (algo == '3'){
cout<<"Optimal Not yet coded"<<endl;
}
else if (algo == '0'){
break;
}
else {
cout<<"Invalid choice. Please try again."<<endl;
}
}
cout<<"Goodbye!!"<<endl;
};
LRU.cpp
#include <iostream>
#include <string>
using namespace std;
class pra
{
int fs,z;
int frame[50], frame1[50][2], pn[50], n, cnt, p, x;
public:
pra();
void init(string pagestring);
void getdata(string pagestring, int n);
void lru(int* pn, int n, string pagestring);
};
pra::pra()
{
int i;
for (i = 0; i < fs; i++)
{
frame[i] = -1;
}
for (i = 0; i < fs; i++)
{
frame1[i][0] = -1;
frame1[i][1] = 0;
}
p = 0;
cnt = 0;
}
void pra::init(string pagestring)
{
int i;
for (i = 0; i < fs; i++)
{
frame[i] = -1;
}
for (i = 0; i < fs; i++)
{
frame1[i][0] = -1;
frame1[i][1] = 0;
}
p = 0;
cnt = 0;
}
void pra::getdata(string pagestring, int n)
{
fs=3;
// index to loop through input string
int i = 0;
// current input string character
char z = pagestring[i];
int x = 0;
//cout << "\nEnter the page numbers : ";
while (z != '\0'){
// skip over commas and spaces
if (!(z == ',')) {
pn[x] = z;
x++;
// cout<<pn[x]<<"-This is pn[x]\n";
}
z = pagestring[++i];
}
//cout<<pn[x]<<"-This is pn[x] AGAIN\n";
this->lru(pn, n, pagestring);
}
void pra::lru(int* pn, int n, string pagestring)
{
init(pagestring);
int ind = 0, fault = 0, pi = 0, j, fn;
char i, z;
p = 0;
cnt = 0;
int min;
cout<<n<<"---"<<i<<" - "<<j<<" - "<<" - "<<fn<<" - "<<z;
for (i = 0; i < fs; i++)
{
frame1[i][0] = -1;
frame1[i][1] = 0;
}
pi = 0;
for (i = 0; i < n; i++)
{
j = 0;
if (ind > fs - 1)
ind = 0;
fault = 1;
min = 999;
while (j < fs)
{
if (frame1[j][0] = pn[pi])
{
fault = 0;
p++;
frame1[j][1] = p;
goto l2;
}
if (frame1[j][1] < min)
{
min = frame1[j][1];
fn = j;
}
j++;
}
j = 0;
while (j < fs)
{
if (frame1[j][0] = -1)
{
fault = 1;
fn = j;
goto l2;
}
j++;
}
ind++;
l2:
if (fault == 1)
{
p++;
frame1[fn][0] = pn[pi];
frame1[fn][1] = p;
cnt++;
}
cout << "\nElement: " << pn[pi];
pi++;
for (z = 0; z < fs; z++)
{
cout << "\t" << frame1[z][0];
}
if (fault == 1)
cout << "\t**Page Fault**";
else
cout << "\t--No Page Fault--";
}
cout << "\nTotal number of page faults: " << cnt;
cout << "\n";
}
void LRU_Execute(string pagestring, int n)
{
pra p;
int j, fault = 0, i, pi, z, fn, ind = 0, ans, ch;
p.getdata(pagestring, n);
//p.lru();
while (ans == 1);
//return 1;
}

Runtime error in Ideone but works fine in codeblocks

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.

Why does the name of a source file affect compilation?

I came across something really weird when I wrote a little lotto program in C++ called "lotto.cpp". Everything was fine until I wrote the write-to-file for my program. When I compiled, it showed me the following error:
ld: can't open output file for writing: lotto, errno=21 for architecture x86_64
collect2: ld returned 1 exit status
By coincidence, I changed the name of my program to "1.cpp", and all of a sudden it compiled without problems. It also worked when I changed the name to "test.cpp".
I am really curious as to why this happened. Any Ideas?
This happened on a MacBook Pro.
If you want the code as well, just let me know!
I know some people asked for the code. Here it is:
#include <iostream>
#include <fstream>
using namespace std;
const int NED = 10;
const int VIKING = 6;
const int NORMAL = 7;
const int MAX = 10;
void quickSort(int arr[], int left, int right);
int checkDuplicates(int arr[], int length);
int main (int argc, const char *argv[])
{
int i, j, k, ans;
char ans2;
int lottoNumbers[MAX];
ofstream out("Lotto.txt", ios::out | ios::app);
srand((unsigned)time(NULL));
do
{
do
{
cout << "\n\nDo you want to play Viking Lotto (press 6), or normal Lotto (press 7): ";
cin >> ans;
}while(ans != VIKING && ans != normal);
(ans == VIKING) ? cout << "\nViking Lotto:\n" : cout << "\n\nnormal Lotto:\n";
(ans == VIKING) ? out << "\nViking Lotto:\n" : out << "\n\nnormal Lotto:\n";
for (i = 0; i < NED; i++) //10 rows
{
for (j = 0; j < ans; j++) //6 or 7 columns
{
(ans == VIKING) ? lottoNumbers[j] = (rand() % 48) + 1 : lottoNumbers[j] = (rand() % 34) + 1;
}
if(checkDuplicates(lottoNumbers, ans) != -1)
{
for(k = 0; k < ans; k++)
{
while(checkDuplicates(lottoNumbers, ans) == lottoNumbers[k])
{
(ans == VIKING) ? lottoNumbers[k] = (rand() % 48) + 1 : lottoNumbers[k] = (rand() % 34) + 1;
}
}
}
quickSort(lottoNumbers, 0, ans - 1);
cout << '\n';
for(j = 0; j < ans; j++)
{
cout << lottoNumbers[j] << '\t';
out << lottoNumbers[j] << '\t';
}
out << '\n';
}
cout << "\n\n";
cout <<"Another lottery ticket (Y/N) ";
cin >> ans2;
}while(ans2 == 'j' || ans2 == 'J');
cout << "\n\nLOTTO NUMBERS WAS WRITTEN TO FILE...\n\n";
return 0;
}
void quickSort(int arr[], int left, int right)
{
int i = left, j = right;
int tmp;
int mid = arr[(left + right) / 2];
while (i <= j)
{
while (arr[i] < mid) i++;
while (arr[j] > mid) j--;
if (i <= j)
{
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
i++;
j--;
}
};
if (left < j) quickSort(arr, left, j);
if (i < right) quickSort(arr, i, right);
}
int checkDuplicates(int arr[], int length)
{
for(int i = 0; i < length; i++)
{
for(int j = i + 1; j < length; j++)
{
if(arr[i] == arr[j]) return arr[j];
}
}
return -1;
}
Error number 21 (on MacOS X 10.7.2) is EISDIR: Is a directory.
The name lotto seems to be a directory, not a file.
This is a linker error that states that we cannot write to the 'lotto' file on your computer while compiling. My guess is that either your program is still running, or you accidentally created a directory called 'lotto'. It's possible that your write-to-file function is keeping the application running, or itself tried to create a lotto directory.
Yeah I ran into this problem by copying some of my visual studio code to my mac. It seems Visual Studio likes to create folders inside your project with your executable name which causes this!
FWIW I got this error when I was trying to write my output file into a directory that hadn't been created yet, i.e. bin/myprogram.
Once I created the bin directory everything was fine; I didn't have to re-name anything. GCC seems to create the directory if it doesn't exist, whereas clang doesn't (at least thats as near as I can tell).