Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 12 Aug 2008 17:02:43 +0200
From:      Jordi Moles Blanco <jordi@cdmon.com>
To:        freebsd-questions@freebsd.org
Subject:   error allocating memory with realloc(). how can i increase max_allowed in the system?
Message-ID:  <48A1A613.5020407@cdmon.com>

next in thread | raw e-mail | index | archive | help
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 <stdio.h>
#include <stdlib.h>

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++;
        }


        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.




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?48A1A613.5020407>