Is this code correct?

  • Thread starter Christopher Benson-Manica
  • Start date
C

Christopher Benson-Manica

#include <map>
#include <vector>
#include <iostream>

using namespace std;

class test
{
public:
unsigned int data;
vector<unsigned int> *v;
test() {data=0; v=new vector<unsigned int>();}
~test() {delete v;}
};

int main(void)
{
map< unsigned int, test > m;

cout << "I hate my implementation!\n";
m[0].data=3;
m[1].data=4;
cout << m[0].data << endl;
return 0;
}

This code hangs after printing the first line when compiled with
Borland 4. It works like a charm when compiled with g++ 3.3.1. Am I
doing something wrong here that invokes undefined behavior, or is
Borland 4 displaying its utter crappiness yet again? If it's the
implementation, can any one suggest a way to work around this?
 
A

Alf P. Steinbach

* Christopher Benson-Manica said:
#include <map>
#include <vector>
#include <iostream>

using namespace std;

class test
{
public:
unsigned int data;
vector<unsigned int> *v;
test() {data=0; v=new vector<unsigned int>();}
~test() {delete v;}
};


Main _technical_ problem with above class is that it doesn't define
copy operations. So a pointer is copied. And hence multiply deleted by the
destructor, which is Undefined Behavior (crash, hang, e-mail to Bush).

You can see that by inserting


private:
test( test const& );
test& operator=( test const& );


at the top and compiling.

To fix it define these operations yourself, or let v simply be a vector
instead of a pointer to a vector.

There are other problems relating to the lack of design, but ask about that
separately.

int main(void)

void is a C'ism.

{
map< unsigned int, test > m;

cout << "I hate my implementation!\n";

You have Undefined Behavior; the C++ implementation is not to blame.

m[0].data=3;
m[1].data=4;
cout << m[0].data << endl;
return 0;
}
 
K

Karl Heinz Buchegger

Christopher said:
#include <map>
#include <vector>
#include <iostream>

using namespace std;

class test
{
public:
unsigned int data;
vector<unsigned int> *v;
test() {data=0; v=new vector<unsigned int>();}
~test() {delete v;}
};

To answer your question:
No it is not correct.
This class lacks a correct copy constructor and assignment operator.
This code hangs after printing the first line when compiled with
Borland 4. It works like a charm when compiled with g++ 3.3.1. Am I
doing something wrong here that invokes undefined behavior, or is
Borland 4 displaying its utter crappiness yet again? If it's the
implementation, can any one suggest a way to work around this?

2 possibilities:

* Why is v a pointer to a vector?
As far as I can see, there is no need for it to be so. So drop
the pointer and make it a plain vanilla vector

class test
{
public:
unsigned int data;
vector<unsigned int> v;
test() {data=0}
};

and everything should be fine

* implement a copy constructor and assignment operator
 
C

Christopher Benson-Manica

Alf P. Steinbach said:
Main _technical_ problem with above class is that it doesn't define
copy operations. So a pointer is copied. And hence multiply deleted by the
destructor, which is Undefined Behavior (crash, hang, e-mail to Bush).

I figured it was me...
You have Undefined Behavior; the C++ implementation is not to blame.

....which is why I asked. Lord knows it *has* been the implementation
plenty of times. Thanks.
void is a C'ism.
Sorry.

There are other problems relating to the lack of design, but ask about that
separately.

Well, considering it was just a minimum compilable example, I'm not
sure how it could demonstrate design at all, but I'm listening all the
same.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,187
Latest member
RosaDemko

Latest Threads

Top