From owner-freebsd-questions@FreeBSD.ORG Wed Aug 20 23:34:31 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 D7F0E106566C for ; Wed, 20 Aug 2008 23:34:31 +0000 (UTC) (envelope-from det135@hoenikker.aset.psu.edu) Received: from f04n07.cac.psu.edu (f04s07.cac.psu.edu [128.118.141.35]) by mx1.freebsd.org (Postfix) with ESMTP id 6B0118FC20 for ; Wed, 20 Aug 2008 23:34:31 +0000 (UTC) (envelope-from det135@hoenikker.aset.psu.edu) Received: from hoenikker.aset.psu.edu (hoenikker.aset.psu.edu [128.118.99.49]) by f04n07.cac.psu.edu (8.13.2/8.13.2) with ESMTP id m7KCNdmN041958; Wed, 20 Aug 2008 08:23:39 -0400 Received: from hoenikker.aset.psu.edu (hoenikker.aset.psu.edu [128.118.99.49]) by hoenikker.aset.psu.edu (8.14.2/8.14.2) with ESMTP id m7KCNcAt081329; Wed, 20 Aug 2008 08:23:38 -0400 (EDT) (envelope-from det135@hoenikker.aset.psu.edu) Received: (from det135@localhost) by hoenikker.aset.psu.edu (8.14.2/8.14.2/Submit) id m7KCNc90081328; Wed, 20 Aug 2008 08:23:38 -0400 (EDT) (envelope-from det135) Date: Wed, 20 Aug 2008 08:23:38 -0400 From: Derek Taylor To: Jordi Moles Blanco Message-ID: <20080820122338.GY2699@psu.edu> Mail-Followup-To: Jordi Moles Blanco , freebsd-questions@freebsd.org References: <48A1A613.5020407@cdmon.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <48A1A613.5020407@cdmon.com> User-Agent: Mutt/1.5.18 (2008-05-17) X-Virus-Scanned: by amavisd-new 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 Reply-To: Derek Taylor List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 20 Aug 2008 23:34:31 -0000 On Tue, 12 Aug 2008, Jordi Moles Blanco wrote: >Hi, > >i'm running a FreeBSD 7.0 amd64 machine and struggling with some C code >i'm writing. In addition to the other comments already given, I think that it might be useful for your learning experience to have some additional comments. >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)); You should not cast the return value of malloc. [1] > missatge[0]='h'; > missatge[1]='o'; > missatge[2]='l'; > missatge[3]='a'; Recall that malloc() makes no promises about the contents of the memory allocated. I recommend, in the current style, adding: missatge[4]='\0'; > printf("\n\ntaula1: %s",missatge); > > int voltes; > voltes = 0; Traditionally in C, all variable declarations appear at the beginning of the local scope. Declaring new variables mid-scope is valid in certain off-shoots of C and may be acceptable in newer dialects/standards of the language, but the majority of C programmers might make certain assumptions about your code following older traditions. This certainly is not too egregious, but while I was here, I thought I'd mention it. > while(voltes<4) > { > midataula = midataula+500; > realloc(missatge, midataula * sizeof(char)); > voltes++; > } > > > printf("\n\ntaula2: %s",missatge); >} >************* > > >this is a full "working" you can compile on your machine. > >Like this... i get "Segmentation fault (core dumped)" > >but if instead of "while(voltes<4)" i use "while(voltes<3)" > >the script works fine with this output: > >********** >taula1: hola > >taula2: hola >********** > >so... i guess there must be a limit in the system somewhere. > >I've tried to reset all variables that i've seen in the "sysctl -a" list >refering to malloc, memory, mem, and so on... but so far i haven't fixed >the problem. > >i'm running this script as root and in the /etc/login.conf file there's >only the "default" group with the "unlimited" values. >A part from that, if i perform a "limit" call, i get this: > >************* > ># limit >cputime unlimited >filesize unlimited >datasize 33554432 kbytes >stacksize 524288 kbytes >coredumpsize unlimited >memoryuse unlimited >vmemoryuse unlimited >descriptors 45000 >memorylocked unlimited >maxproc 22500 >sbsize unlimited > >************* > >i've tried to resize datasize and stacksize, but the system won't let me >do so. > >any idea how to solve this? > >thanks. > Good luck with your programming and systems work! -Derek. [1]