Topic: Help me figuring out these functions


Author: wti535@mail.ncku.edu.tw (Tzone-I-Wang)
Date: 15 May 92 16:27:50 GMT
Raw View
Hi
    Is there any one who can help me finding out the correct
types of the arguments in these two functions listed below? Or
anyony who can provide me the E-mail address of the author of
the book "C++ Techniques & Applications by Scott Robert Ladd"?

 void * _vec_new_(
  void *       aptr,
  unsigned int num,
  size_t       size,
  void *       (*ctor)(void*));

 void _vec_delete_(
  void *       aptr,
  unsigned int num,
  size_t      size,
  int          (*dtor)(int,void *)
  int          freeup);

  These two functions are mentioned in the above book in page
181-182. They are the functions called by the overloaded
operator new and delete(in BC++, they have the name _vector_new_
and _vector_delete_).

  A sample program is also listed below. Could you tell me
what's wrong with it?

extern "C" {
 #include "stddef.h"
 #include "stdlib.h"
        #include <conio.h>
 }
#include <iostream.h>

void * operator new (size_t size);

void operator delete (void * ptr);

extern "C" {
 void * _vec_new_(
  void *       aptr,
  unsigned int num,
  size_t       size,
  void *       (*ctor)(void*));

 void _vec_delete_(
  void *       aptr,
  unsigned int num,
  size_t      size,
  int          (*dtor)(int)
  int          freeup);
 }
void * operator new (size_t size)
 {
 void * temp = malloc(size);
 cout << "new called\n";
 return temp;
 }

void operator delete (void * ptr)
 {
 free(ptr);
 cout << "delete called\n";
 }

void * _vector_new_(
 void *      aptr,
 unsigned int num,
 size_t      size,
 void *      (* ctor)(void *))
 {
 aptr = malloc(num * size);

 if ((ctor != NULL) && (aptr != NULL))
  {
  for (unsigned int n = 0; n < num; ++n)
   ctor((char*)aptr+n*size);
  }
 cout << "_vec_new called\n";
 return aptr;
 }

void _vector_delete_(
 void *       aptr,
 unsigned int num,
 size_t      size,
 void         (*dtor)(int,void *)
 int          freeup)
 {
 if (aptr == NULL)
  return;

 if (dtor != NULL)
  {
  for (unsigned int n = 0; n < num; ++n)
   dtor(2,(char*)aptr+n*size);
  // I don't understand why 2 in the first argument?
  }
 cout << "_vec_delete called\n";
 if (freeup)
  free(aptr);
 }

class three_d
{
public:
 int x,y,z; //three dimension coordinates
 three_d(int a, int b, int c); //constructor
 three_d(void) //needed for arrays
  {
  cout << "constructing\n";
  }
 ~three_d()
  {
  cout << "destructing\n";
  }
 void print()
  {
  cout << x << ", ";
  cout << y << ", ";
  cout << z << "\n";
  }
};

three_d::three_d(int a, int b, int c)
{
cout << "constructing\n";
x=a;
y=b;
z=c;
}

int main(void)
{
three_d *p;
int i;
const int N=5;
clrscr();

p=new three_d[N];
if (!p){
 cout << "allocation failure\n";
 return 1;
 }
for (i=0; i<N; i++)
 {
 p[i].x=i+1;
 p[i].y=i+2;
 p[i].z=i+3;
 }
for (i=0; i<N; i++)
 p[i].print();
delete [N] p;
return 0;
}

Many Thanks for you!




Author: jimad@microsoft.com (Jim ADCOCK)
Date: 26 May 92 19:18:14 GMT
Raw View
In article <1992May15.162750.13861@dec8.ncku.edu.tw> wti535@mail.ncku.edu.tw (Tzone-I-Wang) writes:
|  A sample program is also listed below. Could you tell me
|what's wrong with it?

What's wrong is that you're trying to overload compiler-implementation
details.   Don't do that, stick to programming "in" *the language* instead.