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.
Related
I am writing a Particle-In-Cell code to simulate plasma-laser interactions. The core of the code is the following (all the variables have been initialized properly before this):
for(int it = 0; it < Nt-1; it++)
{
//cout << "it = " << it << endl;
pos = pos_update(pos, vel, qm, E_P, it);
vel = vel_update(pos, vel, it);
rho_part = rho_part_update(rho_part, pos, qm, it);
rho = rho_update(rho, rho_part, it);
E_P = E_P_update(E_P, pos, rho, rho_part, it);
}
}
As you can see cout is commented. If so, when executing the program it eventually stops and the debugger shows the message "Program received signal SIGTRAP, Trace/breakpoint trap". The weird thing is that if I allow that "cout" line of code (which I wrote to find out in which time the code fails), then there is no error and the code works as expected. Any idea of what is happening? If you need the detailed functions
SIGTRAP suggests that your C++ code has Undefined Behavior. But SIGTRAP is just one of the infinite ways in which Undefined Behavior can manifest itself. It's not that strange that adding another statement causes Undefined Behavior to show up in a different manner.
As for your comment, new[] and delete are primitives that should generally be avoided. std::vector is probably the cleaner solution. But if you do use then, then new[] must be paired with delete[], not delete.
I am new in protocol buffer. So, I am trying to implement a small c++ code for protocol-buffer3.
In my protocol buffer, I have only one variable
int32 val = 1;
In C++ code, when I use Bytesize() function to know the total size, but this application receives segmentation fault all the time.
Codes are
test.proto
syntax = "proto3"
message testmsg {
int32 val = 1;
}
//cpp codes
main(){
testmsg test1;
memset(&test1, 0, sizeof(testmsg));
test1.set_val(1); // works fine
cout<<test1.val()<<endl; //works fine. Display the result
int size = test1.ByteSize(); //---> Segmentation faults here
cout << "size = " <<size;
}
Thank you
I got the solution.
memset function is creating this problem.
I removed the memset function. it works fine
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 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);
}
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.