From owner-freebsd-questions@FreeBSD.ORG Tue Aug 12 16:09:18 2008 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 38A00106568B for ; Tue, 12 Aug 2008 16:09:18 +0000 (UTC) (envelope-from keramida@ceid.upatras.gr) Received: from igloo.linux.gr (igloo.linux.gr [62.1.205.36]) by mx1.freebsd.org (Postfix) with ESMTP id B662D8FC13 for ; Tue, 12 Aug 2008 16:09:17 +0000 (UTC) (envelope-from keramida@ceid.upatras.gr) Received: from kobe.laptop (adsl100-81.kln.forthnet.gr [77.49.107.81]) (authenticated bits=128) by igloo.linux.gr (8.14.3/8.14.3/Debian-5) with ESMTP id m7CG94x4015178 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NOT); Tue, 12 Aug 2008 19:09:10 +0300 Received: from kobe.laptop (kobe.laptop [127.0.0.1]) by kobe.laptop (8.14.2/8.14.2) with ESMTP id m7CG93nX057431; Tue, 12 Aug 2008 19:09:03 +0300 (EEST) (envelope-from keramida@ceid.upatras.gr) Received: (from keramida@localhost) by kobe.laptop (8.14.2/8.14.2/Submit) id m7CG91l1057430; Tue, 12 Aug 2008 19:09:01 +0300 (EEST) (envelope-from keramida@ceid.upatras.gr) From: Giorgos Keramidas To: jordi@cdmon.com References: <48A1A613.5020407@cdmon.com> Date: Tue, 12 Aug 2008 19:09:01 +0300 In-Reply-To: <48A1A613.5020407@cdmon.com> (Jordi Moles Blanco's message of "Tue, 12 Aug 2008 17:02:43 +0200") Message-ID: <87fxpab4gy.fsf@kobe.laptop> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.0.60 (berkeley-unix) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-MailScanner-ID: m7CG94x4015178 X-Hellug-MailScanner: Found to be clean X-Hellug-MailScanner-SpamCheck: not spam, SpamAssassin (not cached, score=-3.824, required 5, autolearn=not spam, ALL_TRUSTED -1.80, AWL 0.57, BAYES_00 -2.60) X-Hellug-MailScanner-From: keramida@ceid.upatras.gr X-Spam-Status: No Cc: freebsd-questions@freebsd.org Subject: Re: error allocating memory with realloc(). how can i increase max_allowed in the system? X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 12 Aug 2008 16:09:18 -0000 On Tue, 12 Aug 2008 17:02:43 +0200, Jordi Moles Blanco wrote: > Hi, > > i'm running a FreeBSD 7.0 amd64 machine and struggling with some C > code i'm writing. > > I've had some trouble with this home-made script as it keeps crashing > while launching a "realloc()" call. > > I narrowed down the problem and here i'm sending you a short example of > code that crashes: > > ************* > #include > #include > > int main() > { > > int midataula; > > midataula = 3000; > > char *missatge = (char *)malloc(midataula * sizeof(char)); > > missatge[0]='h'; > missatge[1]='o'; > missatge[2]='l'; > missatge[3]='a'; > > printf("\n\ntaula1: %s",missatge); > > int voltes; > voltes = 0; > > while(voltes<4) > { > midataula = midataula+500; > realloc(missatge, midataula * sizeof(char)); > voltes++; > } There's your problem. realloc() works fine, but it *returns* the new pointer; it does _not_ modify missatge "in place". The program should work fine if you use size_t for midataula (it is the 'size' of an array, which may not necessarily fit in an 'int'), and if you use realloc() correctly, as in: #include #include size_t midataula; char *missatge; /* * DON'T cast the result of malloc(). It may 'hide' the bug of * a missing include, and cause troubles when * malloc() is implicitly defined by the compiler as: * * int malloc(...); * * On a 64-bit machine converting a 64-bit pointer to `int' will * lose the high-order 32 bits of the address, and you will try * to access unexpected memory areas. */ midataula = 3000; missatge = malloc(midataula * sizeof(*missatge)); if (missatge == NULL) err(1, "malloc"); Then when you use realloc() keep both midataula and missatge in temporary copies until you are sure that realloc() worked: while (voltes < 4) { char *tmp; size_t newsize; newsize = midataula + 500; tmp = realloc(missatge, newsize * sizeof(*missatge)); if (tmp == NULL) err(1, "realloc"); /* * Now that you know the resize has succeeded, update * midataula and missatge. realloc() is allowed to * relocate missatge. See the following note in its * manpage: * * Note that realloc() and reallocf() may move the * memory allocation, resulting in a different return * value than ptr. */ midataula = newsize; missatge = tmp; } Right now you are calling realloc() as: realloc(missatge, newsize * sizeof(*missatge)); and throwing away the resulting pointer. The first time that realloc() discovers that the `resized' vector cannot fit in its original location, it relocates the array, and returns the new location. You throw away that location and your next iteration through the loop tries to access an invalid (already freed) memory region. That's what causes your segmentation fault.