| delorie.com/archives/browse.cgi | search |
| From: | Martin Str|mberg <ams AT father DOT ludd DOT luth DOT se> |
| 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 <kos AT kbtem DOT by> wrote:
: Hi all!
: I have a code:
: #include <stdio.h>
: #include <stdlib.h>
: #include <conio.h>
: #include <string.h>
: 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
| webmaster | delorie software privacy |
| Copyright © 2019 by DJ Delorie | Updated Jul 2019 |