From: Martin Str|mberg Newsgroups: comp.os.msdos.djgpp Subject: Re: Why so? Date: Thu, 26 Jul 2001 13:06:44 +0000 (UTC) Organization: University of Lulea, Sweden Lines: 58 Message-ID: <996152804.153539@queeg.ludd.luth.se> References: <9jp3tu$vb6c$1 AT ID-89475 DOT news DOT dfncis DOT de> X-Trace: news.luth.se 996152804 11720 130.240.16.109 (26 Jul 2001 13:06:44 GMT) X-Complaints-To: abuse AT luth DOT se User-Agent: tin/pre-1.4-981225 ("Volcane") (UNIX) (SunOS/4.1.4 (sun4m)) Cache-Post-Path: queeg.ludd.luth.se!unknown AT father DOT ludd DOT luth DOT se X-Cache: nntpcache 2.4.0b5 (see http://www.nntpcache.org/) To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com Sergey Kovalev wrote: : Hi all! : I have a code: : #include : #include : #include : #include : int main() : { : char *str; : str=(char*)malloc(10); Here you allocate 10 bytes and let str point to those bytes. : clrscr(); : str="01234"; : puts(str); Here you let str point at the constant string "01234". You just leaked 10 bytes of memory. : printf("%s\n",str); : strcat(str,"567"); Here you modify element number 5 (== '\0' == nul) and write three characters beyond "01234". Note that even if you only did ``strcat(str,"5");'' you would try to modify a readonly string, which isn't allowed. : puts(str); : printf("%s\n",str); : return 1; : } : Why this code produces following results? : 01234 : 01234 : 01234567 : 67 : Other compilers give me: : 01234 : 01234 : 01234567 : 01234567 Only (bad) luck. Right, MartinS