"Ramprasad A Padmanabhan" <> wrote in
message news:Vq5Kd.32$...
> In my code below I have an array class where I am trying to add two arrays
> using "+" .
> I am not sure why I get a '0' always for the first element of the result
>
> My code ( slightly longish , I hope you would bear with a newbie )
Hi, allow me to make some comments along the way...
> --------------------------
> using namespace std;
> #include <iostream>
NB: the 2 previous lines should be swapped to compile without error
> #define MAXARRAYSIZE 500
In C++, we like to use enum { x = 500 }; or const int x = 500;
instead of macros, as these alternatives will respect C++ scopes.
> class MyArr {
> public:
> int *data,length;
(Many will prefer two separate declarations for clarity.)
> MyArr(){
> data = new int[0];
> length = 0;
While it is legal to allocate a 0-size array, why not
use a NULL pointer value instead?
> }
>
> ~MyArr(){
> if(length) free(data);
1) Based on the constructor, you will leak memory if length=0.
But it will be ok if you initialize data with NULL instead
(and you can omit the if(length) test anyway).
2) Memory allocated with new[] must be freed with delete[] :
delete[] data;
> }
>
> void newFill(int *d,int l,bool freeD = true){
> length = l;
> if(freeD) free(data);
> data = (int*) malloc(sizeof(int)*(length +2));
1) be consistent: use new int[length] here as well.
2) For exception/error safety, it is usually a good
idea to allocate the new memory before freeing
the previous data.
3) Consider using 'const': because the array pointed
to by d is not modified by the function, best
would be to declare the parameter as: int const* d
> for(int i=0;i<length;i++) data[i] = d[i];
> }
>
> void dispArr() {
> for(int i=0;i<length;i++) cout << data[i] <<"\t";
> cout << "\n";
> }
>
> MyArr operator + ( const MyArr &r){
> int a[length];
This array of a non-const dimention is not legal in ISO C++ 98
(although its has become legal in ISO C'99, and probably will
at some point be included in standard C++ as well).
Why not first allocate the memory for 'ret', and then
compute its new contents in place ?
> MyArr ret;
You probably need to check that (*this) and (r) have
compatible lengths !
> for(int i=0;i<length;i++) a[i]= data[i] + (r.data)[i];
> ret.newFill(a,length);
> return(ret);
> }
The problem with your class is that it lacks a copy-constructor
(and an assignment operator):
MyArr( MyArr const& orig );
MyArr& operator=( MyArr const& orig );
This is what causes the error
you observe in your test code.
> };
Rather than fixing the errors in your class, the best would
really be to use std::vector instead of a naked array.
This will allow the compiler to automatically and correctly
generate the additional member functions you need:
class MyArr {
public:
std::vector<int> data;
MyArr() : data () {}
MyArr(int count) : data(count) {}
// the default-generated destructor and copy-ctr/op are ok
// same as before, uses data.size() instead of length :
void dispArr() {
for(int i=0;i<data.size();i++) cout << data[i] <<"\t";
cout << "\n";
}
MyArr operator + ( const MyArr &r)
{
assert( r.data.size() == this->data.size() );
MyArr ret(this.length);
for(int i=0;i<data.size();++i)
ret.data[i] = this->data[i] + r.data[i];
return ret;
}
};
Finally, the class you are writing looks very much like
std::valaray<int>, available in the standard C++ library.
Alternatively, you may also want to consider using
a template parameter to specify the size of the array
(and even the element type)... but that's another story.
I hope this helps,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <>
http://www.brainbench.com