:: Re: [DNG] Making sense of C pointer…
Pàgina inicial
Delete this message
Reply to this message
Autor: KatolaZ
Data:  
A: Edward Bartolo
CC: dng
Assumpte: Re: [DNG] Making sense of C pointer syntax.
On Wed, Mar 30, 2016 at 09:16:22AM +0200, Edward Bartolo wrote:
> Hi and many thanks for the replies,
>
> I can understand that a pointer being an address depends heavily on
> machine architecture which means, on 32 bit machines it is 4 bytes
> long and on 64 bit machines it is 8 bytes long. I also understand that
> a pointer variable is essentially made of two parts as illustrated
> below:
>
> address [always allocated] ------------------> data [not allocated
> automatically]
>
> The address does depend on architecture but the data? And what else
> can enter into a pointer's definition other than what I illustrated?
>


Hi Edward,

sorry but your description is incorrect. A pointer in C is just a
variable large enough to contain a memory address. Period. There is no
explicit or implicit linking between a pointer (which is a variable
large enough to contain a memory address) and the area of memory it
points to. You may have several pointers pointing to the same memory
area. You can have the same pointer (i.e., the same named variable
able to contain a memory address) pointing to different memory areas
at different times. You can use a pointer to wander around an
allocated memory area at your will, changing its value by using the
powerful pointer arithmetic provided by C. You can also have some
allocated memory area for which you don't have any pointer at all (and
this is what is called a "memory leak", and you should avoid it).

Also, memory areas are not "typed" in C, meaning that you can in
principle access a correctly allocated memory area with pointers of
any type, the only problem being the semantics of pointer arithmetics,
which is entirely left to the programmer.

But please forget any implicit linkage between a pointer (the
variable) and the memory area it points to (i.e., the address
contained in that variable), as you don't assume any intrinsic link
between an integer variable and the possible values that the variable
can contain. If you don't break this spell, you will never get around
with C pointers.

Pointers are just variables which are able to contain memory
addresses. What you put in those variables does not bother them in any
discernible way. And shouldn't bother you either, as long as you be
careful in using them to refer to correctly allocated memory. SIGSEGV
is the alternative.

My2Cents

KatolaZ

--
[ Enzo Nicosia aka KatolaZ --- GLUG Catania -- Freaknet Medialab ]
[ me [at] katolaz.homeunix.net -- http://katolaz.homeunix.net -- ]
[ GNU/Linux User:#325780/ICQ UIN: #258332181/GPG key ID 0B5F062F ]
[ Fingerprint: 8E59 D6AA 445E FDB4 A153 3D5A 5F20 B3AE 0B5F 062F ]