Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
Here is all of my code:
#include <fstream>
#include <vector>
#include <algorithm>
#include <set>
#include <iostream>
using namespace std;
int main(){
ifstream fin("cereal.in");
ofstream fout("cereal.out");
int n, m, f, s;
cin >> n >> m;
int c1[n];
int c2[n];
for(int i = 0; i < n; i++){
cin >> c1[i] >> c2[i];
c1[i]--;
c2[i]--;
}
vector<int> fm(m, -1);
set<int> fs;
vector<int> ans;
for(int i = n-1; i >= 0; i++){
if(fs.find(c1[i]) == fs.end()){
fs.insert(c1[i]);
}else{
if(fs.find(fm[c1[i]]) == fs.end()){
fs.insert(fm[c1[i]]);
}
}
fm[c1[i]] = c2[i];
ans.push_back(fs.size());
}
for(int i = 0; i < n; i++){
cout << ans[i] << endl;
}
}
In the first for loop when i = n-1, I'm getting a Segmentation fault: 11. I used a cout call after the cin in that for loop to find this information.
What is the reason for this? It was working fine a couple of minutes ago and I didn't even touch this part of the code and it stopped working. I'm using VS Code, but even when I use an online compiler it doesn't work. Here is the input I am giving:
4 2
1 2
1 2
1 2
1 2
Please help me. This isn't the first time I'm having a problem like this.
for(int i = n-1; i >= 0; i++){ // <------
if(fs.find(c1[i]) == fs.end()){
fs.insert(c1[i]);
}else{
if(fs.find(fm[c1[i]]) == fs.end()){
fs.insert(fm[c1[i]]);
}
}
fm[c1[i]] = c2[i];
ans.push_back(fs.size());
}
Change to i--. You're accessing c1[i] with out of bound index.
You have an array index out-of-bounds bug, causing the program to access data where it shouldn't.
for(int i = n-1; i >= 0; i++){
In the above statement the integer i wraps around until it overflows, thus indexing c1 into forbidden regions of memory.
The OS is alerted of the memory violation and killed the misbehaving program. When this happens the program is said to encounter a segmentation fault.
Related
This question already has answers here:
Uninitialized variable behaviour in C++
(4 answers)
Closed 1 year ago.
Please help me with this strange problem where the input value is given as 4 (i.e. n = 4) and after a for loop the same value is getting displayed as 2, but why? It was not used anywhere (AFAIK) and it shouldn't get changed (I have no clue about it).
The original problem on HackerRank.
MY CODE >>
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int n;
int arr[n];
cin >> n; // input given from stdin is 4
cout << n << "\n"; // outputs 4
for(int i=0; i<n; i++){
scanf("%d",&arr[i]);
}
cout << n << "\n"; // outputs 2 (but why?)
for(int i=n-1; i>=0; i--){
printf("%d ",arr[i]); // if it were n = 4, then i will go from 3 to 0, but it goes from 1 to 0 (as n=2, but again, why?)
}
return 0;
}
Thank you for any help!
int n;
int arr[n]; // <<<<<< I magically know what n will be after the user types it!
cin >> n; // input given from stdin is 4
First of all, that's not even legal in C++. As a gcc extension to support C style VLA's, what is the value of n when the array declaration is seen? You have not read it yet!!
Instead, use:
int n;
cin >> n;
std::vector arr(n);
Although this still is not the "C++ way" as you are pre-defining the entire collection and then assigning to each element in turn; as opposed to adding each element to the collection. This is not a big deal with an int but more generally you don't want dead unused items in a collection; rather they simply don't exist at all.
std::vector arr; // don't pre-allocate any size
for(int i=0; i<n; i++){
int val;
scanf("%d",&val); //<<< uhhhhhh. you know about `cin` why this?
arr.push_back(val);
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I tried sorting this array in ascending order and I got reply saying RUNTIME error(SIGSEGV). Can Anybody explain ?
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n, arr[1000];
cin >> n;
for (int i = 0; i < n; i++)
scanf("%d", &arr[i]);
sort(arr, arr + n);
for (int i = 0; i < n; i++)
cout << arr[i] << endl;
return 0;
}
When you get input from a user that is used to populate an array, you should always validate the input before using it. If the user wants to enter 1001 numbers or more, or even a negative amount, you will access the array out of bounds and your program has undefined behaviour.
A better alternative is to use a std::vector to allocate just as much space as you need. One possible solution:
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
size_t n;
if(std::cin >> n) { // check that the user actually entered a number
std::vector<int> arr(n); // create a vector with n elements
for(size_t i=0; i<n; ++i)
std::cin >> arr[i];
std::sort(std::begin(arr), std::end(arr));
for(size_t i=0; i<n; ++i)
std::cout << arr[i] << '\n';
}
}
You're allocating space for only 1000 elements, thereby causing segmentation fault if n is more than that, because scanf will try to put 1001-th element in memory location un-allocated to arr. Since you're using C++, Please use std:vector to avoid such pitfalls.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
so I have this code that with a function is supposed to take all the numbers in a 2D array and print them to the second power but my code keeps throwing segmentation fault and i don't know why
#include <bits/stdc++.h>
using namespace std;
void er(int arr[][100000000], int, int);
int main()
{
int n, m;
cin >> n >> m;
int arr[n][100000000];
er(arr, n, m);
return 0;
}
void er(int arr[][100000000], int n, int m)
{
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> arr[i][j];
arr[i][j] *= arr[i][j];
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout << arr[i][j];
}
}
}
Using
int arr[n][100000000];
is problematic on two accounts.
VLAs are not standard C++. It is supported by some compilers as an extension.
The size 100000000 is too large for a variable on the stack. Changing that to 100 and making sure that m is less than or equal to 100 will most likely work as long as your compiler supports VLAs.
A better alternative would be to use std::vector.
int n, m;
cin >> n >> m;
std::vector<std::vector<int>> arr(n, std::vector<int>(m));
Of course, that will require you to change the function er accordingly.
In addition, please don't use
#include <bits/stdc++.h>
See Why should I not #include <bits/stdc++.h>? for further details.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am trying to solve a CodeChef problem. Whenever I run it I get a segmentation fault. This is the link to the problem: Malvika is peculiar about color of balloons
Here is my code :
#include<iostream>
#include<cstring>
#include<algorithm>
int main(){
std::string balloonColors;
size_t numberOfAmber;
size_t numberOfBrass;
int t;
int results[t];
std::cin >> t;
for (int i = 0; i < t; i++){
int result = 0;
std::cin >> balloonColors;
numberOfAmber = std::count(balloonColors.begin(), balloonColors.end(), 'a');
numberOfBrass = std::count(balloonColors.begin(), balloonColors.end(), 'b');
if (numberOfAmber == 0 || numberOfBrass == 0){
result = 0;
}
if (numberOfAmber <= numberOfBrass){
result = (int)numberOfAmber;
}
else {
result = (int)numberOfBrass;
}
results[i] = result;
}
for (int x = 0; x < t; x++){
std::cout << results[x] << std::endl;
}
}
These lines is the problem:
int t;
int results[t];
You declare results using the uninitialized variable t. An uninitialized variable have an indeterminate value, and using it before initialization leads to undefined behavior.
You should use a std::vector here, and set its size once you get the actual size from the user:
int t;
std::vector<int> results;
std::cin >> t;
results.resize(t);
Arrays in C++ need to have a fixed size. You defined results with size t, which isn't fixed.
To use dynamic size, use a std::vector instead:
#include <vector>
...
int t;
std::cin >> t;
std::vector<int> results (t);
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I'm new to C++ and trying to populate and output a 12x12 array using pseudo-random numbers.
Can anyone see where the code is failing?
Code:
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <random>
using namespace std;
const int i = 12;
const int j = 12;
int myArray [i] [j] = {0};
void generateArray();
int main()
{
generateArray();
return 0;
}
void generateArray()
{
srand(1234);
for(int i=0; i < 12; i++);
{
for(int j=0; j < 12; j++);
{
myArray[i][j]= rand();
}
cout << myArray[i][j] << " ";
}
}
Thanks,
Ryan
Your for loops have semicolons after their closing parentheses.
This will effectively treat the for loop as an empty body (and just ignore the loop in general).
Remove them and the code should execute.
Your for loop should look as below.
for(int i=0; i < 12; ++i)
{
for(int j=0; j < 12; ++j)
{
myArray[i][j]= rand();
cout << myArray[i][j] << " ";
}
cout << endl;
}