SIGTRAP does not show up if I write "cout" - c++

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.

Related

Program received signal SIGSEGV, Segmentation fault while debugging in codeblocks

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.

Error deleting an array while porting code from Windows to Linux

I'm trying to port a library, which correctly works in Windows, to Linux.
In these lines of code I get an error:
long* permutation = new long[result->getGeneListCount()];
for(long i=0; i<result->getGeneListCount(); i++)
permutation[i]=i;
Util::ArrayUtil::DurstenfeldArrayPermutation<long>(permutation, result->getGeneListCount());
//result->PerformGenePermutation(permutation);
std::cout << "Just skipped the permutation" << std::endl;
delete[] permutation;
The error seems, to me, to occur during the deletion. I know that, since I have commented the PerformGenePermutation(), I could simply comment also the other lines, but similar problem could appear again in the other code, so I would like to understand the error.
The error-output which I get is:
*** glibc detected *** /usr/lib/jvm/java-7-oracle/bin/java: munmap_chunk(): invalid pointer: 0x09f287f8 ***
Can anyone help me, please?
Please, ask me if you need further details.
The given code & info is not sufficient to nail down the cause of the problem, but you can do the following:
replace the code
long* permutation = new long[result->getGeneListCount()];
for(long i=0; i<result->getGeneListCount(); i++)
permutation[i]=i;
Util::ArrayUtil::DurstenfeldArrayPermutation<long>(permutation, result->getGeneListCount());
//result->PerformGenePermutation(permutation);
std::cout << "Just skipped the permutation" << std::endl;
delete[] permutation;
with
std::vector<long> permutation( result->getGeneListCount() );
for(long i=0; i<long(permutation.size()); i++)
permutation[i]=i;
Util::ArrayUtil::DurstenfeldArrayPermutation<long>(&permutation.at( 0 ), permutation.size());
//result->PerformGenePermutation(permutation);
std::cout << "Just skipped the permutation" << std::endl;
//delete[] permutation;
Note that the delete is removed since std::vector does that automatically for you.
If this now throws an exception from range error from std::vector::at, well then you know that the size is probably zero. Anyway you can now very simply check that in your debugger. And more importantly, if it does not throw an exception, then you know that all's well and good with this code (because std::vector is reliable), so the problem is then elsewhere.
Unfortunately this was too long to post as a comment, but it's not really an answer. This is a problem with SO. Since it's designed for pure answers it doesn't support general help.

c++ new crashing in release only (MSVS10)

When running the release executable only (No problems occur when running through visual studio) my program crashes. When using "attach to process" function visual studio indicates the crash occurred in the following function:
World::blockmap World::newBlankBlockmap(int sideLen, int h){
cout << "newBlankBlockmap side: "<<std::to_string((long long)sideLen) << endl;
cout << "newBlankBlockmap height: "<<std::to_string((long long)h) << endl;
short*** bm = new short**[sideLen];
for(int i=0;i<sideLen;i++){
bm[i] = new short*[h];
for(int j=0;j<h;j++){
bm[i][j] = new short[sideLen];
for (int k = 0; k < sideLen ; k++)
{
bm[i][j][k] = blocks->getAIR_BLOCK();
}
}
}
return (blockmap)bm;
}
Which is called from a child class...
World::chunk* World_E::newChunkMap(World::floatmap north, World::floatmap east, World::floatmap south, World::floatmap west
,float lowlow, float highlow, float highhigh, float lowhigh, bool displaceSides){
World::chunk* c = newChunk(World::CHUNK_SIZE+1,World::HEIGHT);
for (int i = 0; i < World::CHUNK_SIZE ; i++)
{
for (int k = 0; k < World::CHUNK_SIZE ; k++)
{
c->bm[i][0][k] = blocks->getDUMMY_BLOCK();
}
}
c->bm[(int)floor((float)(World::CHUNK_SIZE+1)/2.0f)-1][1][(int)floor((float)(World::CHUNK_SIZE+1)/2.0f)-1] = blocks->getSTONE_BLOCK();
c->bm[(int)ceil((float)(World::CHUNK_SIZE+1)/2.0f)-1][1][(int)floor((float)(World::CHUNK_SIZE+1)/2.0f)-1] = blocks->getSTONE_BLOCK();
c->bm[(int)floor((float)(World::CHUNK_SIZE+1)/2.0f)-1][1][(int)ceil((float)(World::CHUNK_SIZE+1)/2.0f)-1] = blocks->getSTONE_BLOCK();
c->bm[(int)ceil((float)(World::CHUNK_SIZE+1)/2.0f)-1][1][(int)ceil((float)(World::CHUNK_SIZE+1)/2.0f)-1] = blocks->getSTONE_BLOCK();
return c;
}
where...
class World {
public: typedef short*** blockmap;
...
The line which VS points at is...
short*** bm = new short**[sideLen];
The "attach to process" function stats the Local variables are...
sideLen = 1911407648
h = 0
which is what i did NOT expect, but the cout outputs 9 and 30 respectively, which was expected.
I am aware that most "crashes in release only" problems are due to uninitialized variables, however, I fail to see that related here.
The only error message I get is...
Windows has triggered a breakpoint in Blocks Project.exe.
This may be due to a corruption of the heap
I am stumped on this problem, what's the error? how can I better debug release executable?
I can post more code if needed, however, bear in mind there is a lot of it.
Thank you in advanced.
"And I don't see World::newBlankBlockmap() called from that second chunk of code. – Michael Burr", I forgot that bit, here you go...
World::chunk* World::newChunk(int side, int height){
cout << "newChunk side: "<<std::to_string((long long)side) << endl;
cout << "newChunk height: "<<std::to_string((long long)height) << endl;
chunk* ch = new chunk();
ch->bm = newBlankBlockmap(side,height);
ch->fm = newBlankFloatmap(side);
return ch;
}
where...
struct chunk {
blockmap bm;
floatmap fm;
};
as defined in the World class
To reiterate what the comments where hinting at: From what you've posted, you're code seems to be badly structured. Triple pointer constructs like short*** are almost impossible to debug and should be avoided at all costs. The heap corruption error message you got suggests that you have a bad memory access somewhere in your code, which is impossible to find automatically with your current setup.
Your only options at this point are to either dig through your entire code manually, until you've found the bug, or start refactoring. The latter might seem like the more time-consuming now, but it won't be if you plan to work with this code in the future.
Consider the following as possible hints for a refactoring:
Don't use plain arrays for storing values. std::vector is just as effective and a lot easier to debug.
Avoid plain new and delete. In modern C++ with the STL containers and smart pointers, plain memory allocation should only happen in very rare exceptional cases.
Always range-check your array access operations. If you worry about performance, use asserts which disappear in release builds, but be sure the checks are there when you need them for debugging.
Modeling three-dimensional arrays in C++ can be tricky, since operator[] only offers support for one-dimensional arrays. A nice compromise is using operator() instead, which can take an arbitrary number of indices.
Avoid C-style casts. They can be very unpredictable. Use the C++ casts static_cast, dynamic_castand reinterpret_cast instead. If you find yourself using reinterpret_cast regularly, you probably have a mistake in your design somewhere.
There is a problem in this line short*** bm = new short**[sideLen];. The memory is allocated for sideLen elements, but the assignment line bm[i][j][k] = blocks->getAIR_BLOCK(); requires an array having size sideLen * sideLen * h. To fix this problem changing of the 1st line to short*** bm = new short**[sideLen * sideLen * h]; is required.

semop() failing at failing

I'm trying to write a program in C++, compiled in GCC 4.6.1 on Ubuntu 11.10, and the IPC is giving me a hard time. To demonstrate, here's my code for signaling a semaphore, with semid and semnum already supplied:
struct sembuf x;
x.sem_num = semnum;
x.sem_op = 1;
x.sem_flg = SEM_UNDO;
int old_value = semctl(semid, 0, GETVAL);
if(semop(semid, &x, 1) < 0)
{
std::cerr << "semaphore failed to signal" << std::endl;
}
else if(semctl(semid, 0, GETVAL) == old_value)
{
std::cerr << "signal returned OK, but didn't work" << std::endl;
}
The code for "wait" is similar; the main difference, of course, is that sem_op is set to -1. Sometimes I get the first error message here, but as often as not I get the second, which makes no sense at all to me. The first, I imagine I could hunt for an error code (though I'm not sure if that depends on C++11 features I'm not supposed to use), but I've got no idea how to even begin addressing the second. Rebooting didn't work. GDB isn't being much help, especially when "next" and "step" seem to jump around back and forth instead of going forward in sequence.

Seg faults with pthreads_mutex

I am implementing a particle interaction simulator in pthreads,and I keep getting segmentation faults in my pthreads code. The fault occurs in the following loop, which each thread does in the end of each timestep in my thread_routine:
for (int i = first; i < last; i++)
{
get_id(particles[i], box_id);
pthread_mutex_lock(&locks[box_id.x + box_no * box_id.y]);
//cout << box_id.x << "," << box_id.y << "," << thread_id << "l" << endl;
box[box_id.x][box_id.y].push_back(&particles[i]);
//cout << box_id.x << box_id.y << endl;
pthread_mutex_unlock(&locks[box_id.x + box_no * box_id.y]);
}
The strange thing is that if I uncomment one (it doesn't matter which one) or both of the couts, the program runs as expected, with no errors occurring (but this obviously kills performance, and isn't an elegant solution), giving correct output.
box is a globally declared
vector < vector < vector < particle_t*> > > box
which represents a decomposition of my (square) domain into boxes.
When the loop starts, box[i][j].size() has been set to zero for all i, j, and the loop is supposed to put particles back into the box-structure (the get_id function gives correct results, I've checked)
The array pthread_mutex_t locks is declared as a global
pthread_mutex_t * locks,
and the size is set by thread 0 and the locks initialized by thread 0 before the other threads are created:
locks = (pthread_mutex_t *) malloc( box_no*box_no * sizeof( pthread_mutex_t ) );
for (int i = 0; i < box_no*box_no; i++)
{
pthread_mutex_init(&locks[i],NULL);
}
Do you have any idea of what could cause this? The code also runs if the number of processors is set to 1, and it seems like the more processors I run on, the earlier the seg fault occurs (it has run through the entire simulation once on two processors, but this seems to be an exception)
Thanks
This is only an educated guess, but based on the problem going away if you use one lock for all the boxes: push_back has to allocate memory, which it does via the std::allocator template. I don't think allocator is guaranteed to be thread-safe and I don't think it's guaranteed to be partitioned, one for each vector, either. (The underlying operator new is thread-safe, but allocator usually does block-slicing tricks to amortize operator new's cost.)
Is it practical for you to use reserve to preallocate space for all your vectors ahead of time, using some conservative estimate of how many particles are going to wind up in each box? That's the first thing I'd try.
The other thing I'd try is using one lock for all the boxes, which we know works, but moving the lock/unlock operations outside the for loop so that each thread gets to stash all its items at once. That might actually be faster than what you're trying to do -- less lock thrashing.
Are the box and box[i] vectors initialized properly? You only say the innermost set of vectors are set. Otherwise it looks like box_id's x or y component is wrong and running off the end of one of your arrays.
What part of the look is it crashing on?