Problems with dynamic Memory

Hello again,

now i have some trouble with the dynamic allocation of memory...i.e.:
I want to create a class, that contains a variable sized field of numbers and a field containing the length of the field....say
1
2
3
4
5
6
7
8
9
10
11
class dynmem {

	double *vkt;
	int len;
public:
		// Constructor
		dynmem();
	       dynmem(int, double*);
              //Destructor
		~dynmem();
        /*etc. pp........................*/


I create an operator for my class that adds the elements of two classmembers (as long as index of the smaller not exceeded) and makes a new "dynmem"-object with the size of the bigger one (field are filled).

The operator looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
dynmem*  dynmem:: operator +(dynmem as){
			dynmem out;
			//nun hier die Felder hernehmen:
			int len1=getLen();
			int len2 = as.getLen();
			int lan=(int)max((double)len1, (double)len2);
			double *vals=new double[lan];
			for (int l=0; l<len1; l++){
				vals[l]=getVals()[l] + as.getVals()[l];
			}
			for(int l=len1; l<lan; l++){
				if(len1<len2){vals[l] = as.getVals()[l];}
				else{vals[l] = getVals()[l];}
			}
			out.setVals(lan, vals);
			return &out;
		}


So, it returns a pointer to the newly created "dynmem" object.....
however, it doesn't work properly. It returns such pointer, but afterwards, I can't dereferenciate it.....any idea ??
It doesn't work if it tries to return a "dynmem"-object, so i decided to let it return a pointer to such an object....

p.s.: My efforts to dereferenciate...
 
f = (*out).getVals()[7];....

 
f = out->getVals()[7];


Is my problem that my object has yet been deleted ? However, if it's so, why doesn't it work, if i try to return an object and not a pointer ?
Could the Constructor or the destructor be the problem ?

Maybe i'll find out on my own....but it would take long i guess...

Greetings and Thanks so far for reading this. I am thankful for any hint.

out is an local object on the stack. It gets invalid as soon as the function (operator +) ends.

Either you allocate out with new or you return a copy.
If you return a copy you need to provide at least the copy constructor dynmem(const dynmem &dm)
So if i do so, say i put
1
2
dynmem* out;
out = new dynmem
instead of
dynmem out,
out will be a pointer the object i want to create....thus i change my code according to that and it should work ?
I am going to try that.....however, what is wrong with this way, but returning
out
instead of &out ? That does not work either, and i don't know why. That's the "copy thing" maybe....

Anyway, thanks a lot for your reply.
Last edited on
First of all: you must not return a pointer to a local object because it is destroyed when the function ends.

The destructor of dynmem is called and it's supposed to destroy it's internal data vkt

if you write it like so:
1
2
3
4
5
dynmem  dynmem:: operator +(dynmem as){ // Note: no * as return
			dynmem out;
...
			return out; // Note: no &
		}


you need a copy operator like so:
1
2
3
4
5
6
7
8
9
void dynmem::operator=(const dynmem &dn)
{
  // if setVals already does this job, just call it here
  if(vkt != NULL)
    delete[] vkt;
  vkt = new double[dn.len];
  memcpy(vkt, dn.vkt, dn.len * sizeof(*vkt));
  len = dn.len;
}

The copy constructor may look like so:
1
2
3
4
dynmem::dynmem(const dynmem &dn)
{
  operator=(dn);
}

In your class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class dynmem {

	double *vkt;
	int len;
public:
		// Constructor
		dynmem();
	       dynmem(int, double*);
	       dynmem(const dynmem &dn);
              //Destructor
		~dynmem();
              //copy operator
	       void operator=(const dynmem &dn);
        /*etc. pp........................*/


By the way your operator + should look like this:
dynmem dynmem:: operator +(const dynmem &as){ // As a reference, otherwise it's a copy

Your destructor should look like this:
1
2
3
4
5
dynmem::~dynmem()
{
  if(vkt != NULL)
    delete[] vkt;
}
Otherwise you will have a memory leak. With this a shallow copy (without a copy operator) will lead to a crash since you copy just the internal pointer which will destroyed when the life time of the local copy of your object (like in operator +(dynmem as)) ends

It is essential to know about shallow vs. deep copy, lifetime of objects, and memory leaks
Hello,
i just read about this "copy constructor", so i think i am able to understand what you are saying.....and my destructor looks like this....fine, or ?

I got my "operator" working already, but i think it might be much better the way you suggested.

Thanks again,

fluppe
Topic archived. No new replies allowed.