Frage

Say I have this:

int *thing;

Is that using 4 bytes of memory even though I have nothing assigned to it? And when I assign something (thing=new int;), would it then use 8 bytes because the pointer itself might use memory?

Also, say I had this:

struct thingy
{
    int *intThing;
}

thingy *result=new thingy;
thingy.intThing=new int;
delete thingy;

Would intThing be deleted as well, or would the memory be left floating around with nothing pointing to it?

War es hilfreich?

Lösung

All variables (primitive numeric types, class instances, pointers, references) start out taking up space.

Then the optimizer gets involved, and may show that some storage is redundant (the value is constant, or always available from another variable, etc)

Making variables disappear is one of the explicit goals of the optimizer because it tends to reduce register pressure, improve cache performance, etc.

In your example, the pointer intThing would be destroyed (it's a member and members die when their parent object does), but the memory it points to (which would correctly be called *thingy.intThing and not just thingy.intThing) will not. If its address is not stored anywhere else, then it would be leaked.

Andere Tipps

  1. A pointers size depends on the machine and the OS it is created in.
  2. It will use up memory if you have or have not assigned anything to it.
  3. intThingy will not be deleted when you delete the object that has a pointer that points to *intThingy

The pointer itself is an object/variable which takes up space (either in stack or heap), which shouldn't be confused with what it points too. Even char *p = nullptr takes up sizeof(*p) bytes of space.

The standard guarantees that the size of all pointer types in a machine are the same I.e. sizeof(char*) == sizeof(void*) == sizeof(int*) is true. It depends on the compiler and the architecture of the target machine.

A host machine is where you compile your code, while the target machine is where you intend to run your code. These two can be different in which case it's called cross compilation. Only the hosts characteristics matter (the compiler's too). Say if the host is 64-bit but your compiler only is 32-bit, then the pointer sizes may be only 4 bytes, instead of 8.

After delete thingy; the object pointed to by thingy would be deleted but not thingy itself. However thingy would still be pointing to the memory where the deleted object existed. Tying to dereference it would lead to undefined behaviour (mostly a crash). Thus it is usually set to NULL after deletion. However there's another camp which believes that setting it to NULL is risky since an incorrect attempt to "re-deleting" the pointer wouldn't crash as it should (deleting NULL is a no-op) thereby hiding a bug.

Pointer is a type similar to other types in C++. Pointer itself takes up space in the memory and can be pointed only to the data type you specify while creating that pointer.

So yes even if you have nothing assigned to it, pointer will take memory space. Exactly how much memory space depends on machine that it is created.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top