Where in the hell is NULL?

Where in the hell is NULL?

por Thiago Pereira Bueno -
Número de respostas: 3

Pessoal, para quem também tiver tido curiosidade em procurar (literalmente) a constante NULL definida na biblioteca padrão C...

Executando o seguinte comando para sistemas UNIX-like (para Windows acho que rola usando o cygwin):

find /usr/include -name stdlib.h -exec grep -nH "#define NULL\|#ifndef NULL" {} \;
/usr/include/stdlib.h:114:#ifndef NULL
/usr/include/stdlib.h:115:#define NULL __DARWIN_NULL

Encontrei que a constante NULL (ao menos no meu SO) está definida em função de outra constante "__DARWIN_NULL".

Por sua vez, procurando por __DARWIN_NULL, encontrei o seguinte:

find /usr/include -name *.h -exec grep -nH "#define __DARWIN_NULL\|#ifndef __DARWIN_NULL" {} \;
[...]
/usr/include/sys/_types.h:82:#define __DARWIN_NULL __null
/usr/include/sys/_types.h:85:#define __DARWIN_NULL (0L)
/usr/include/sys/_types.h:87:#define __DARWIN_NULL 0
/usr/include/sys/_types.h:91:#define __DARWIN_NULL ((void *)0)

O que aparentemente está de acordo com a documentação do GNU disponível em Appendix A C Language Facilities in the Library.

[]s

Thiago

Em resposta à Thiago Pereira Bueno

Re: Where in the hell is NULL?

por José Coelho de Pina -

Oi Thiago

Rapidamente, não olhei muito... De fato não é fácil achar. Veja o que encontrei. Qual a distribuição de linux que você tem?

meu_prompt> locate stdlib.h
/usr/include/stdlib.h
/usr/include/i386-linux-gnu/bits/stdlib.h
/usr/lib/syslinux/com32/include/stdlib.h

meu_prompt> grep NULL stdlib.h
/* Get size_t, wchar_t and NULL from  <stddef.h>.  */
# define	__need_NULL
  return (int) strtol (__nptr, (char **) NULL, 10);
  return strtol (__nptr, (char **) NULL, 10);
  return strtoll (__nptr, (char **) NULL, 10);
/* Return the value of envariable NAME, or NULL if it doesn't exist.  */
/* This function is similar to the above but returns NULL if the
   the master FD is open on, or NULL on errors.

meu_prompt> grep NULL /usr/lib/gcc/i686-linux-gnu/4.7/include/stddef.h
    || defined(__need_ptrdiff_t) || defined(__need_NULL) \
     && !defined(__need_ptrdiff_t) && !defined(__need_NULL)	\
#if defined (_STDDEF_H) || defined (__need_NULL)
#undef NULL		/* in case  has defined it. */
#define NULL __null
#define NULL ((void *)0)  /* Acho que "é" isto aqui. */
#define NULL 0
#endif	/* NULL not defined and  or need NULL.  */
#undef	__need_NULL

P.S. Olhem também a pagina do prof. Paulo Fefiloff Arquivos-interface de algumas bibliotecas padrão.

Em resposta à José Coelho de Pina

Re: Where in the hell is NULL?

por Thiago Pereira Bueno -

Oi Prof. Coelho,

De fato não é nada fácil encontrar o ultimate #define...

Como estou usando i686-apple-darwin10, grepando a minha stdlib.h, tudo que encontrei como referência a NULL foi a definição em função da constante __DARWIN_NULL, específica da plataforma...

grep NULL /usr/include/stdlib.h
#ifndef NULL
#define NULL __DARWIN_NULL
#endif /* ! NULL */

Também não encontrei referência direta à stddef.h na minha stdlib.h ...

O que parece indicar que essas definições de macros são variáveis de implementação à implementação...

De qualquer forma grepando o stddef.h na minha máquina encontro definição semelhante à que encontrou...

find /usr/lib/gcc -name stddef.h -exec grep -nH '#define NULL' {} \;
/usr/lib/gcc/i686-apple-darwin10/4.2.1/include/stddef.h:400:#define NULL __null
/usr/lib/gcc/i686-apple-darwin10/4.2.1/include/stddef.h:403:#define NULL ((void *)0)
/usr/lib/gcc/i686-apple-darwin10/4.2.1/include/stddef.h:405:#define NULL 0

[]s

Em resposta à Thiago Pereira Bueno

Re: Where in the hell is NULL?

por Wellington Castello -

No OS X aparece #include <_types.h> na stdlib.h e a definição de NULL como __DARWIN_NULL, daí:

$ grep __DARWIN_NULL /usr/include/sys/_types.h
#define __DARWIN_NULL __null
#define __DARWIN_NULL (0L)
#define __DARWIN_NULL 0
#define __DARWIN_NULL ((void *)0)

Sim