Is it possible for a pointer to point to address 0x000000

This pointless question is about pointers, can someone point me in the right direction

Can the address of a variable ever be legitimately assigned the value 0x000000, if so means this, for example:

#define NULL 0x00000000
int example;
int * pointerToObject = &example;

if(pointerToObject != NULL)

      

will return false?

+3


source to share


4 answers


No, you cannot get the valid pointer (= nonnull) p

that it p != NULL

returns for false

.

Note, however, that this does not mean anything about "address 0" (whatever that means). C ++ is intentionally very abstract when referring to addresses and pointers. There is a null pointer value, that is, a pointer value that does not point anywhere. The way to create such a pointer is to assign a null pointer constant to a pointer variable. In C ++ 03, a null pointer constant was literal 0

, optionally with a suffix (for example 0L

). C ++ 11 added another null pointer constant, literal nullptr

.

How the representation of a null pointer value is internally outside the scope of the C ++ language. For some (most?) Systems, the address value is used for this 0x00000000

, since nothing that the user program can indicate can legally reside at this address. However, there is nothing that prevents the hardware platform from using a value 0xDEADBEEF

to represent a null pointer. On such a platform, this code:

int *p = 0;

      

must be compiled as assigning a value 0xDEADBEEF

of 4 bytes occupied by a variable p

.



On such a platform, you can legally get a pointer to an address 0x00000000

, but it will not compare to 0

, NULL

or nullptr

.


Another way to look at this is that the following declarations are not equivalent:

int *p = 0;
int *q = reinterpret_cast<int*>(0);

      

p

initialized with a null pointer value. q

initialized with an address 0

(depending on the mapping used in reinterpret_cast

, of course).

+9


source


Yes, of course you can have null pointers. And this check in your question will return false

for null pointers. For example, it malloc()

might return a null pointer on error, and you'd better check that.

0x000000

is interpreted as a null literal by the compiler and therefore can yield a null pointer. Same as code from here :



int* ptr = int();

      

+3


source


At first, you must underestimate how memory is allocated when executing a given program.

There are three types of memory:

  • Fixed memory
  • Stack memory
  • Heap memory

Fixed memory

  • Executable code
  • Global Variables
  • Persistent structures that do not fit inside a machine instruction. (constant arrays, strings, floats, long integers, etc.).
  • Static variables.
  • Local variable of a routine in non-recursive languages ​​(such as early FORTRAN).

Stack memory

  • Local variables for functions, the size of which can be determined by calling time.
  • Information saved when the function was called and restored when the function returned:
    • Callee Argument Values
  • Register values:
    • Return address (PC value)
    • Frame pointer (FP value)
      • Other registers
  • Static link (to be discussed)

Heap memory

  • Structures that are dynamically resized (for example, variable length arrays or strings).
  • Structures that are allocated dynamically (for example, entries in a linked list).
  • Structures created by function calls that must survive after calls return.

Questions:

  • Distribution and free space management
  • Collection to remove / remove garbage

How is the program loaded into memory?

When a program is loaded into memory, it is made up of three areas of memory called segments: a text segment, a stack segment, and a heap segment. A text segment (sometimes also called a code segment) is where the compiled code of the program itself resides. It is a machine language representation of the steps of a program to be executed, including all the functions that make up the program, both user-defined and the system.

The other two areas of system memory are where storage can be allocated by the compiler to store data .

How memory is organized

enter image description here

Of course, the zero you see in this image refers to offset.

Conlcusion

Even if a program is allocated in memory starting at an address 0x00000000

, the storage engine does not associate that address with the constant, static, or dynamic data that the program uses. Because all these elements are stored in sections data

, heap

or stack

, and at the same time they are highlighted after the section .text

.

Discussion

I think the pointers are NULL

set to 0x000000 for the reason that no variable (dynamic, local, static or global) is allocated at this address.

+2


source


It is not possible for C ++ to access memory at address 0. C ++ reserves a value (but not necessarily that physical address) for a null pointer .

So, your statement if (pointerToObject != NULL)

will never be false

.

Pre C ++ 11, you would point to this pointer using NULL

, which would typically be a macro defined for 0

or (void*)0

. In C ++ 11, the keyword is nullptr

used for the specified pointer value.

One useful property is that it delete p

will be benign if p == nullptr

. free

in C has a similar property.

0


source







All Articles