Re: Question on accessing structure
- From: richard@xxxxxxxxxxxxxxx (Richard Tobin)
- Date: 31 Jan 2008 12:31:36 GMT
In article <147788b5-f183-4043-8f51-c36115f5a5e0@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>,
Sid <Sid4Comp@xxxxxxxxx> wrote:
why does not the above program result in segmentation error.
I am just allocating memory for structure A and typecasting it to
structure X and writing value to its members i.e Structure A and
structure B.
Most general-purpose operating systems allocate memory in units of
"pages", which might be a few kilobytes long. You only get a
segmentation fault when you write to an unallocated page. The bytes
immediately after your malloc()ed space are probably within the same
page, and accessing them doesn't generate an error. Even if it
happened to be at the end of a page, it's quite likely that the
following page has already been allocated by the operating system,
even if malloc() hasn't used it for anything yet.
Keeping track of the exact bounds of malloc()ed memory would add a lot
of overhead and slow your program down. But there are tools that do
this, for finding bugs of the kind your program has. "Valgrind" is a
well-known example.
-- Richard
--
:wq
.
- References:
- Question on accessing structure
- From: Sid
- Question on accessing structure
- Prev by Date: Re: Strange C operator
- Next by Date: Re: Type conversion
- Previous by thread: Re: Question on accessing structure
- Next by thread: Re: Question on accessing structure
- Index(es):