I am having a segmentation fault while running this code: http://ideone.com/yU80Bd
The problem is when I run it in GDB, the code runs fine and excellent. Why is this running in gdb with no segfault but running every other place with a segmentation fault?
Here is the problem I am trying to solve: http://www.codechef.com/DEC13/problems/CHODE
The problem is that your input includes characters that are not in the range [a-Z]. For example: ! That causes the vector to be accesed at invalid indexes.
You can check these things running your program with valgrind.
valgrind ./ideone < stdin
...
==2830== Invalid read of size 4
==2830== at 0x40111A: main (ideone.cpp:53)
...
==2830== Invalid write of size 4
==2830== at 0x401120: main (ideone.cpp:53)
The problem is in these lines:
for(int i=0;i<cipherText.size();++i)
{
char c = tolower(cipherText[i]);
++(cipherF[c-97].frequency);
}
c - 97 may be lower than 0.
You can check, for example:
for(int i=0;i<cipherText.size();++i)
{
char c = tolower(cipherText[i]);
if (c < 'a' || c > 'z') continue;
++(cipherF[c-97].frequency);
}
Related
I'm a beginner in c++ and I was trying to solve https://projecteuler.net/problem=9 . I wrote a code for it and it shows the error - Program received signal SIGSEGV, Segmentation fault.
In strcmp () (C:\Windows\syswow64\msvcrt.dll)
while debugging.
If I straightaway run the program, a dialog box appears that says "windows is checking for a solution."
I've tried not using the string function and instead of writing pytha(a,b,c)=="true" , I just wrote axa+bxb=c*c (I wrote * instead of x but here it is not showing * between the two a's so I am replacing it with x) and the code works perfectly fine. But the thing is why does it not work with the string function?
I do not see anything wrong with the code.
I've found plenty of similar questions-
1. https://www.codeproject.com/Questions/93770/what-is-this-means-Program-received-signal-SIGSEGV
According to this one, my program is referring to a memory location which it does not have access to. But I do not see anything that is restricting this code to access something.
Program received signal SIGSEGV, Segmentation fault error
3.Debug---Program received signal SIGSEGV, Segmentation fault
program received signal SIGSEGV, segmentation fault
"Program received signal SIGSEGV, Segmentation fault."
Program received signal SIGSEGV, Segmentation fault
Program received signal SIGSEGV, segmentation fault, Linked list program
None of them answer my query as I am not able to relate to the codes mentioned in them to my code.
The link numbered 5 mentions that probably the error is because of the large number of computations involved. Even I had that doubt for my code, but it works fine when I don't use the function "pytha". Also, I do not see the large number of steps involved related in any way to an error related to memory access.
Also, even if large number of steps are involved is the reason, the program should compile when given enough time. But it doesn't. It straightaway shows the error that "Windows is looking for a solution."
#include <cmath>
#include <iostream>
#include <string>
using namespace std;
string pytha(int a, int b, int c) {
if(a * a + b * b == c * c) return "true";
}
int main() {
for(int a = 1; a < 1000; a++) {
for(int b = 1; b < 1000; b++) {
for(int c = 1; c < 1000; c++) {
if(a + b + c == 1000) {
if(pytha(a, b, c) == "true")
cout << "a= " << a << " b= " << b << " c= " << c;
}
}
}
}
}
Please note that this code is a very inefficient one. The point is not to solve the question but to know why is the program not compiling.
pytha doesn't return a value on its all control flow paths.
Fix:
string pytha(int a, int b, int c)
{
if (a*a+b*b==c*c)
return "true";
return "";
}
Always compile your code with warnings enabled. For gcc and clang the compiler command line options are -Wall -Wextra -Werror.
You probably want to use bool type instead of string.
I received a segfault when running my code. The code contains a for loop and segmentation fault doesn't appear when I run the program for smaller iterations. It appears when I run the code for the larger loop.
I used a debugger to check where doe the problem occurs:
Program received signal SIGSEGV, Segmentation fault.
0x00000000004062d0 in std::vector<int, std::allocator<int> >::push_back (
this=0x64dc08, __x=#0x7fffffffd180: 32)
at /usr/include/c++/5/bits/stl_vector.h:915
915 if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)
What does it mean? Does anyone know? The code error is related to this part of the code:
int box_new = getBoxID(x[i],y[i],R0,nbox);
if(bx[i] != box_new)
{
vector<int>::iterator position = std::find(box_particles[bx[i]].begin(), box_particles[bx[i]].end(), i);
if (position != box_particles[bx[i]].end()) // .end() means the element was not found
box_particles[bx[i]].erase(position);
bx[i] = box_new;
box_particles[box_new].push_back(i);
}
Because value of box_new is out of bound, that is, it may be larger than or equals to the size of box_particles or less than 0. Then corruption occur and
segfault.
Please make sure box_new is in the range[0, box_particles.size()). Note that this range may change at each iteration.
Description
SFDD and Manager are classes that I created myself, following is my test file for testing class SFDD. (You can skip them and check the my test results)
#include <iostream>
#include <string>
#include "SFDD.h"
int main(int argc, char** argv) {
vector<int> vars_order;
int var_no = 18;
for (int i = 1; i <= var_no; ++i) vars_order.push_back(i);
Vtree* v = new Vtree(1, var_no*2-1, vars_order);
v->save_file_as_dot("vtree");
Manager m(v);
SFDD sfdd1 = m.sfddZero(); // create a SFDD representing Zero
SFDD sfdd2 = m.sfddOne();
SFDD sfdd3 = m.sfddVar(3);
SFDD sfdd4 = m.sfddVar(11);
SFDD sfdd6 = sfdd3.And(sfdd4, m, true);
sfdd6.save_file_as_dot("f=x11_and_x3"); // export sfdd6
cout << "Haha 1" << endl; // flag 1: for debugging
SFDD sfdd8 = sfdd4.Xor(sfdd6, m, true);
sfdd8.save_file_as_dot("f=x11_xor_(x11_and_x3)");
cout << "Haha 2" << endl; // flag 2: for debugging
sfdd3.Xor(sfdd8, m, true).save_file_as_dot("f=x3_xor_x11_xor_(x11_and_x3)");
cout << "Haha 3" << endl; // flag 3: for debugging
return 0;
}
After make and run. I got
Haha 1
Haha 2
Haha 3
which means it works. (execute to the end)
1. Segmentation fault 1
After I comment this line
sfdd6.save_file_as_dot("f=x11_and_x3"); // export sfdd6
I make and run again, I got
Haha 1
Segmentation fault (core dumped)
This comfuses me, because the function save_file_as_dot(string s) is just to export class SFDD to a dot file (dot is a language drawing simple graphs), I think it shouldn't be able to avoid a segmentation fault.
2. Segmentation fault 2
After I comment this line (uncomment above line this time)
SFDD sfdd1 = m.sfddZero(); // create a SFDD
I got
Haha 1
Haha 2
Segmentation fault (core dumped)
This comfuses me again, because last several lines don't use object sfdd1, why commenting this line avoids sementation fault?
Why do these segmentation faults appear? All I need are some clues or directions to solve these. Thank you.
You should be aware of the concept of Undefined Behavior, in your circumstances it's pretty safe to assume that your code invokes such behavior at some point and it sometimes causes the program to abruptly terminate due to a segmentation fault and sometimes it just doesn't.
To solve this problem, you need a tool like valgrind. Using this tool or it's equivalent for your OS and environment, you should be able to find the exact place where the violation occurs and solve the apparently random segmentation fault that appears and disappears depending on parts of the code that do not have any relation to the real problem.
I have try the following code to judge prime:
const int N = 200000;
long prime[N] = {0};
long num_prime = 0;
int is_not_prime[N]={1,1};
void Prime_sort(void)
{
for( long i = 2 ; i<N ; i++ )
{
if( !is_not_prime[i] )
{
prime[num_prime++] = i;
}
for( long j = 0; j<num_prime && i*prime[i]<N ; j++ )
{
is_not_prime[i*prime[j]] = 1;
}
}
}
But when I run it, it cause a segmentation fault! That fault I have never meet.And I searched Google,and it explain segmentation fault as follow:
A segmentation fault (often shortened to segfault) is a particular
error condition that can occur during the operation of computer
software. In short, a segmentation fault occurs when a program
attempts to access a memory location that it is not allowed to access,
or attempts to access a memory location in a way that is not allowed
But I don't know where cause this fault in my code.Please help me.
Your array is_not_prime has length N. For example, at the final lap of the outer for loop, i will have the value N-1. When i is that big, is_not_prime[i*prime[j]] will cause you to write far out of bounds of the array.
I'm not quite sure what j<num_prime && i*prime[i]<N is supposed to do, but it is likely part of the bug. Single step through the program with your debugger and see at what values the variables have when the program crashes.
Just re-write your program in a less complex manner and all bugs will go away.
Compare your loop bound checking to your indexing - they aren't the same. (I believe you meant to write i*prime[j]<N in your for loop.)
Your program crashes because an index goes out of bounds. And the index goes out of bounds because your algorithm is not valid.
As it still crashes if you set N at a much smaller value
const int N = 3;
it shouln't be too difficult to see what goes wrong by running your program with pencil and paper...
I have a seg fault in C++ when entering a for loop. But I mean when ENTERING IT. Here is the code I'm running:
std::cout<<"forcing order"<<endl;
std::cout<<"crossoverPointNumber = "<<crossoverPointNumber<<endl;
for (long j=0; j<crossoverPointNumber; j++)
{
std::cout<<"j = "<<j<<". ";
offsprings[1][positionsInParent1[j]] = valuesInParent2[j]; // Forces the order
}//end for j
The output I get on the terminal is:
forcing order
crossoverPointNumber = 4
Segmentation fault
Can anyone explain to me what am I missing here?? it seems to be either very elementary or very complex C++ stuff...
You aren't adding an endl to the cout stream in your loop, so the code you've posted doesn't tell us when you are getting the segmentation fault. Until you add an endl the output stream won't be flushed.
I would strongly suspect that you are running off the end of your positionsInParent1 or valuesInParent2 arrays.