I'm trying to learn c++, I've read a lot about it and get it, but every time I program something that uses OOP concepts I have memory problems.
This is the exeption I'm getting with my code:
First-chance exception at 0x002EFB60 in Mathmatician.exe: 0xC0000005: Access violation executing location 0x002EFB60.
If there is a handler for this exception, the program may be safely continued.
So my question here is:
what is wrong with my specific code?
And more importantly, how can I avoid such exeptions? Are ther rules of thumb in c++ to avoid these?
And another thing: How can I avoid returning a local variable from a function that gets deleted (I assume just return the value itself, not a pointer?)
(More info:This specific code will be used to calculate derivative, with diffrent formulas like devision, multiplication, and more inhereting from the Form virtual class)
// Mathmatician.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <typeinfo>
#include <iostream>
using namespace std;
#include <sstream>
template <typename T>
std::string NumberToString ( T Number )
{
std::ostringstream ss;
ss << Number;
return ss.str();
}
class Form {
public:
virtual Form& Derive() = 0;
Form& operator+(Form& const other);
Form& operator*(Form& const other);
virtual string Print() = 0;
};
class Number : public Form {
public:
int a;
Number(int a) : a(a)
{}
Form& Derive()
{
return (Form&) Number(0);
}
string Print()
{
return NumberToString(a);
}
};
class Addition : public Form {
private:
Form& a;
Form& b;
public:
Addition(Form& const a, Form& const b) : a(a),b(b)
{
}
Form& Derive()
{
return a.Derive() + b.Derive();
}
string Print()
{
return "("+a.Print();// + "+" + b.Print()+")";
};
};
class Multiply : public Form {
private:
Form& a;
Form& b;
public:
Multiply(Form& a, Form& b) : a(a),b(b)
{
}
Form& Derive()
{
return *new Addition(a.Derive(),b.Derive());
//return a.Derive()*b + b.Derive()*a;
}
string Print()
{
return "("+a.Print() + "*" + b.Print()+")";
};
};
inline Form& Form::operator+(Form& const other) // copy assignment
{
return Addition(*this,other);
//LocationNode* A = new LocationNode('A',1,2);
}
inline Form& Form::operator*(Form& const other) // copy assignment
{
return Multiply(*this,other);
}
int _tmain(int argc, _TCHAR* argv[])
{
Form* n = &(*new Addition((Form&)*new Number(5),(Form&)*new Number(5))).Derive();
cout << (*n).Derive().Print() << "/n";
return 0;
}
Thank you!
You may see my answer here. The problem of new is that it does not clear the memory of the allocated space. The solution for new is to add the following line as long as you initialized the elements.
memset(element,0,sizeof(element));
Therefore you can ensure there is no garbage in the memory.
This answer is somehow based on my C language experience. Recently I found that C++ has another function called std::fill
using namespace std;
/* some function */
fill(elem,elem+sizeof(elem),0);
/* end of some funtion */
Related
I am new to C++, and I am trying to return the struct declared within a class via char*.
#include<iostream>
using namespace std;
#include <cstring>
class test
{
public:
struct pair
{
int a,b;
};
test(){
pir=new pair;
pir->a=1;
pir->b=2;
}
void readx(char* str)
{
char* str2=new char[sizeof(pair)+1];
str2=reinterpret_cast<char*>(pir);
strncpy(str,str2,sizeof(pair));
}
private:
pair* pir;
};
int main()
{
test t;
char* ptr;
t.readx(ptr);
cout<<*((int*)ptr)<<endl;
cout<<*((int*)ptr+4)<<endl;
return 0;
}
I have tried multiple ways but still failed to return a char* that can be reinterpreted into the struct. Actually the returned char* can only hold the first parameter, losing information about the second parameter, despite that I have used strncpy function. The output of this test code is
1
0
How could I return the everything about struct within the class?
cout<<*((int*)ptr+4)<<endl;
This line should be:
cout<<*((int*)ptr+1)<<endl;
Because you have convert ptr to int *, which +1 means move pointer to next 4 bytes (size of int is 4).
You've got serious problems with your code. If it's really working, it's working only by accident.
Whether it's strncpy or memset you use it doesn't matter too much because you are passing unitialized pointer to the functions which writes to it:
char* ptr;
t.readx(ptr);
Where does ptr point to? It may point almost anywhere and using it as memcpy target is unpredictable. If you are not very unlucky, you will get a segfault/access violation.
Your readx method does some crazy thing:
char* str2 = new char[sizeof(pair)+1];
str2 = reinterpret_cast<char*>(pir);
Here you first alocate some storage and assign its address to str2 then you re-assign it to address of pir. You can never delete[] memory allocated by new[] so you introduce a memory leak.
Constructor of class test allocates memory using new but you don't have corresponding destructor calling delete -> memory leak.
Then there is your ptr+4 problem which was already pointed by the others. Here you can find the code corrected to the point where you can expect it to do what you want.
#include <iostream>
#include <cstring>
using namespace std;
class test
{
public:
struct pair
{
int a, b;
};
test() : pir{ new pair() }
{
pir->a = 1;
pir->b = 2;
}
~test() { delete pir; }
void readx(char* str)
{
memcpy(str, reinterpret_cast<char*>(pir), sizeof(pair));
}
private:
pair* pir;
};
int main()
{
test t;
char* ptr = new char[sizeof(test::pair)];
t.readx(ptr);
cout << *((int*)ptr) << endl;
cout << *((int*)ptr + 1) << endl;
delete[] ptr;
return 0;
}
When you understand the basic issues your code have read about the rule of five. For the test class you would add something like this:
#include <algorithm>
class test
{
//...
test(test const& other) : pir{ new pair() }
{
pir->a = other.pir->a;
pir->b = other.pir->b;
}
test& operator=(test const& other)
{
test copy(other);
swap(*this, copy);
return *this;
}
test(test&& other) noexcept : pir{ nullptr }
{
swap(*this, other);
}
test& operator=(test&& other) noexcept
{
swap(*this, other);
return *this;
}
friend void swap(test& lhs, test& rhs)
{
std::swap(lhs.pir, rhs.pir);
}
};
You are incrementing the int pointer by 4. This points 4 integers ahead. That's not what you want.
cout<<*((int*)ptr+1)<<endl;
That should get you b.
Say I wish to define a member variable in a parent class and set its value in an inherited class. Perhaps these identify functionality available in the class or the nature of the child class. For example:
class A
{
public:
inline int getX() { return x; }
protected:
const int x = 0;
};
class B : public A
{
protected:
const int x = 10;
};
class C : public A
{
protected:
const int x = 50;
};
It should go without saying that scope issues will prevent the above from working properly. However, is there a way to make this work as intended?
Since the variable is meant to identify the nature of the inherited classes, I would prefer if it were const - this problem would not arise if it were not const and merely redefined in the constructor, so far as I can tell.
While fiddling with the compiler trying to make sure my example code made sense, I actually came across the fact that the way I was attempting to define the constants was C++11-specific. That led me to look into the ways it was done before, and I found this question, which shed some light on the matter indirectly.
Defining a variable in this way should be done by having the base class take an argument in its constructor, in the form of:
class A
{
public:
A( const int& type ) : x(type) {}
inline int getX() { return x; }
protected:
const int x;
};
class B : public A
{
public:
B() : A(10) {}
};
class C : public A
{
public:
C() : A(50) {}
};
This will work as intended and allow the constant x to be redefined by inherited classes.
To demonstrate the point I made in my comment, here is an example of what I think you're trying to do (deduced from comments).
I have provided both duck-typed and polymorphic solutions in the same program with a timed run through each.
I use 10 million samples of each to eliminate memory cache noise.
You will notice that the run time of the polymorphic solution is significantly less than that of the duck-typed solution.
#ifdef _WIN32
#include <Windows.h>
double get_cpu_time(){
FILETIME a,b,c,d;
if (GetProcessTimes(GetCurrentProcess(),&a,&b,&c,&d) != 0){
// Returns total user time.
// Can be tweaked to include kernel times as well.
return
(double)(d.dwLowDateTime |
((unsigned long long)d.dwHighDateTime << 32)) * 0.0000001;
}else{
// Handle error
return 0;
}
}
#else
#include <sys/time.h>
inline double get_cpu_time() noexcept {
return (double)clock() / CLOCKS_PER_SEC;
}
#endif
#include <iostream>
#include <vector>
#include <memory>
struct A
{
A(bool copy_) : copy{copy_} {}
virtual ~A() = default;
const bool copy = false;
};
struct RealA : public A
{
RealA() : A { false } {}
};
struct CopyA : public A
{
CopyA() : A { true } {}
};
// A Thing holder will hold any object which has an interface supports do_something_to(T& thing)
struct AHolder {
template<class Thing>
AHolder(std::unique_ptr<Thing> ptr)
: _ptr { std::move(ptr) }
{
}
template<class Thing, class...Args>
static AHolder construct(Args&&...args)
{
return AHolder { std::make_unique<model<Thing>>(std::forward<Args>(args)...) };
}
void do_something() const {
_ptr->do_something();
}
private:
struct concept {
virtual ~concept() = default;
virtual void do_something() = 0;
};
template<class Thing> struct model : concept {
template<class...Args>
model(Args&&...args) : _thing { std::forward<Args>(args)... } {}
private:
void do_something() override {
do_something_to(_thing);
}
Thing _thing;
};
std::unique_ptr<concept> _ptr;
};
using namespace std;
size_t copies_processed = 0;
size_t reals_processed = 0;
void do_something_to(const CopyA&)
{
// simulate work
++copies_processed;
}
void do_something_to(const RealA&)
{
// simulate work
++reals_processed;
}
int main(int argc, char **argv) {
std::vector<std::unique_ptr<A>> duck_typing;
std::vector<AHolder> polymorphic;
constexpr size_t samples = 10000000;
for (size_t i = 0 ; i < samples ; ++i) {
if (i % 2) {
duck_typing.push_back(make_unique<RealA>());
polymorphic.emplace_back(AHolder::construct<RealA>());
}
else {
duck_typing.push_back(make_unique<CopyA>());
polymorphic.emplace_back(AHolder::construct<CopyA>());
}
}
auto duck_start = get_cpu_time();
// nasty duck-typing solution
for (const auto& ptr : duck_typing) {
if (ptr->copy) {
do_something_to(*(static_cast<CopyA*>(ptr.get())));
}
else {
do_something_to(*(static_cast<RealA*>(ptr.get())));
}
}
auto duck_stop = get_cpu_time();
auto poly_start = get_cpu_time();
for (const auto& a_like : polymorphic) {
a_like.do_something();
}
auto poly_stop = get_cpu_time();
cout << "duck typing : " << duck_stop - duck_start << endl;
cout << "polymorphic : " << poly_stop - poly_start << endl;
cout << "copies processed : " << copies_processed << endl;
cout << "reals processed : " << reals_processed << endl;
return 0;
}
sample output :
duck typing : 0.162985
polymorphic : 0.137561
copies processed : 10000000
reals processed : 10000000
I have a type hierarchy similar to the code sample below, and I'm trying to instantiate them via the factory pattern (or, to be pedantic, rather the builder pattern, as my factory takes input from an XML document... but I digress).
However I try to do this, I'm running into problems which I suspect are due to either slicing, if I return by value, or to scoping, if I return by reference.
The below program, for instance, segfaults on the line a.doA() inside C::doStuff(). If I change the call to value_C_factory<C>() to ref_C_factory<C>() instead, I get a couple of warnings to the effect of "returning reference to temporary", but the program compiles, segfaults instead on b.doB() on the next line (without having printed anything from a.doA()...).
The backtrace from gdb looks like this - the second line is the one in my code referred to above
#0 0x00007ffff7dbddb0 in vtable for std::ctype<char> () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#1 0x00000000004010e9 in C::doStuff (this=0x7fffffffdd00) at syntax.cpp:57
#2 0x0000000000400cf2 in main () at syntax.cpp:95
What is causing these segfaults? Is it, as I suspect, slicing/scoping in the value/reference case? If not, what is it? And most importantly, what is a good way to build my instances from the input data?
Code sample
The code below should compile and give the above behavior with e.g. GCC 4.8, using
gcc -g -Wall -std=c++11 -o test test.cpp (that's what I do, anyway).
#include <iostream>
#include <typeinfo>
class IA {
public:
virtual void doA() const = 0;
virtual ~IA() { }
};
class A : public IA {
private:
std::string atask;
public:
explicit A(const std::string &task) : atask(task) {
std::cout << "Created A with task " << atask << std::endl;
}
void doA() const {
std::cout << "I did A! " << atask << std::endl;
}
};
class IB {
public:
virtual void doB() const = 0;
virtual ~IB() { }
};
class B : public IB {
private:
std::string btask;
public:
explicit B(const std::string &task) : btask(task) {
std::cout << "Created B with task " << btask << std::endl;
}
void doB() const {
std::cout << "I did B! " << btask << std::endl;
}
};
class IC {
public:
void doStuff() const;
virtual ~IC() { }
};
class C : public IC {
private:
const IA &a;
const IB &b;
public:
C(const IA &a, const IB &b) : a(a), b(b) { }
void doStuff() const {
a.doA(); // with value factory method, segfault here
b.doB(); // with reference factory, segfault here instead
}
};
template<typename TA>
TA value_A_factory() {
return TA("a value");
}
template<typename TB>
TB value_B_factory() {
return TB("b value");
}
template<typename TC>
TC value_C_factory() {
return TC(value_A_factory<A>(), value_B_factory<B>());
}
template<typename TA>
const TA &ref_A_factory() {
return TA("a ref");
}
template<typename TB>
const TB &ref_B_factory() {
return TB("b ref");
}
template<typename TC>
const TC &ref_C_factory() {
const TC &c(ref_A_factory<A>(), ref_B_factory<B>());
return c;
}
int main() {
C c = value_C_factory<C>();
std::cout << typeid(c).name() << std::endl;
c.doStuff();
}
You have two problems, both caused by undefined behavior.
The first is that you can't return a reference to a local variable. Once the function returns and the local variable goes out of scope and is destructed, what does the returned reference then reference?
The other problem is that you store references to temporary values. When you create your C class like TC(value_A_factory<A>(), value_B_factory<B>()) the values returned by the value_X_factory functions are temporary, and will be destroyed once the complete expression (TC(...)) is done.
In
template<typename TA>
const TA &ref_A_factory() {
return TA("a ref");
}
returning a reference to a local variable is undefined behavior.
In
TC(value_A_factory<A>(), ...)
the lifetime of the value returned by value_A_factory will be the end of the expression TC(...). After that your references in C are dangling.
If you really want to use interfaces and factories for polymorphic types, there is no real alternative to dynamic memory allocation and some kind of ownership scheme. The simplest would be for C to simply assume ownership of its members and take care of their deletion.
#include <memory>
#include <cassert>
struct IA {
virtual void doA() const = 0;
virtual ~IA() { };
};
struct A : IA {
void doA() const override {}
};
struct C {
/* C assumes ownership of a. a cannot be null. */
C(IA* a) : a{a} { assert(a && "C(IA* a): a was null"); };
private:
std::unique_ptr<IA> a;
};
C factory() {
return C{new A};
}
int main()
{
C c = factory();
return 0;
}
Here is some code that is from a great website which does what i want quite well (searching in a vector of class objects by addressing the class objects).
#include <iostream>
#include <vector>
#include <functional>
#include <algorithm>
using namespacestd;
class class1
{
private:
int id;
double value;
public:
class1(int i, double v) :id(i), value(v){ }
int getId()const { return id; }
double getValue() const { return value; }
};
class HasIdentifier :public unary_function<class1, bool>
{
public:
HasIdentifier(int id) : m_id(id) { }
bool operator()(const class1& c)const
{
return (c.getId() == m_id);
}
private:
int m_id;
};
class class2
{
private:
vector <class1> objects;
public:
class2()
{
objects.push_back(class1(1, 100.0));
objects.push_back(class1(2, 100.0));
objects.push_back(class1(3, 100.0));
}
double GetValueOfId(int id)
{
vector<class1>::iterator itElem = find_if(objects.begin(), objects.end(), HasIdentifier(id));
return itElem->getValue();
}
};
int main() {
class2 c;
int id = 4;
cout << id << " " << c.GetValueOfId(id);
cin.get();
return 0;
}
It works well but whenenver i put "int id ">3 it crashes because object only has the size 3. I got this, but is there a possibility to get warned when this will happen so that is does not crash but im able to correct it somehow in the code with a warn message?
You should check the returned iterator for validness and throw and exception (or report an error any other way) if it is invalid:
if (itElem == objects.end())
throw MyVeryCoolException("Woops wrong id");
Dont forget to set up a global exception handler (toplevel catch), otherwise your application will still crash if the exception is uncaught.
I'm having a problem with inheriting from STL set (i think):
Here is class Prime:
class Prime : public set<A> {
private:
// private data members.
public:
// C'tor...
void printParticularA(const int& id);
}
Here is class A:
class A : public List<B>{
private:
// data members.
int id;
public:
// C'tor
A(const A& copy) : List<B>(copy), //copy data members
{ // validate data and throw exceptions if needed. };
bool operator< (const A& rhs) const{
return id < rhs.id;
}
void printReport() const {
for(const B& item : *this){ item.print(); }
}
}
now here is the problem. in the next function i want to print a particular A object in the set:
void Prime::printParticularA(const int& id) {
find(AFinder(id))->printReport();
}
i also tried this:
void Prime::printParticularA(const int& id) {
*(find(AFinder(id))).printReport();
}
note: assume that class B has print() method.
note2: AFinder is a class for making dummy A objects using only the id data.
the problem is that when 'find' finds the objects it returns const_iterator (because every object in set is const), and when i dereference it i get a copy of the object (??) but the list of B inside it is empty!
this happens also for the '->' version.
now i know that set doesn't allow me to change the objects but i do not intend to change the object (as you can see in the declaration of printReport member function).
i appreciate any help in this!
EDIT: thanks everyone, you have helped me a lot especially learning what not to do.
I solved the problem and it wasn't in set, list nor any of my classes presented here.
my mistake was in understanding the question i was given (yes this is my homework assignment, i'm still new to c++).
sorry if you feel i have wasted your time.
i hope that i can learn from all of your experience and someday help others!
in short, THANKS!! :)
Your code is pretty much messed up all around. And the problem does not look tied directly to set or iterator, but general bad things done.
For starters make your op< const and delete the copy ctor for good -- the stock one shall work fine. Use internal find of set to search and look if it found an item.
All that will likely make your described problem gone, if not, post a complete compileable example with proper text of what you see and what you expect.
Your code really violate many rules of C++. Why don't you try smth like this:
#include <iostream>
#include <list>
#include <map>
using namespace std;
class SimpleInt {
public:
int data_;
SimpleInt(const int data): data_(data) {};
void print() const {cout << data_ << " ";};
};
template <typename T>
class A {
private:
// private data members.
public:
list<T> list_of_B_; // this is for simlicity. Make getters as you need
const int id_; // delete it or copy to map. You sholdn't change it.
A(int id) : list_of_B_(), id_(id) {}
A(const A<T>& copy) : list_of_B_(copy.list_of_B_), id_(copy.id_) {} //copy data members
A(A<T>&& copy) : list_of_B_(::std::move(copy.list_of_B_)), id_(copy.id_) {} //move data members
void printReport() const {
for(const T& item : list_of_B_){ item.print(); }
}
};
template <typename T>
class Prime {
private:
// private data members.
public:
// The main difference with your source
map<int, T> map_of_A_; // this is for simlicity. Make getters as you need
// C'tor...
void printParticularA(const int& id) {
auto it = map_of_A_.find(id);
if (it != map_of_A_.end())
it->second.printReport();
}
};
int _tmain(int argc, _TCHAR* argv[])
{
typedef A<SimpleInt> ASimpled;
Prime<ASimpled> prime;
ASimpled a(1);
a.list_of_B_.push_back(SimleInt(1));
a.list_of_B_.push_back(SimleInt(2));
a.list_of_B_.push_back(SimleInt(3));
ASimpled b(2);
b.list_of_B_.push_back(SimleInt(10));
b.list_of_B_.push_back(SimleInt(20));
b.list_of_B_.push_back(SimleInt(30));
prime.map_of_A_.insert(make_pair(a.id_, a));
prime.map_of_A_.insert(make_pair(b.id_, b));
prime.printParticularA(2);
return 0;
}
Although you haven't included the implementation of List the problem is likely there. To be more precise the begin() and end() member functions of List may be broken. Chances are the values they return are identical (or invalid) resulting in the range based for loop doing nothing. This of course is based on your set::find is returning a valid iterator and not the end iterator.
The following example is a modification of the code in your question. It uses std::list instead of List and doesn't use AFinder since you haven't included the code for it.
#include <set>
#include <list>
#include <iostream>
struct B
{
int id_;
explicit B(int id) : id_(id) {}
void print() const
{
std::cout << "B::id = " << id_ << std::endl;
}
};
class A : public std::list<B>
{
public:
explicit A(int id) : id_(id) {}
bool operator<(const A& rhs) const
{
return id_ < rhs.id_;
}
bool operator==(const A& other) const
{
return id_ == other.id_;
}
void printReport() const
{
for(auto& item : *this)
{
item.print();
}
}
private:
// data members.
int id_;
};
class Prime : public std::set<A>
{
public:
void printParticularA(const int& id)
{
std::cout << "finding " << id << std::endl;
auto el = find(A(id));
if(el == cend())
{
std::cout << "not found" << std::endl;
}
else
{
find(A(id))->printReport();
}
std::cout << "done finding " << id << std::endl;
}
};
int main()
{
Prime p;
A a1(1);
a1.push_back(B(1));
a1.push_back(B(2));
a1.push_back(B(3));
p.insert(a1);
A a2(2);
a2.push_back(B(4));
a2.push_back(B(5));
a2.push_back(B(6));
p.insert(a2);
p.printParticularA(1);
p.printParticularA(2);
// doesn't exit
p.printParticularA(3);
}
This produces the following output.
finding 1
B::id = 1
B::id = 2
B::id = 3
done finding 1
finding 2
B::id = 4
B::id = 5
B::id = 6
done finding 2
finding 3
not found
done finding 3