From owner-freebsd-questions@FreeBSD.ORG Tue Aug 12 16:22:30 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 659581065687 for ; Tue, 12 Aug 2008 16:22:30 +0000 (UTC) (envelope-from jordi@cdmon.com) Received: from correo.cdmon.com (correo.cdmon.com [212.36.74.112]) by mx1.freebsd.org (Postfix) with ESMTP id 1B73E8FC0A for ; Tue, 12 Aug 2008 16:22:30 +0000 (UTC) (envelope-from jordi@cdmon.com) Received: from localhost (localhost.cdmon.com [127.0.0.1]) by correo.cdmon.com (Postfix) with ESMTP id F0D87130F8C; Tue, 12 Aug 2008 18:22:28 +0200 (CEST) X-Spam-Flag: NO X-Spam-Score: -0.568 X-Spam-Level: X-Spam-Status: No, score=-0.568 required=5.9 tests=[AWL=-0.316, BAYES_00=-2.599, FH_HOST_ALMOST_IP=1.751, SPF_SOFTFAIL=0.596] Received: from correo.cdmon.com ([127.0.0.1]) by localhost (correo.cdmon.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id sGEciCzyHwRh; Tue, 12 Aug 2008 18:22:22 +0200 (CEST) Received: from [192.168.0.174] (62.Red-217-126-43.staticIP.rima-tde.net [217.126.43.62]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by correo.cdmon.com (Postfix) with ESMTP id 1C5A6130E92; Tue, 12 Aug 2008 18:22:22 +0200 (CEST) Message-ID: <48A1B8BD.3080306@cdmon.com> Date: Tue, 12 Aug 2008 18:22:21 +0200 From: Jordi Moles Blanco User-Agent: Thunderbird 2.0.0.16 (X11/20080724) MIME-Version: 1.0 To: Giorgos Keramidas References: <48A1A613.5020407@cdmon.com> <87fxpab4gy.fsf@kobe.laptop> In-Reply-To: <87fxpab4gy.fsf@kobe.laptop> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Cc: freebsd-questions@freebsd.org Subject: Re: error allocating memory with realloc(). how can i increase max_allowed in the system? [solved] X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: jordi@cdmon.com List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 12 Aug 2008 16:22:30 -0000 Hello, thank you very much for your time and help, i had completely misunderstood how realloc() works. i though i was able to write some C code but now i feel a complete newbie, hehehe. anyway... that made everything clear to me and now my script is working like a charm. thanks for everything En/na Giorgos Keramidas ha escrit: > 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. > > _______________________________________________ > freebsd-questions@freebsd.org mailing list > http://lists.freebsd.org/mailman/listinfo/freebsd-questions > To unsubscribe, send any mail to "freebsd-questions-unsubscribe@freebsd.org" >