Re: segmentation fault on calloc
From: Martin Dickopp (expires-2004-01-31_at_zero-based.org)
Date: 12/30/03
- Next message: copx: ""serializing" structs in C"
- Previous message: Vijay Kumar R Zanvar: "C99 Question"
- In reply to: Jared Dykstra: "Re: segmentation fault on calloc"
- Next in thread: Jared Dykstra: "Re: segmentation fault on calloc"
- Reply: Jared Dykstra: "Re: segmentation fault on calloc"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: copx: ""serializing" structs in C"
- Previous message: Vijay Kumar R Zanvar: "C99 Question"
- In reply to: Jared Dykstra: "Re: segmentation fault on calloc"
- Next in thread: Jared Dykstra: "Re: segmentation fault on calloc"
- Reply: Jared Dykstra: "Re: segmentation fault on calloc"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|