Date: Tue, 13 Jan 1998 18:02:00 -0800 (PST) Message-Id: <199801140202.SAA28703@adit.ap.net> Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" To: "=?iso-8859-1?Q?=22Dr._Andr=E1s_S=F3lyom=22?=" , djgpp AT delorie DOT com From: Nate Eldredge Subject: Re: Q: How to check validity of a pointer? Content-Transfer-Encoding: 8bit Precedence: bulk At 12:32 1/14/1998 +0100, Dr. András Sólyom wrote: >One of my programs keep crashing on realloc() and I cannot find the >reason. I am allocating and reallocating some million times and somehow >some pointers in the program which were valid becomes invalid, usually >having a value of 0x09000000). I have tried many things (like using one >of DJ's malloc()'s, or compiling under linux with ElectricFence) to >catch the error but without success. So for debugging I can try to catch >the invalid pointers. >Is there any way to check a pointer for validity before dereferencing >it? As an initial sanity check, you can see if the pointer value is less than 4096 (i.e. in the NULL page), or greater than `sbrk(0)'. However, I think that DJGPP's allocation scheme may leave holes in the address space, so it may happen that an invalid pointer passes both these tests. You can also run under a debugger like GDB, and inspect things when a SIGSEGV arrives. Symifying the traceback printed at the crash may also help. Nate Eldredge eldredge AT ap DOT net