c++ initialize pointer at 0x0

0
32

I was looking into the C++ compiler’s default behavior of allocating memory for a pointer at its current address. I was told this was a legacy of the old C compiler. Apparently, this is the default behavior from the compiler with the same name.

This is a very interesting point. In C, the main argument is the pointer that a pointer is supposed to return, and in C++, the main argument is the pointer that a pointer is supposed to return. It’s a way of saying that, and so the compiler can see if there’s a pointer that will be called to point to.

The thing is that with pointers you can pass a pointer to one area of memory and another to another. You can even pass a pointer to a function and another to a variable.

By default, the compiler will always run as it’s been initialized, even if the pointer is actually passed by reference. A compiler can then take care of that, just like the standard library.

The problem is that all pointer-related constructs require at least one pointer you pass to it. But the compiler can’t know this for sure. The only way it can check that you haven’t accidentally passed a pointer without knowing it is that it prints a warning. I don’t think that’s such a bad thing though, because most programmers don’t really think about the pointer-related stuff.

Sometimes a compiler can be smart to not compile a line of code that looks like it is meant to work and just ignore it. If you arent careful you can end up with code that will never compile. This happens all too often in C++. For example, if you try to copy a string into a string that is passed by reference, the compiler has no idea what you’re trying to do.

On the topic of pointers, I think the most important of these pointers is the one pointed to the memory location that is being pointed at.

A pointer can point to the location of a location on the heap. If you don’t have the pointers pointed to, you don’t even have control over the memory location. You can either be just pointing to location zero, and the pointer pointed to does not contain any data. I think this is the most important thing to learn about pointers today.

It’s simple actually. A pointer is a variable that points to a location on the heap. It should be pointed to with the value 0. If you do not have that, then I’m afraid you will never be able to use those pointers. In this case, the c++ language does not offer you any control over the memory location you’re trying to point to.

For example, when you try to assign a string to a pointer, you are attempting to set a memory location with the contents of the string. So if it is to the 0x0 address, your string will be there with no contents. So if you want to make it point to a bit of memory, you need to use a string literals.

LEAVE A REPLY

Please enter your comment!
Please enter your name here