3

For example, we have

int* p;

Could this pointer be initialized by 0 randomly, it means initialized by the operating system, in this case we dont change the value of this pointer ?

  • 3
    It could. But how would that affect your code in any way whatsoever? – user253751 Feb 24 '15 at 06:21
  • Sorry, but I have heard that the address space which contains 0, is reserved for handling the exceptional situations, checking invalid pointers, moreover this place of the memory could be used only by kernel of the os. Is it correct ? –  Feb 24 '15 at 06:24
  • 2
    Both Windows and Linux will never make address 0 a valid address, no idea about Mac OS. But how would that affect your code in any way whatsoever? – user253751 Feb 24 '15 at 06:25
  • 1
    Pointers set to 0 are typically used to say they aren't pointing to anything (i.e. null). You probably shouldn't expect a pointer (or any local variable for that matter) to be (or to not be) randomly initialized to a certain value, you should set it yourself... – Chris Usher Feb 24 '15 at 06:28
  • According C++ standard, there no any guarantee that the implicit default constructor of the local object could be invoked. Now imagine, we have a the follwwong class class foo{ int a; int *p} and we don't initialize these members. According to this, is there any possibility that these members, especially int* p could be initialized by 0 randomly ? –  Feb 24 '15 at 06:28
  • 2
    Yes. It is possible. But instead of worrying about if it's possible, you should probably just make sure the value is initialized. – Chris Usher Feb 24 '15 at 06:32

3 Answers3

2

Here's the tricky part: no valid program can figure this out. Reading p is Undefined Behavior, and anything may happen including returning nullptr even though p doesn't actually contain nullptr (!)

If you wonder how that's possible, p may be put in a register on first write. Trying to read p before that would give rather random results.

MSalters
  • 173,980
  • 10
  • 155
  • 350
0

Assumption: you are talking about the possibility that the return of a malloc or new should happen to be 0 at some point.

In this case, I believe the answer is no. The pointer will take a virtual address. Being something allocated dynamically, it will get an address belonging to the Heap that will never start at address 0.

The virtual memory space of your process is divided in more sections: Text, Data, BSS, Heap (where all the dynamically allocated objects go), the stack and the kernel space. The image below is for a 32b OS but for 64b the picture is similar.

You can make a small program and read some addresses in different spaces, and understand what you can and cannot access.

enter image description here

The heap (the place where your pointer will point), grows after the Text, Data and BSS segments. So it will never be 0.

VAndrei
  • 5,420
  • 18
  • 43
-1

Declaring the variable as Global or static would be automatically initialized to 0X0 by OS.

GingerJack
  • 3,044
  • 1
  • 17
  • 19
  • I don't "_believe you_". Can you point to the source code in the runtime library which does that? Or at least point to a specification documentation which claims this is guaranteed by all `C++` implementations and by all OSes? – xmojmr Feb 24 '15 at 07:08
  • 1
    @xmojmr: That "specification document" would be the ISO C++ Standard (ISO 14882-2014). It can't guarantee what the OS does, but a typical OS doesn't even have a notion of " globals" anyway. The requirement is on the C++ implementation. – MSalters Feb 24 '15 at 08:24
  • 2
    @xmojmr Section 3.6.2 of the C++ standard. – molbdnilo Feb 24 '15 at 08:27
  • @molbdnilo is it some closed source information someone has to buy or you can provide a link to click and see the proof for free (like in free beer)? The answer, even after comments, is still _believe me_ and not useful – xmojmr Feb 24 '15 at 09:08
  • @xmojmr The draft is available for free. Your favourite search engine can find it for you. – molbdnilo Feb 24 '15 at 09:26
  • @molbdnilo the latest draft dated 2014 available from repository [open-std.org: JTC1/SC22/WG21 - Papers 2014](http://open-std.org/jtc1/sc22/wg21/docs/papers/2014/) linked from the [Wikipedia: C++14](https://en.wikipedia.org/wiki/C%2B%2B14) article seems to be `N4296`. Among other things (like chapter "3.6.2 Initialization of non-local variables") it says "_Note: this is an early draft. It’s known to be incomplet and incorrekt, and it has lots of bad formatting_". Is this the free information source you were referring to? – xmojmr Feb 24 '15 at 11:37