🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.os.os2.programmer
Article #1119

Re: malloc causes a segmentation violation!?

#1119
From: Naatula Mika
Date: Wed, 19 Aug 1992 15:56
51 lines
2782 bytes
As a responce to my article (Bill Maniatty) writes:
>Possibility - Bug is yours (very small I'm sure, but still possible)

Yes that is true. People generally tend to think that they make no
errors...

>Can you compile and link with a debugging version of malloc?
>	(Some environments provide for one).
>
>You might want to count number of mallocs/frees and trap on a free when
>number malloced < number freed.

I made my own Smalloc and and Sfree functions which call the standard
malloc and free. I save in a table the allocated pointers and compare them
before free is called. This is an easy way to catch many possible errors.

>Verify that no access is made beyond the end of malloced structures.  Typically
>malloc creates a linked list looking like the following:
>
>	--------------------------------------------
>	| malloc info a | user data | malloc info b|
>	--------------------------------------------
>
>The malloc info contains ptrs to other blocks in free/use chain, size information,
>etc...  Please note that malloc info may be found at one end or the other of
>the structure, and writing beyond the end of structure would trash it and corrupt
>the heap.

Yes. That was the kind of information I was looking for. After my own
Smalloc and Sfree functions writing beyond allocated space is the only
possible cause of segmentation faults.

I thought that OS/2 could detect writes to non-allocated areas and
give me a segmentation fault right away.  Like it usually does. 

But back to the problem. I still have not found the actual spot of
the error, but I know what the reason is. (My program is a large one) 
In my own Smalloc when I call malloc I allocate the requested space
plus one and the segmentation fault is gone! ( p=malloc(size+1) )

Like many times before, the error was made by user! 

Thanks to all of you who have helped me with this.

>|	maniattb@cs.rpi.edu - in real life Bill Maniatty
-- 
             __   | Mika Naatula            Email: mic@cc.tut.fi
|\ //| ||  //  \  | Lindfrosinkatu 8 A 11          mn87504@cs.tut.fi
| \/ | || ||      | 33720 Tampere, FINLAND
|    | ||  \\__/  | 931-185 993
-- 
Technical Details
Message-ID: <1992Aug19.155648.6210@funet.fi>
Path: arc1-ca.pugleaf.net!sparky!uunet!charon.amdahl.com!pacbell.com!mips!darwin.sura.net!Sirius.dfn.de!zrz.tu-berlin.de!news.netmbx.de!Germany.EU.net!mcsun!news.funet.fi!mn87504
References: <1992Aug18.143135.6259@cc.tut.fi> <3dfy6bq@rpi.edu>

Show Full Headers
Complete Article Headers
Newsgroups: comp.os.os2.programmer,comp.lang.c
Path: arc1-ca.pugleaf.net!sparky!uunet!charon.amdahl.com!pacbell.com!mips!darwin.sura.net!Sirius.dfn.de!zrz.tu-berlin.de!news.netmbx.de!Germany.EU.net!mcsun!news.funet.fi!mn87504
From: mn87504@cs.tut.fi (Naatula Mika)
Subject: Re: malloc causes a segmentation violation!?
Message-ID: <1992Aug19.155648.6210@funet.fi>
Sender: usenet@funet.fi (#Kotilo NEWS system )
Nntp-Posting-Host: kaarne.cs.tut.fi
Organization: Tampere University of Technology
References: <1992Aug18.143135.6259@cc.tut.fi> <3dfy6bq@rpi.edu>
Date: Wed, 19 Aug 1992 15:56:48 GMT
Lines: 51