Re: correct way to initialize pointer...

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Mandeep Sandhu wrote:
On 10/24/06, *Momchil Velikov* <velco@xxxxxxxxx <mailto:velco@xxxxxxxxx>> wrote:

    Mandeep Sandhu wrote:
     > Hi All,
     >
     > This is more of a 'C' question than a kernel one. But since i'll be
     > putting this
     > in a module, I thought I'd ask here too...
     >
     > Whats the correct way to initialize a pointer?
     >
     > 1. char *p = 0;
     > OR
     > 2. char *p = NULL;

    "[#3] An integer constant expression with  the  value  0,  or
    such  an  expression  cast  to type void *, is called a null
    pointer   constant."

    You could do char *p = 1 - 1; char *q = 0; char *r = 1 / 2;
    if you wish.


Thats an interesting way of writing zero! :)

I read some time ago (can't remember where!), that 0 is not always
an invalid address.
Depends on what you call invalid address. There's no object pointed to by a null pointer, thus you must not dereference it. This is regardless of whether the environment would trap the null access or would silently return whatever bytes are at address zero.

Though I think the C compiler is supposed to generate
one for any such assignments (as shown above).
C compiler is supposed to initialize the pointers wioth the null pointer constant, regardless of the null pointer constants representation.

Is this a C standard?
Yes.

Are all compilers supposed to do it?
Only C compilers.

Shouldn't NULL be a better choice so that we can #define it to an invalid
value, where this #define is specific to an architechture?? CMIIW.
If you want don't like your pointers initialized with that specific invalid value, called ``null pointer constant'', I suggest you #define, say, INVALID_POINTER_VALUE and not overload the semantics of NULL.

~velco


--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/kernelnewbies/
FAQ:           http://kernelnewbies.org/faq/


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux