From owner-freebsd-hackers@FreeBSD.ORG Wed Feb 24 14:25:16 2010 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id EBA65106566C for ; Wed, 24 Feb 2010 14:25:16 +0000 (UTC) (envelope-from max@love2party.net) Received: from moutng.kundenserver.de (moutng.kundenserver.de [212.227.17.10]) by mx1.freebsd.org (Postfix) with ESMTP id 820ED8FC1A for ; Wed, 24 Feb 2010 14:25:16 +0000 (UTC) Received: from vampire.homelinux.org (dslb-088-066-028-210.pools.arcor-ip.net [88.66.28.210]) by mrelayeu.kundenserver.de (node=mrbap0) with ESMTP (Nemesis) id 0MVb0p-1OGU9l06xT-00Ydaz; Wed, 24 Feb 2010 15:25:14 +0100 Received: (qmail 91410 invoked from network); 24 Feb 2010 14:25:13 -0000 Received: from f8x64.laiers.local (192.168.4.188) by laiers.local with SMTP; 24 Feb 2010 14:25:13 -0000 From: Max Laier Organization: FreeBSD To: freebsd-hackers@freebsd.org Date: Wed, 24 Feb 2010 15:25:11 +0100 User-Agent: KMail/1.12.4 (FreeBSD/8.0-RELEASE-p2; KDE/4.3.5; amd64; ; ) References: <983a1cf21002240544s59006035ifbf0ef7eb045e44f@mail.gmail.com> In-Reply-To: <983a1cf21002240544s59006035ifbf0ef7eb045e44f@mail.gmail.com> MIME-Version: 1.0 Content-Type: Text/Plain; charset="iso-8859-15" Content-Transfer-Encoding: 7bit Message-Id: <201002241525.11930.max@love2party.net> X-Provags-ID: V01U2FsdGVkX1/SD62JulL38UeQz6xv6hkyLr46RSwgZaNBQqs Ix5Di/LKvlr7y4AGJSgUjhqDeJ4bHAfdntmxtuq7m/b77fk3xG AYZkKju0QSmy8hEYyJDpQ== Cc: Andrey Zonov Subject: Re: 2 bytes allocated problems X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 24 Feb 2010 14:25:17 -0000 On Wednesday 24 February 2010 14:44:35 Andrey Zonov wrote: > Hi, > > When I try allocated pointer to a pointer, and in it some pointers > (important: size is 2 bytes), the pointers lose their boundaries. > Why it can happen? > > Test program in attach. Your test program is broken: >#define S1 "ab" >#define S2 "cd" > > pp = (char **) Malloc(2 * sizeof(char *)); > > pp[0] = (char *) malloc(2); > memcpy(pp[0], S1, 2); > pp[1] = (char *) malloc(2); > memcpy(pp[1], S2, 2); > > printf("%s\n", *pp); > printf("%s\n", pp[0]); > printf("%s\n", pp[1]); Why should *pp == pp[0], or pp[1] be a nul-terminated string? You just copied the two characters. It's pure luck if there is a \0 at the end of any of these elements, or that the access doesn't cause a SEGV. If you do: > pp[0] = (char *) malloc(3); > memcpy(pp[0], S1, 3); > pp[1] = (char *) malloc(3); > memcpy(pp[1], S2, 3); instead, you copy the termination and things work as expected. Regards, Max