delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/08/26/18:18:15

From: Endlisnis <s257m AT unb DOT ca>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: keyword "new" causes SIGSEV
Date: Tue, 25 Aug 1998 12:31:04 -0300
Organization: NBTel Internet
Lines: 62
Message-ID: <35E2D8B7.9ECC9F7B@unb.ca>
References: <35E1C842 DOT 45EFF366 AT newnorth DOT net>
NNTP-Posting-Host: fctnts10c69.nbnet.nb.ca
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019