My program is stopping [closed] - c++

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
This is my class,and my problem is when I call the constructor
Eveniment e1(1,m1);
with parameters in main method, my program is stopping and I don't know why. M1 is an object of IntrareCAlendar.
class Eveniment{
private:
const int id;
IntrareCalendar data;
char* detalii;
int static nrIntrari;
public:
Eveniment(int nr,IntrareCalendar ic) :id(nr){
this->data = ic;
nrIntrari++;
}
~Eveniment(){
if (this->detalii != NULL)
delete[]this->detalii;
}
};
What should I do? thanks a lot!

You never set detalii to anything valid. It remains uninitialised - it is not initialised to a particular value automatically. You could set it to nullptr in your constructor. (Don't use NULL in C++.)
Your destructor calls delete[] on that member, but no new[] has been called prior to that. As such the behaviour of your program is undefined.
Also, consider using static std::atomic<int> as the type for nrIntarari in case multiple threads instantiate an Eveniment.

Related

Would this be considered bad programming practice? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
I had a situation where I wanted to use a default paramenter for a reference in a VERY large legacy codebase for a fix.
static bool _defaultValue = false;
bool SomeFunction(const SomeComplexObject& iObj, bool& isSomeVal = _defaultValue )
{
// ... code
}
My issue is with using a static variable inside a namespace just dangling there by itself.
This code is going to be reviewed before being shipped but I'm unsure if it would be considered bad practice to have a dangling static variable like that.
Without the variable you can't have a default value for a reference. My options are very limited to make other changes to get the desired effect.
Would this be considered "hacky unprofessional coding"?
My suggestion would be:
Remove the global variable.
Don't use a default value for the reference argument.
Create a function overload that has only one argument.
Call the first function from the second function.
bool SomeFunction(const SomeComplexObject& iObj, bool& isSomeVal)
{
// ... code
}
bool SomeFunction(const SomeComplexObject& iObj)
{
bool dummy;
return SomeFunction(iObj, dummy);
}
Client code can call whichever function is appropriate in their context.

memory access violation using std::map as local member [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I have a memory access violation error on the line m_Lights=tmp; during the call of the method v_init() in Algo::Init(). Shouldn't map m_Lights be created at the instantiation of m_LightsManager? Why do I have this error?
class LightManager
{
private:
std::map<sint32,Light> m_Lights;
public:
LightManager (void);
~LightManager (void);
void v_init();
};
LightManager ::LightManager (void)
{
}
LightManager ::~LightManager (void)
{
}
void LightManager ::v_init()
{
Light tL;
std::memset(&tL,0,sizeof(Light));
std::map<sint32,Light> tmp;
tmp.insert(std::pair<sint32,Light> (-1,tL));
m_Lights=tmp;
}
class Algo
{
private:
LightsManager m_LightsManager;
....
public:
Algo();
void Init();
};
Algo::Algo()
{
Init();
}
void Algo::Init()
{
m_LightsManager.v_init();
}
If Light is not trivially-copyable then
std::memset(&tL,0,sizeof(Light));
is undefined behavior. This is likely the cause of your error.
In addition to Vittorios answer: Do all the initialization in Lights constructor, instead of relying on an external memset call. And don't use std::memset in C++ to initialize all member variables of an object in one statement, do it explicitely for every variable (usually using a simple assignment; in C++11 you can do that even in the declaration/header, where it IMHO belongs).
Reason: Light may be derived, and the base class(es) define their own data, which you overwrite by recklessly nulling the whole object.
I solved the error, the problem came from a wrong declared pointer in other part of the soft that overlaped with the memory space where the object m_LightsManager where initiated ,

C++ Initialising std::vector<std::unique_ptr<AbstractClass>> [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I've derived std::vector and I'm trying to initialise it. I don't understand why the following is happening:
namespace Bpm {
typedef std::unique_ptr<PolingNormalised> PolingNormalisedPtr;
class PolingNormalisedArray : public std::vector<std::unique_ptr<PolingNormalised>>
{
using std::vector<std::unique_ptr<PolingNormalised>>::vector;
};
}
// Works perfectly
Bpm::PolingNormalisedArray qpmNormArray;
qpmNormArray.push_back(std::move(Bpm::PolingNormalisedPtr(new Bpm::QpmNormalised(2.))));
// Doesn't compile
Bpm::PolingNormalisedArray qpmNormArray(std::move(Bpm::PolingNormalisedPtr(new Bpm::QpmNormalised(2.))));
// Doesn't compile
Bpm::PolingNormalisedArray qpmNormArray(Bpm::PolingNormalisedPtr(new Bpm::QpmNormalised(2.));
// Doesn't compile
Bpm::PolingNormalisedArray qpmNormArray(new Bpm::QpmNormalised(2.));
I don't understand why many of the initialisation methods do not compile. What's going on here? What's the proper way to initialise the std::vector?
Thank you.
If you get rid of all the unique_ptrs and aliases running around in your example, what you're trying to do in the cases that don't compile boils down to this:
#include <vector>
struct foo{};
int main()
{
std::vector<foo> vec(foo{});
}
This obviously won't compile because std::vector has no constructor that takes an instance of the value_type as its argument.
Either use vector::push_back as you've shown in your example, or use vector::emplace_back to construct the unique_ptr in-place within the vector without having to move it.
qpmNormArray.emplace_back(new Bpm::QpmNormalised(2.));

Error while trying to create simple thread in c++ [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
Im trying to create a simple thread and have it execute.
My function definition is:
void MyClass::myFunction()
{
//Do Work
}
I'm creating the thread and executing it:
std::thread t1(myFunction);
Upon compiling my code, i get the following error:
error C3867: function call missing argument list; use '&MyClass::myfunction' to create a pointer to member.
Since my function does not take any parameters, i'm assuming i'm declaring it wrongly where i'm creating my thread? Any help will be appreciated, Thanks!!
If your method is a non-static member : You need an instance of your object to call the member function on.
If your method is static member, do what the compiler suggest : simply pass the address of your function.
Example:
class A
{
public:
void foo() { cout << "foo"; }
static void bar() { cout << "bar"; }
};
int main() {
std::thread t1(&A::foo, A()); // non static member
t1.join();
std::thread t2(&A::bar); // static member (the synthax suggested by the compiler)
t2.join();
return 0;
}

What is wrong with my function? (method to copy from private class member into passed in float) [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
These functions are both public members of a class. Private members of the class include *theCharArray and *theFloat.
This one works fine:
void theClass::getCharArray(char charArrayParam[]) const
{
strcpy(charArrayParam, this->theCharArray);
}
This one underlines "this" and VS express says "Error: Expression must be modifiable value"
void theClass::getFloat(float theFloatParam) const
{
theFloatParam = this->theFloat;
}
Please tell me what I'm doing wrong.
In theClass::getCharArray(char charArrayParam[]), charArrayParam is passed basically as a pointer to character array without any idea of the buffer size. This is kind of risky with the risk of overflowing the buffer. Netter interface would be:
theClass::getCharArray(char *charArrayParam, int charArraySize) const {
strncpy(charArrayParam, this->theCharArray, charArraySize - 1);
charArrayParam[charArraySize - 1] = 0;
}
And for the second one:
void theClass::getFloat(float *theFloatParam) const
{
*theFloatParam = this->theFloat;
}
otherwise, since theFloatParam being passed by value, changing that within the function has no effect on the caller.