CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15

Thread: null pointer

  1. #1
    Join Date
    Sep 2005
    Location
    New Delhi, India
    Posts
    332

    null pointer

    where does the null pointer point to??does it point to address 0( i don't think so)...plz verify
    Appreciate others by rating good posts

    "Only buy something that you'd be perfectly happy to hold if the market shut down for 10 years." - Warren Buffett

  2. #2
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    Re: null pointer

    Null pointer does not point to any valid memory location. Granted that memory locations start at 0, the value 0 (#define NULL 0) is not valid for zero-th location to memory when process is running.

    NULL value has special meaning, it is not a valid address - when you delete a null pointer using free/delete it wont harm your process (i.e. wont fire and exception) since null is well-known as not-a-valid-memory-location.
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

  3. #3
    Join Date
    Sep 2004
    Location
    A Planet Called Earth... :-)
    Posts
    835

    Re: null pointer

    when you delete a null pointer using free/delete it wont harm your process (i.e. wont fire and exception) since null is well-known as not-a-valid-memory-location.
    Just in case someone is intrested..., the actual reason why delete on a null pointer does no harm is because operator delete is implemented something like this...
    Code:
    .....
    if (pointer == NULL)
                return;         //So does nothing
    
    .....
    .....
    C++ program ran... C++ program crashed... C++ programmer quit !!

    Regards

    Shaq

  4. #4
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Thumbs up Re: null pointer

    Quote Originally Posted by Ajay Vijay
    null is well-known as not-a-valid-memory-location.
    I think this is not true. If you initialize a pointer to a null value and take the address of that pointer - it will return you a valid memory address. This:
    Code:
    #define NULL 0
    is used on most programs and the compiler converts the 'NULL' to 0 (a memory address, may be, cast as a void*). Now there could be machines that have a different bit pattern (say non-zero) and in that case its the compiler's responsibility to modify that '0' or 'NULL' to the specific null pointer. NULL pointer is a pointer that is assured not to point to some function or data (objects) - not that it is not-a-valid-memory-location, IMO. For details on NULL pointers, you may want to refer to these small questions provided at the following link - NULL pointers. Hope this helps. Regards.

  5. #5
    Join Date
    Sep 2004
    Location
    A Planet Called Earth... :-)
    Posts
    835

    Re: null pointer

    I dont see this being a "valid memory address"

    #define NULL 0
    C++ program ran... C++ program crashed... C++ programmer quit !!

    Regards

    Shaq

  6. #6
    Join Date
    Sep 2005
    Posts
    7

    Re: null pointer

    If you create a null pointer, the pointer itself will have a memory address. After all the address which the pointer points to must be stored somewhere in memory.
    The null bit refers to what the pointer is pointing to - the address which it holds is empty.

    Quote Originally Posted by exterminator
    I think this is not true. If you initialize a pointer to a null value and take the address of that pointer - it will return you a valid memory address. This:
    Code:
    #define NULL 0
    is used on most programs and the compiler converts the 'NULL' to 0 (a memory address, may be, cast as a void*). Now there could be machines that have a different bit pattern (say non-zero) and in that case its the compiler's responsibility to modify that '0' or 'NULL' to the specific null pointer. NULL pointer is a pointer that is assured not to point to some function or data (objects) - not that it is not-a-valid-memory-location, IMO. For details on NULL pointers, you may want to refer to these small questions provided at the following link - NULL pointers. Hope this helps. Regards.

  7. #7
    Join Date
    Jan 2001
    Posts
    588

    Re: null pointer

    There's nothing invalid about a null memory address in general. The only case in which dereferencing a null pointer is a problem is when the OS enforces a rule that address 0x00000000 is invalid. This is the case on most PC platforms, which is why you'll get a runtime error in Windows if you access address 0x00000000. The virtual memory manager for Windows enforces the rule that those low addresses are not accessible by your process.

    However, in an embedded system, address zero is often a valid address. On many CPUs, address 0 is where the processor begins to retrieve code at startup. If you're in a case with a basic OS or none at all, jumping to or accessing address zero isn't a problem at all. Many issues like this are misconstrued to be things that "C++ doesn't allow," where really the behavior that people see (i.e. a crash when you dereference a null pointer) is really just due to the operating system's memory management policy.

  8. #8
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,725

    Lightbulb Re: null pointer

    If it is too early in your code to know which address to assign to the pointer, then you first assign the pointer NULL, which is a constant with a value of zero defined in several standard libraries, including iostream
    Code:
    #include<iostream >
    using namespace std; 
    
    int main () 
    { 
      int* iPtr; 
      iPtr = NULL;  
      cout << "The value of iPtr is " << iPtr << endl; 
      return 0; 
    }
    NOTE: You also could use initialization instead of declaration followed by assignment, thus combining the first two statements in main to int* iPtr = NULL.

    The resulting output is
    The address of x using iPtr is 00000000
    A pointer that is assigned NULL is called a null pointer.


    On most operating systems, programs are not permitted to access memory at address 0 because that memory is reserved by the operating system. ”However, the memory address 0 has special significance; it signals that the pointer is not intended to point to an accessible memory location. Thus, if it is too early in your code to know which address to assign to a pointer, you should first assign the pointer to NULL, which then makes it safe to access the value of a pointer before it is assigned a “real” value such as the address of another variable or constant.

  9. #9
    Join Date
    Apr 1999
    Posts
    27,449

    Re: null pointer

    Quote Originally Posted by Vedam Shashank
    I dont see this being a "valid memory address"
    Read an old DOS operating system technical manual. Address 0000:0000 is perfectly valid and is the start of the interrupt vector table.

    Regards,

    Paul McKenzie

  10. #10
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: null pointer

    Quote Originally Posted by Paul McKenzie
    Read an old DOS operating system technical manual. Address 0000:0000 is perfectly valid and is the start of the interrupt vector table.
    With Borland C++'s small memory model, the NULL pointer, when accessed with a near pointer is ds:0000.
    Not ony, it is a valid memory address, but modifying the four first bytes at this address don't do any harm.
    Of course a program doing so is probably buggy, and will perhaps crash elsewhere.

    However, since it is not ISO compliant to access the NULL pointer, BC++ tests for modifications (with a checksum) of this first four bytes on program termination. If the checksum has changed, it outputs on stderr : Null pointer assignment.
    "inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
    Club of lovers of the C++ typecasts cute syntax: Only recorded member.

    Out of memory happens! Handle it properly!
    Say no to g_new()!

  11. #11
    Join Date
    Jan 2001
    Posts
    145

    Re: null pointer

    I think you guys all know either valid or invalid memory here refer to the same thing but with your different refered contexts.

    From the concept, if a pointer variable has a null pointer value, then this pointer variable has NOT been assigned a valid pointer value. A null pointer value represents an invalid pointer values in concept, which means this null pointer value does not reference to any value or object. It is something like a place holder.

    In C++, unfortunately, a pointer variable is not automatically initiated, it has an arbitrary value when being declared/defined. Using a null pointer value to set the new pointer variable to express this pointer variable has not been assigned with a valid pointer value, otherwise, there is no means for later on to detect if current pointer variable holds a valid pointer value or not.

    -

  12. #12
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    Re: null pointer

    From the process' point of view memory at location 0 is at base address of physical memory - which is not the case! Instead, the OS/Linker/Loader adjusts memory locations at runtime so that two processes (same or different executables) can have same memory locations, say 0xbbf0, but they are actually pointing to different memory locations in physical memory (or paging file).

    This management is done by OS to relocate memory locations when process starts up, when pointer is being de-referenced and when page-in/page-out of some memory location (Page being a unit) is being done.

    Not sure but 0 address, in this scenario, points to "base-address" of the process image - which is relative to the process and may be (definitely ) different for the OS. So, when attempt is made to access this address, a memory exception is generated since that memory is 'protected' (granted it is property of same process).

    So, null pointer is invalid from process' perspective, but can be classified as "valid" from Operating System/Loader side.
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

  13. #13
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Re: null pointer

    Note that 0x00 : 0x00 (Segment : Offset) is a valid virtual memory location (or simply 0x00 in real mode) reserved by any processor that is Intel x86 Compliant (what runs most of our computers) irrespective of the OS that runs on it.

    This location is the start of the Interrupt Vector Table - an array that extends for 1024 Bytes - making space for 256 Pointers to Interrupt Handlers - each pointer being 4 bytes wide.
    This makes for 256 possible Interrupts (including software and hardware).

    Now, NULL is defined as 0x00. But, this does not mean that even as a (virtual) address it points to 0x00 : 0x00

    The address held in a 32-Bit application pointer is an Offset (and not the Segment). Hence, a NULL pointer actually points to offset location 0x00 in a particular segment (like Data Segment).

    So, this pointer -
    Code:
    int * pInteger = NULL;
    ...Could be pointing to 0x00456A44 : 0x00000000 (Segment : Offset) - and even this is a valid location.

    Note that 0x00 : 0x00 should ideally be a protected location (being reserved by the processor), and not accessible by a normal application.

    Accessing 0x00 : 0x00 will perhaps raise a Protection Fault.
    Using a NULL pointer will cause an Access Violation.
    Last edited by Siddhartha; September 26th, 2005 at 03:30 PM.

  14. #14
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Thumbs up Re: null pointer

    Quote Originally Posted by Siddharta
    ...Could be pointing to 0x00456A44 : 0x00000000 (Segment : Offset) - and even this is a valid location.
    This address does not exists, because segments are 16 bits on x86 machines.
    "inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
    Club of lovers of the C++ typecasts cute syntax: Only recorded member.

    Out of memory happens! Handle it properly!
    Say no to g_new()!

  15. #15
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Re: null pointer

    Quote Originally Posted by SuperKoko
    This address does not exists, because segments are 16 bits on x86 machines.
    Yes... But, hope the point was conveyed...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured