Mail Archives: djgpp/1999/09/16/17:52:09
// I am having a problem with the new malloc.c.
//When one uses the new malloc.c via the include statement
//at MARK below the program compiles but does not run. When one
//removes the include statement, the program uses the old malloc
//(? malloc.h located in c:\djgpp\include), compiles and runs properly.
//Note: The new malloc.c may reside in some other directory
//than c:\djgpp\src\libc\stdlib on your machine (or, by now, it
//may be part of the Delorie distribution of its
//port of gnu to DOS, in which case you may not have the
//old malloc)
//The problem occurs only in connection with overloading an operator
//(such as +) to define how + applies to the members of a user
//defined class (zvector in the example below).
//Of course, it is possible that I am making some other error.
//If so, please tell me.
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <math.h>
//#include "c:\djgpp\src\libc\ansi\stdlib\malloc.c" //MARK
const int length=10;
const int lengthpad=length+0xa; //padding in memory allocation
class zvector {
public:
unsigned int *s;
zvector();
~zvector();
};
zvector::zvector() //constructor for realmp
{s = new unsigned int [lengthpad];}
zvector::~zvector() //destructor for realmp
{delete[] s;}
zvector e;
zvector f;
zvector g;
zvector w1;
zvector w8;
zvector w9;
extern zvector operator+(zvector w8,zvector w9);
zvector operator+(zvector w8,zvector w9)
{ int zi;
for (zi=1;zi<length+1;zi++)
{w1.s[zi+1]=w8.s[zi+1] + w9.s[zi+1];
}
return w1;
}
main()
{ int k,qqq;
for (k=1;k<length+1;k++)
{e.s[k+1]=k;
f.s[k+1]=k+1;
}
cout<<"add the following two vectors"<<'\n';
for (k=1;k<length+1;k++) cout<<e.s[k+1]<<" ";
cout<<'\n';
for (k=1;k<length+1;k++) cout<<f.s[k+1]<<" ";
cout<<'\n';
cout<<'\n';
g=e+f;
cout<<"the result is"<<'\n';
for (k=1;k<length+1;k++) cout<<g.s[k+1]<<" ";
cout<<'\n';
cout<<'\n';
g=f+e;
cout<<"check that the overloaded + operator is commutative"<<'\n';
for (k=1;k<length+1;k++) cout<<g.s[k+1]<<" ";
return 0;
}
- Raw text -