Afficher un message
Vieux 31/01/2008, 13h31   #3
Richard Tobin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Question on accessing structure

In article <147788b5-f183-4043-8f51-c36115f5a5e0@k39g2000hsf.googlegroups.com>,
Sid <Sid4Comp@gmail.com> 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
  Réponse avec citation
 
Page generated in 0,05193 seconds with 9 queries