MatrixXd p(10,10);
// set values of p
// for loop prints out all values of p
for (int i = 0; i < 100; i++)
std::cout << p(i/10,i%10) << std::endl;
const IOFormat CleanFmt(4,0,", ","\n","[","]");
std::cout << p.format(CleanFmt);
// fails before running any code with error 1 (cxx = 20)
// fails before running any code with error 127 (cxx = 14/11)
Having trouble debugging this, as it is failing outright. My OS setup is Cygwin in Windows. Perhaps it is failing since windows does not support FIFO-Qs?
Or is something wrong going on here?
Related
I have a program that performs an FFT on a 2d array. In order to work with the fft library fftw3 I have to use a temporary array (called FTtemp) that reads out the result of the FFT: it is 3d since it contains the x & y axis plus the real and imaginary value for each (x,y) tupel.
The transfer of the data from the FFT array (which has a special variable type) to the ordinary array is working in debug mode but not in release. In release I get the following runtime error: Access violation writing location 0x02913000.
From my google search I found that release version bugs are usually related to uninitialized objects. This led me to explicitly initialise every item in FTtemp with 0.0, however to no avail. Furthermore, I printed the FFt array items to console and numbers appeared which means that they are also initialised. Hence, I am a bit out of ideas and wondered if someone might be wiser than me?
Here is the code snippet I am talking about. Since the program relies on a lot of other things, I was not quite able to recreate a minimal example yet, but I will add one as soon as I got the same error.
Fun fact: I print the I & j values of the loop to the console for trouble shooting and it is another (I,j) tupel where it crashes every time when I run it: eg: 49,212 or 116,169. I am really confused by this.
FTtemp = new double** [width];
for (i = 0; i < width; i++) {
FTtemp[i] = new double*[height];
}
for ( i = 0; i < width; i++)
{
for (j = 0; j < height; j++) {
FTtemp[i][j] = new double[2];
FTtemp[i][j][0] = 0.0;
FTtemp[i][j][1] = 0.0;
}
}
cout << "width,height: " << width << "," << height << endl;
for (i = 0; i < width; i++)
{
for (j = 0; j < height; j++) {
/*
cout << "access to out: " << out[indexFFT(i, j)][0] << endl;
cout << "access to FTtemp: " << FTtemp[i][j][1] << endl;
*/
cout << "i,j is: " << i << "," << j << endl;
FTtemp[i][j][1] = out[indexFFT(i, j)][0]; <--------- error occours here
FTtemp[i][j][2] = out[indexFFT(i, j)][1];
}
}
Thank you for your consideration.
All the best,
Blue
There is an error in this line:
FTtemp[i][j][2] = out[indexFFT(i, j)][1];
Notice that FTtemp[i][j] is initialized to new double[2] earlier in your code, which means that FTtemp[i][j][2] is an out-of-bounds write.
There may be other issues here - perhaps indexFFT(i, j) gives a bad index? - but without seeing how out was initialized or how indexFFT works we can't be sure.
Hope this helps!
I'm currently encountering the most strange behavior in my C++ project. What I'm trying to do is calculate the euclidean distance between two double vectors (well, actually, vectors of double vectors, hence the m_Data[0].size()).
This is the source:
double NEAT::Behavior::Distance_To(NEAT::PhenotypeBehavior* other)
{
double sum = 0.0;
for (int i = 0; i < m_Data[0].size() && i < other->m_Data[0].size(); i++) {
double x1 = m_Data[0][i];
double x2 = b->m_Data[0][i];
double difference = x1 - x2;
difference *= difference;
sum += difference;
}
return sqrt(sum);
}
I initially had all this written in one line, but I've split it up to locate the error. What happens is that after a few thousand calls to this function, it throws a SIGSEGV at the last line of the for loop:
sum += difference;
I have NO idea how this could happen. I've checked the stack trace, it's from the Distance_To(...) function and it gets thrown at this line precisely. As soon as I comment it out, everything's fine (but of course the function won't work lol). The signal gets thrown at the same time each time I run the program with the same objects interacting.
Help would be much appreciated. Thanks!
Edit: I've verified the integrity of the pointers in this method by printing out the needed values before entering the loop. All values get printed correctly. Here is the complete version of the function I used for debugging purposes:
double NEAT::Behavior::Distance_To(NEAT::PhenotypeBehavior* other)
{
double sum = 0.0;
Behavior* b = (Behavior*) other;
// Gets executed without any problems
if (genomeid == 300 && b->genomeid == 399) {
std::cout << "PROBLEM CASE" << std::endl;
std::cout << "Printing values for 300..." << std::endl;
for (int i = 0; i < m_Data[0].size(); i++) std::cout << m_Data[0][i] << std::endl;
std::cout << "Printing values for 399..." << std::endl;
for (int i = 0; i < m_Data[0].size(); i++) std::cout << b->m_Data[0][i] << std::endl;
}
// Doesn't get executed
if (m_Data[0].size() != other->m_Data[0].size()) {
std::cout << "Different sizes, " << m_Data[0].size() << " and " << b->m_Data[0].size() << std::endl;
}
// SIGSEGV at size() call
for (int i = 0; i < m_Data[0].size() && i < b->m_Data[0].size(); i++) {
double x1 = m_Data[0][i];
double x2 = b->m_Data[0][i];
double difference = x1 - x2;
difference *= difference;
// If this line gets commented out, no SIGSEGV but the program starts behaving weirdly afterwards (suddenly different sizes after the faulty run)
sum += difference;
}
return sqrt(sum);
}
ASAN and valgrind are the tools you should use to identify the root cause of this type of errors. Eventhough the error thrown at line sum += difference, your actual error could be somewhere else before hitting this point which corrupts your memory. These tools will help you to track that.
Sorry guys, I missed out on some MultiNEAT framework functions I should've used but didn't for initializing the objects etc. Anyways, thanks a lot to all of you, I learned a lot about using valgrind and ASAN (both are really handy and I didn't know about either of them before! lol) and even got a few good articles to read. Duh!
I'm trying to learn about vectors in C++, but just trying to declare one throws a runtime error in NetBeans.
Error: Run failed (exit value -1, 073, 741, 511, total time 51 ms)
PS: The code works perfectly in Eclipse.
#include <iostream>
#include <vector>
using namespace std;
int main() {
// create a vector to store int
vector<int> vec;
int i;
// display the original size of vec
cout << "vector size = " << vec.size() << endl;
// push 5 values into the vector
for (i = 0; i < 5; i++) {
vec.push_back(i);
}
// display extended size of vec
cout << "extended vector size = " << vec.size() << endl;
// access 5 values from the vector
for (i = 0; i < 5; i++) {
cout << "value of vec [" << i << "] = " << vec[i] << endl;
}
// use iterator to access the values
vector<int>::iterator v = vec.begin();
while (v != vec.end()) {
cout << "value of v = " << *v << endl;
v++;
}
return 0;
}
I have tested on my system (latest GCC and NB dev build) and it works without any problem. The desired output is shown. It also runs fine from command line.
Therefore I assume it's more a configuration or system related problem. But without any further informations it's impossible to find the root cause.
More information please:
What OS / Environment are you using?
What Compiler (and versions) do you have?
What version of Netbeans do you use?
Do other project's work?
Here's some first aid you can try:
Create a new C++ Project (C++ Application). It already includes a blank main() function. Run this and check if there's still an error
Create a simple .cpp file and build it from command line (don't involve any IDE). Does it work this way?
Build and your source file without IDE. From Commandline: g++ <your filename>.cpp
Review your compiler settings (Tools -> Options -> C/C++ at Build Tools). Especially use the Version button and check if everything is right.
Windows only: Make sure you have the correct make executable selected in the compiler settings
Have a look at the IDE's log (View -> IDE Log). Are there any problems mentioned?
Run in Debug without any breakpoint. This is going to stop the execution on any runtime error.
I have a fstream that seems to get into a phantom failure state, even though examining it (via conversion to bool) reveals no error flags. Subsequent reads fail, which is unexpected.
#include <fstream>
#include <iostream>
#include <cassert>
int main()
{
const unsigned int SAMPLE_COUNT = 2;
// Setup - create file with two "samples"; each sample has a double and a float
{
std::ofstream ofs("/tmp/ffs", std::ios::binary);
const double colA[SAMPLE_COUNT] = { 1.0, 2.0 };
const float colB[SAMPLE_COUNT] = { 42.0, 100.0 };
for (size_t i = 0; i < SAMPLE_COUNT; i++) {
ofs.write((char*)&colA[i], sizeof(colA[i]));
ofs.write((char*)&colB[i], sizeof(colB[i]));
}
}
// Actual testcase
{
std::fstream fs("/tmp/ffs", std::ios::binary | std::ios::out | std::ios::in);
assert(fs);
unsigned int sample_n = 0;
while (true) {
double a;
fs.read((char*)&a, sizeof(a));
if (!fs) {
std::cerr << "No more samples\n";
break;
}
std::cerr << "Sample " << (++sample_n) << " first column = " << a << '\n';
// Read column B
float b;
fs.read((char*)&b, sizeof(b));
assert(fs);
std::cerr << "Current value second column = " << b << '\n';
// Multiply it by two and write back
b *= 2;
fs.seekp(-sizeof(b), std::ios::cur);
fs.write((char*)&b, sizeof(b));
assert(fs);
#ifdef DO_THE_TELLG
// Unless I do this, the `fs.read` on the next iteration fails!
// So the loop ends, and I get only the first sample transformed in my file.
fs.tellg();
#endif
}
}
// Inspection - should see values 84 and 200, but see 84 and 100 instead.
{
std::ifstream ifs("/tmp/ffs", std::ios::binary);
std::cerr << "All values at end:\n";
for (size_t i = 0; i < SAMPLE_COUNT; i++) {
double a;
float b;
ifs.read((char*)&a, sizeof(a));
ifs.read((char*)&b, sizeof(b));
std::cerr << a << '\t' << b << '\n';
}
assert(ifs);
}
}
Observe in the following output, only the first sample was "parsed", and so at the end the second sample retains its original value 100:
[root#localhost ~]# ./test
Sample 1 first column = 1
Current value second column = 42
No more samples
All values at end:
1 84
2 100
If I perform a tellg() or tellp() operation on it, the subsequent read succeeds, so the loop is not prematurely ended and the second sample is also multiplied by 2, to produce 200:
[root#localhost ~]# ./test
Sample 1 first column = 1
Current value second column = 42
Sample 2 first column = 2
Current value second column = 100
No more samples
All values at end:
1 84
2 200
This only occurs for me in the following environment:
CentOS 6 x86_64, GCC 4.4.7
CentOS 6 x86_64, GCC 4.8.2 (via devtoolset-2)
I get the expected behaviour, with or without tellg()/tellp(), on:
Coliru, GCC 6.3.0
CentOS 7
(Where a listed compiler supports both C++03 and C++11, I have tried it under both and observed no difference.)
Does my program have UB? Or, is this a libstdc++ bug that I need to work around?
Update: okay, so this is a known thing. But Dietmar doesn't say whether this is standard-mandated, or a libstdc++ bug. To me, it looks like bug 40732, but that was RESOLVED/WONTFIX, so why would my program work as expected under Coliru and CentOS 7? Ideally I'd like to get a better handle on precisely what's going on before putting this workaround in place.
My program seems to always produce ridiculous errors.
Please provide directions for me. The following code segment cutout all irrelevant parts.
Thanks.
Part A of the code segment seems failed to initialize the array correctly, how to debug?
Part B of the code segment always crash, is there anything i missed?
typedef unsigned long T_PSIZE;
int main()
{
int AG_TOTAL = 6 ;
/* part A1 */
T_PSIZE* cntPeopleByAge = new T_PSIZE[AG_TOTAL + 1];
/* part A2 - originally i use static array like this, but it also fails */
//T_PSIZE cntPeopleByAge T_PSIZE[AG_TOTAL + 1];
for (int i = 0; i < (AG_TOTAL + 1); i++)
{
std::cout << i << ":" << cntPeopleByAge[i] << "\t";
cntPeopleByAge[i] = 0;
std::cout << cntPeopleByAge[i] << "\n";
}
std::cout << "cntPeopleByAge:" << cntPeopleByAge[ AG_TOTAL + 1 ] << "\n";
/* part B */
delete [] cntPeopleByAge;
return 0; // <--- crash here!
}
Sample Output
0:200320 0
1:201581 0
2:201582 0
3:201583 0
4:0 0
5:0 0
cntPeopleByAge:1799119387:0:0
Platform: win 7 x64
Compiler: TDM-GCC x64
for (int i = 0; i < (AG_TOTAL + 1); i++)
{
std::cout << i << ":" << cntPeopleByAge[i] << "\t";
// ^^^^^^^^^^^^^^^^
// You're reading uninitialized memory here
cntPeopleByAge[i] = 0;
std::cout << cntPeopleByAge[i] << "\n";
}
And here
std::cout << "cntPeopleByAge:" << cntPeopleByAge[ AG_TOTAL + 1 ] << "\n";
you're going out of bounds. The last valid index is AG_TOTAL.
You've got undefined behaviour (UB). The errors are only as ridiculous as UB can be.
/* Sorry. but that earlier answer is not correct. The loop correctly starts at zero and ends at < the limit. The problem is that you are declaring an array of pointers, but never allocating memory to the objects that they point to. Your output is showing you addresses not numbers. One way is to allocate the objects as you use them (you must delete them individually as well) */
T_PSIZE* cntPeopleByAge = new T_PSIZE[AG_TOTAL + 1];
for (int i = 0; i < (AG_TOTAL + 1); i++)
{
cntPeopleByAge[i] = new T_PSIZE();
}
What you really want to use is the vector class in the standard library which handles all of this for you:
#include <vector>
std:vector<T_PSIZE *> cntPeopleByAge;
cntPeopleByAgex.resize(AG_TOTAL + 1);
Good luck ...