Re: segmentation fault on calloc

From: Martin Dickopp (expires-2004-01-31_at_zero-based.org)
Date: 12/30/03


Date: 30 Dec 2003 11:04:48 +0100

dyksjare@hotmail.com (Jared Dykstra) writes:

> Hallvard B Furuseth <h.b.furuseth(nospam)@usit.uio(nospam).no> wrote in message news:<HBF.20031229v23v@bombur.uio.no>...
> > laberth wrote:
> >
> > > I've got a segmentation fault on a calloc and I don'tunderstand why?
> >
> > The simplest explanation I can think of is that you
> > may have forgotten '#include <stdlib.h>' which declares calloc().
>
> Failure to #include something is only going to confuse the compiler.
> The linker doesn't care, and of course has no effect at runtime being
> a preprocessor directive. A segmentation fault indicates a runtime
> error.

Failure to #include <stdlib.h> makes the compiler believe that `calloc'
returns `int', while in fact it returns `void *'. Normally the compiler
would emit a diagnostic when an `int' is assigned to a pointer, however
the OP prevented this by casting the return value of `calloc'. The result
is undefined behavior, which may well cause a runtime error.

Martin



Relevant Pages

  • Re: Why does passing two dimensional array to function accepting pointer to a pointer generate an er
    ... As seebs says you lied to the compiler, instead of (int **) you can: ...
    (comp.lang.c)
  • Re: It Pays to Enrich Your C Skills
    ... Check if you can score a perfect 10 (without using a compiler). ... int main{ ... struct bitfield { ... out if it is a negative integer constant or a constant expression ...
    (comp.lang.c.moderated)
  • OT: Re: Perl Peeves
    ... I see the result of a test being used as an int. ... the compiler just assumed you knew what you were doing ... introduced to the language later, so void * was unheard of in most code. ... This didn't mean bool was special, declaring it just signaled to the ...
    (comp.lang.perl.misc)
  • Re: OT: Re: Perl Peeves
    ... when I see the result of a test being used as an int. ... compiler just assumed you knew what you were doing and would ... This didn't mean bool was special, declaring it just signaled to the ... What "normalization of bool results is built into the compiler"? ...
    (comp.lang.perl.misc)
  • Re: [CodeGallery] MFC MD5 Calculator
    ... Then when they added types, internally, the compiler still thought they were int values, ... ANSI standard began to emerge that the language design ...
    (microsoft.public.vc.mfc)