From: brunobg AT geocities DOT com (Bruno Barberi Gnecco) Newsgroups: comp.os.msdos.djgpp Subject: Re: Trouble with many mallocs and reallocs Date: 10 Aug 1998 15:24:02 -0500 Organization: Newscene Public Access Usenet News Service (http://www.newscene.com/) Lines: 50 Message-ID: <35cf519a.8496651@news3.newscene.com> References: To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Precedence: bulk On Mon, 10 Aug 1998 09:39:50 +0300 (IDT), Eli Zaretskii wrote: >On 9 Aug 1998, Bruno Barberi Gnecco wrote: >> Debugging it, I find that the error is exactly at the line that >> allocates memory to a new line (it allocates 1 byte). I'm running >> the Windows 95 DPMI server. What may be the problem? >You need to supply more information to get meaningful replies. At the >very least, explain what is that ``error exactly at the line that >allocates memory to a new line''. Please describe what is the problem >that you see under a debugger. The node is: struct node { struct node *forward, *back; unsigned char *data; }; typedef struct node NODE; and the trouble is at int insert_node( unsigned char *p, int d) { node *nn_p; unsigned char *nd_p; nn_p = ( NODE * ) malloc ( sizeof ( NODE )); nd_p = ( unsigned char * ) malloc ( strlen(p) + 1 ); p value was '\0', and I still can't see any special reason to it happen. I tried to type a line over 1kb in length, and it worked perfectly, but 20 lines of 3 characters didn't. The reallocation is OK. I debugged it step by step twice, and these particular times the trouble happened at the nn_p line, returning a SIGABRT, and in another point, also returning a SIGABRT. I realy don't believe is lack of RAM, because I used less than 1kb in variables, and the .EXE is 195kb length (unoptimized and with debugging info). >> Is there a better way to allocate memory? >Yes. The usual method is to allocate memory for the buffer, and then >maintain pointers into that buffer which point to the beginning of >each line. There are other methods as well. For example, an editor >could be totally oblivious to the line-oriented structure of text, and >instead treat it as a stream of bytes. I'm doing the first method; I first allocate a temporary buffer, then, if everything is alright, pass the address to the node.data.ú "There's never enough time to do all the nothing you want" Bill Watterson Bruno Barberi Gnecco ICQ #1383173 - PGP 5.0i user My other OS is Linux -=- http://graphx.home.ml.org -=- Electric Eng, Poli