Receiving debugger notifications in the IDE; however the project compiles and runs fine. I have added flags for X0 in an attempt to remedy the problem (thinking debugger software might be pre-c++11), but that has not remedied the issue. I'm experiencing the issue with arrays directly, but have seen it occur with other data types. I have tried refreshing the index in the project and "freshening" the files. I'm also receiving an odd 'invalid overload of endl' notification. Code below is just for experimenting with the IDE. Errors located at every line with an array class function call and the cout statements.
Any thoughts?
#include <iostream>
#include <array>
using namespace std;
int main()
{
array <double, 5> rainfall;
rainfall[0] = 2.3;
rainfall[1] = 0.3;
rainfall[2] = 0.0;
rainfall[3] = 4.1;
rainfall[4] = 0.5;
rainfall.at(5) = 7.2;
for(size_t x = 0; x < rainfall.size(); x++)
{
cout << rainfall[x] << endl;
}
for(size_t x = 0; x < rainfall.size(); x++)
{
cout << "Please enter a rainfall amount: " << endl;
cin >> rainfall[x];
}
for(size_t x = 0; x < rainfall.size(); x++)
{
cout << rainfall[x] << endl;
}
return 0;
}
See this answer: https://stackoverflow.com/a/22001437/1274747
By default the indexer parser does not have C++11 enabled, so to use the C++11 functionality and classes, the -std=c++11 parameter needs to be added to the index parser command line.
You can also check the other answers in the thread, but this one was working in my case.
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 am very new to software programming.I have been working on a C++ project (using Code::Blocks) and trying to get results from a third party DLL written with Visual C++ 2010. So far I am able to load the DLL using LoadLibrary() and get the function address using GetProcAddress(). But I am not sure how I can proceed further to get some meaningful results. Information from the third party DLL manual is not very clear, at least to me. I would be much appreciated, if someone can point me to the right direction, please.
DLL information:
Calcdll.dll exports function StartJob.
StartJob, that accepts three parameters
double aInputData[NINPUTDATA], specifies input data
VARIANT aResData[NRESDATA], contains calculation results
VARIANT aOptions[NOPTIONSDATA], specifies optional flags (for future use)
NINPUTDATA: 100
NRESDATA: 100
NOPTIONSDATA 1
Usage from C++
// Calcdlld.h is located in the installation directory together
// with calcwin.dll and calcwin.lib
#include “calcdlld.h”
void CCalcdllsvrView::OnCalcolo()
{
// TODO: Add your control notification handler code here
VARIANT aRis[NRESDATA];
double aInp[NINPUTDATA];
double aOpt[NOPTIONSDATA];
// Collect data from input mask
GetData(vInp);
// Check for errors
if (!StartJob(aInp, aRis, aOpt))
return;
// Show results
ShowResults(aRis);
}
/*
Link Calcdll.lib together with your files.
Warning: positions 14,15 and 30 of the output array contain BSTR values, so in order to convert them to CString (in
Visual C++) is possible to create an instance of the class CString passing aRis[n].bstrVal to the constructor (see
VARIANT structure declaration):
*/
CString coilDescription (aRis[29].bstrVal);
AfxMessageBox(CString(“Coil type: “) + coilDescription);
//Contents of calcdlld.h
#define NINPUTDATA 100
#define NOPTIONSDATA 1
#define NRESDATA 100
extern "C" BOOL FAR PASCAL EXPORT StartJob(double vInp[NINPUTDATA], VARIANT vRis[NRESDATA], double vOpt[NOPTIONSDATA]);
My code
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
#include <windows.h>
int main() {
double vInp[100];
VARIANT vRis[100];
double vOpt[1];
for (int i = 0; i < 100; i++ ) {
vInp[i] = 0;
}
//vInp[] = ;
vInp[0] = 2;
vInp[1] = 25;
vInp[8] = 1;
vInp[9] = 45;
vInp[14] = 2;
vInp[15] = 48;
vInp[16] = 2;
vInp[17] = 16;
vInp[18] = 11250;
vInp[26] = 45;
vInp[28] = 2.41627;
vInp[40] = 1;
vInp[45] = 1;
vInp[50] = 14;
vInp[51] = 2.2;
vInp[60] = 1;
vInp[61] = 0.11;
vInp[62] = 0.35;
vInp[63] = 203;
vInp[77] = 2;
cout << "vInp" << endl;
for (int i = 0; i < 100; i++ ) {
cout << i << ": " << vInp[i] << endl;
}
cout << "Start!" << endl;
typedef int (__stdcall *f_StartJob)(double vInp[100], VARIANT vRis[100], double vOpt[1]);
HINSTANCE hGetProcIDDLL = LoadLibrary("C:\\......\\calcdll.dll");
if (!hGetProcIDDLL) {
cout << "could not load the dynamic library" << endl;
return EXIT_FAILURE;
} else {
cout << "DLL loaded!" << endl << endl;
}
// resolve function address here
f_StartJob StartJob = (f_StartJob)GetProcAddress(hGetProcIDDLL, "StartJob");
if (!StartJob) {
cout << "could not locate the function" << endl;
return EXIT_FAILURE;
}
cout << "StartJob() returned " << StartJob(vInp, vRis, vOpt) << endl;
cout << vRis << endl;
return EXIT_SUCCESS;
}
Many thanks in advance!
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!
For some unknown reason this simple code runs, does what it's expected to do and then crashes the run. I am using NetBeans IDE, which overlapped my arrays before (tends to be buggy), so I was wondering if someone gets the same error - that would mean I certainly have to change the IDE environment.
#include <iostream>
using namespace std;
int main ()
{
int first[4][4];
for (int a = 0; a < 5; a++)
{
for (int b = 0; b < 5;b++)
{
cout << a << " " << b << " ";
if (first [a][b] != 0)
{
first[a][b] = 0;
}
cout << first[a][b] << " ";
}
cout << endl << endl << endl;
}
return 0;
};
here you are declearing a array with 4 indexes.In c/c++ index number starts at 0.
In your code you are saying :
int first[4][4];
that means indexs are : 0 1 2 3.Array length or total index are 4.
But in for loop you are saying
for (int a = 0; a < 5; a++) {
....
}
so you are trying to access index number 0 1 2 3 4 respectively.But remember you don't have index number 4.That is why it should give array index out of bound error.
Also at the end of main function you are using a semicolon.remove that
main () {
....
};
Hope this solves the problem.From next time Please try to provide details about the errors your IDE is giving you as it will be easier for the people who are giving answer.
I am currently working on a project where we are using a Moxa EM-1220-LX running linux, programming it using C++. During the development I have come across a bizzare error.
I have the following code:
std::string vector_to_string(std::vector<int> vec) {
std::stringstream ss;
for (int i = 0; i < vec.size(); i++) {
ss << vec[i] << ".";
}
return ss.str();
}
void print_string(std::string text) {
std::cout << text << "\n";
}
std::vector<int> local_vector;
local_vector.push_back(123);
local_vector.push_back(456);
local_vector.push_back(789);
//Case 1: Prints 456.789.
print_string(vector_to_string(local_vector));
//Case 2: Prints 123.456.789.
std::string temp = vector_to_string(local_vector);
print_string(temp);
Anyone have any idea why the first entry in the vector gets removed in Case 1, but works fine in Case 2?
I cannot reproduce your issue by compiling the code from the question. Perphaps you have a bug somewhere else in the code which causes heap/stack corruption.
Old answer: you seem to have a bug here:
for (int i = 0; i < vec.size(); i++) {
ss << vec << ".";
}
Shouldn't the middle line read ss << vec[i] << ".";? This may well explain why your vector is not printed properly.