Moxa embedded programming: weird error - c++

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.

Related

How can I make a vector with all the video modes in SFML?

I have this:
std::vector <sf::VideoMode> *screenResolution = new std::vector<sf::VideoMode>;
*screenResolution = sf::VideoMode::getFullscreenModes();
for (std::size_t i = 0; i < screenResolution->size(); ++i)
{
std::cout << screenResolution[i]->width << ":" << screenResolution[i]->height <<std::endl;
}
And for some reason an error appears in the cout that says "the expresion must be a type of pointer".
You have to carefuly read error message. Your vector is of sf::VideoMode which doesn't look like pointer. Only -> can dereference pointers, so you can't use it in your loop. You also probably don't need dynamic allocation for your vector.
The following code should work for you:
std::vector<sf::VideoMode> screenResolution = sf::VideoMode::getFullscreenModes();
for (std::size_t i = 0; i < screenResolution.size(); ++i)
{
std::cout << screenResolution[i].width << ":" << screenResolution[i].height << std::endl;
}

<iostream> std:cout within function not printing (FIXED:for loop issue)

NEVERMIND, FIXED IT. for loop issue
I'm trying to do some number crunching with formatted output, and I've run into a problem with half my output not printing. I've written a small test code to illustrate the problem:
#include <iostream>
int testF(){
for (int i; i<10; i++) {
std::cout << i << "\n";
}
return 0;
}
int main(){
std::cout << "START_(this_line_seems_to_be_causing_problems)\n";
int sret = 1;
sret = testF();
std::cout << sret << "\n";
std::cout << "END\n";
return 0;
}
The problem seems to hinge on the line std::cout << "START_(this_line_seems...".
If I comment out this line, it will print the contents of testF(). If I don't then testF() does not print, although it does give a return value.
It's important that I can print from both main and my functions. Is there a way I can do both?
try initializing i:
int testF(){
for (int i = 0; i<10; i++) {
std::cout << i << "\n";
}
return 0;
}
and why int sret = 1; // value is never used or passed into function
all you want is:
int sret;
sret = testF();
std::cout << sret << "\n";
std::cout << "END\n";
also why doing
sret = testF(),
cout<<sret
this will just append 0 to your output,
instead call testF() directly.
for (int i; i<10; i++) {
This loop tries to read from an uninitialised int. That's undefined behaviour, which means everything can happen and that the program may behave strangely.
What probably happens here in practice is that the memory location of the uninitialised variable contains some random data which is interpreted as something greater than 10, and that the random data is somehow affected by the previous std::cout statement. But that's just guessing; the C++ language simply does not define how your program behaves.
In order to avoid undefined behaviour, you must initialise the variable:
for (int i = 0; i<10; i++) {
By the way...
int sret = 1;
sret = testF();
std::cout << sret << "\n";
This can be written more concisely as:
std::cout << testF() << "\n";

c++ polynomial copy-constructor and ostream override cause memeory leaks?!

Hi there I am working on a polynomial class in c++. So far everything works very well. But now I encountered an error I simply cannot spot :/
polynomial.cpp
// copy-constructor
Polynomial::Polynomial(const Polynomial &p){
size = p.size;
buffer = p.buffer;
poly = new double(buffer);
for (int i = 0; i < size; ++i) poly[i] = p[i];
for (int i = size; i < buffer; ++i) poly[i] = 0;
}
// output stream override | it's a non-member function
ostream& operator<<(ostream& os, const v1::Polynomial& p){
int degree = p.degree();
stringstream ss;
if (degree == 0) ss << '0';
else if (degree > 0){
ss << '(';
for (int i = degree; i >= 0; --i){
ss << p[i];
ss << "x^";
ss << i;
if (i > 0)
ss << " + ";
}
ss << ')' << endl;
}
os << ss.str();
return os;
}
So this is how I call the copy-constructor:
// note: printing 'a' itself does not cause problems...
v1::Polynomial b(a);
cout << "Polynomial b: " << b << " degree: " << b.degree() << endl;;
The stack-log from Visual Studio says it's in Line 23 (here: the line above this one, where I actually want to print 'b') and then it continues to call some heap functions etc..
Running the program without debug (via cmd) results in an APPCRASH, with "Polynomial b: " being the last thing displayed.
I unfortunately don't know how to debug in Visual Studio, I was used to valgrind in linux, which i currently don't have set up :/
Anyone any idea? Or do you need additional info?
Regardless, many thanks in advance =)
poly = new double(buffer);
Here you allocate a single double and set it to buffer. You probably mean
poly = new double[buffer];
or whatever size you desire.
A much better solution would be using std::vector instead of raw arrays. You can copy it with =, resize it with std::vector::resize and reserve more space with std::vector::reserve.

how to access images in sequence using loop in openCV

Guys i ran into a problem regarding accessing images in sequential order. i have images whose names change with incrementing number i.e. cube_0.jpg, cube_1.jpg, .... and so on. Now i want to access each image one-by-one and show.
Following is my code that i am playing with since 2-days and don't know how to handle this situation or what is wrong with this problem.
ostringstream s;
for (int fileNumber = 0; fileNumber<=40; fileNumber++)
{
s<<"\"cube_"<<fileNumber<<"\.jpg\""<<endl;
string fullfileName(s.str());
images[i] = fullfileName;
}
stringstream ss;
cout<<"file name"<<images[0]<<endl;
for (int file = 0; file<41; file++)
{
string str = images[file];
cout<<"str "<<str<<endl;
img_raw = imread(ss.str(), 1); // load as color image Error
cout<<"Done"<<endl<<"size"<<img_raw.size();
system("pause");
}
This code runs fine till it gets reached to "img_raw = imread(ss.str())", now this line is basically hindering me from accessing file. Since imread requires "string& filename" therefore i performed stringstream operation but nothing is working!
Any help would be greatly appreciated!
There are a few errors.
Your stringstream ss is empty. You declared it but did not fill with any values. I am pretty sure you meant imread(str, 1); instead of imread(ss.str(), 1);
In the first for loop, you are continuously printing filenames to ostringstream, so it goes like this:
0: "cube_0.jpg\"
1: "cube_0.jpg\""cube_1.jpg\"
2: "cube_0.jpg\""cube_1.jpg\""cube_2.jpg\"
...
so the ostringstream just grows and grows. ostringstream needs to be declared in the loop to be cleared for every iteration.
Edited code:
string images[41];
Mat img_raw;
for (int fileNumber = 0; fileNumber < 41; fileNumber++)
{
stringstream ss;
ss << "\cube_" << fileNumber << "\.jpg" << endl;
string fullfileName;
ss >> fullfileName;
images[fileNumber] = fullfileName;
}
for (int file = 0; file < 41; file++)
{
cout << "Loading " << images[file] << endl;
img_raw = imread(images[file], 1);
if (!img_raw.empty())
{
cout << "Successfully loaded " << images[file] << " with size " << img_raw.size() << endl;
}
else
{
cout << "Error loading file " << images[file] << endl;
}
system("pause");
}

ostringstream SOMETIMES causes a crash

So I wrote an implementation of an ArrayList over the summer, and I have a toString method within which I use an ostringstream object to tack on strings and then output them.
the method is below:
template<class T>
std::string ArrayList<T>::toString() {
std::ostringstream streamOut;
streamOut << "(";
for (int i = 0; i < size; i++) {
streamOut << array[i];
if (i != (size - 1)) {
streamOut << ", ";
}
}
streamOut << ")\n";
std::string returnString = streamOut.str();
return returnString;
}
The problem is that when I run this program it sometimes crashes on the line in the above method:
streamOut << "(";
I tried adding a flush statement at the end but that didn't do the trick...I really have no idea what could be wrong here.
I think this issue may be related but I can't be sure....
https://stackoverflow.com/questions/8250851/big-ostringstream-causes-application-crash
EDIT:
I forgot to mention, that I am using eclipse for development, and I have been unable to get a crash of the program to occur when I run it in eclipse. It's only when I run the exe generated via windows or the command line that it crashes
I think it crashed because somewhere before this method incorrectly free the memory.