Here from getcwd(3) :
DESCRIPTION
The getcwd () function copies the absolute pathname of the current working
directory into the memory referenced by buf and returns a pointer to buf.
The size argument is the size, in bytes, of the array referenced by buf.
If buf is NULL, space is allocated as necessary to store the pathname.
This space may later be free (3) 'd.
That is - when dir NULL and free(3) dir set; OR make space for buf yourself (since you tell getcwd(3) , you have 1K there).
Edit:
So, to clean it up a bit, this is either:
char *dir = getcwd( NULL, 0 ); if ( dir == NULL ) { } free( dir );
or
char buf[1024]; if ( getcwd( buf, 1024 ) == NULL ) { }
source share