Mail Archives: djgpp/1998/08/26/18:18:15
Mike Rusch wrote:
> In my program, I define a structure reptfmt as follows:
>
> struct reptfmt {
> char numfields;
> char numsorts;
> char **fnames;
> fieldinfo *fields;
> sortinfo *sorts;
> char groups;
reptfmt& operator = (reptfmt& a);
> } ;
How do you initialize this data?
> Sometimes I need a copy of a reptfmt structure. Since var1 = var2
> doesn't make copies of the data pointed to by fnames, fields, and sorts,
> I had to write a special function called "reptfmtcpy":
Since you are using C++ (new doesn't work in C), you could override the '=' operator
> void reptfmtcpy(reptfmt &dest, const reptfmt src) {
> dest = src;
>
> dest.fields = new fieldinfo[dest.numfields];
> dest.fnames = new (char *)[dest.numfields];
> if(dest.fields != NULL && dest.fnames != NULL) {
> for(int i = 0; i < dest.numfields; i++) {
> dest.fields[i] = src.fields[i];
> // ---THE NEXT LINE IS LINE 1292---
> effstrcpy(dest.fnames[i], src.fnames[i]);
> }
> }
> dest.sorts = new sortinfo[dest.numsorts];
> if(dest.sorts != NULL)
> for(int i = 0; i < dest.numsorts; i++)
> dest.sorts[i] = src.sorts[i];
> }
'new' will never return 'NULL'. If it runs out of memory, it will end your program and write a message.
> So, what do I do? From the traceback it almost looks like "new" calls
> malloc, but this seems strange to me since new is supposed to be a
> keyword, and supposedly more efficient than malloc. I suppose I could
> use malloc instead, but then I'd have to change all my new's to malloc's
> and my delete's to free's. Plus, who knows if malloc would work any
> better?
All new does, is call malloc, then call the constructor for the object you are creating.
--
(\/) Endlisnis (\/)
s257m AT unb DOT ca
Endlisnis AT GeoCities DOT com
Endlis AT nbnet DOT nb DOT ca
- Raw text -