Allocation of objects and pointers

1

How do I allocate a pointer, which points to a class, and that of an object? Is there a difference? Size, etc.

    
asked by anonymous 05.04.2015 / 01:44

1 answer

3

In general pointers do not point to classes, but to objects. Classes are abstract concepts that exist while defining their form in code, after compilation they do not exist. There may be some static data contained in it, then you could have a note for these data individually. Maybe this is what you're calling class. But in the background these data are still objects.

Objects can be in multiple locations, they do not need to have pointers to them. If it is an instance of a class then it will probably have pointer pointing to it.

Pointer is pointer, the size does not vary in the same architecture. In 16-bit architectures they have a 2-byte size. In 32-bit architectures, still the most common, it has 4 bytes. In 64-bit architectures they have 8 bytes. As the pointer indicates a memory address and 64 bits allows addressing 256 Hexabytes, I do not think we will see this popping up.

More information on this on this question . There is a curiosity for how you can use an architecture on a specific platform and still allocate pointers of a different size.

Utilization strategies in this other (there is a link to explain the difference between stack and heap that is important). Although it may have pointers to the stack , this is less common since the information is much closer and it is fleeting. The code usually points to stack addresses but these notes are not usually considered pointers, at least in the sense of what you seem to be talking about, after all there is no allocation in this case.

Question to understand pointers .

The allocation of it is a given as any other. On most platforms it is confused with int and these two types of data can be exchanged, ie you can take a pointer and consider it as an integer and vice versa, even if this is not done with careful, it can be disastrous. Pointers can be allocated to the stack or heap .

There may be an extra overhead of memory consumption because of the object's allocation in the heap , but this is unrelated to the pointer. This overhead - an administrative allocation area - is related to the memory management used by the operating system or a runtime runtime manager of the language or application. This may vary, but in many implementations it is 8 bytes in 32 bits. Nor am I talking about memory alignment that is another subject.

Wikipedia article .

Article that shows how memory allocation works.

    
05.04.2015 / 03:01