Object and pointer allocation

Asked

Viewed 451 times

2

How do you allocate a pointer, which points to a class, and an object? Is there a difference? Size, etc

  • 1

    Could you clarify a little more what you want? Difference than compared to what?

  • 3

    I think that’s the same doubt : http://answall.com/questions/50165/quando-devo-choose-entre-utili-ou-n%C3%A3o-a-pointer-to-create-an-object/50171#50171

  • Object is an instantiated class. There is no difference between common pointers (pointing to a struct, for example) of an object. What changes is the semantics between them (for example in the resolution of virtual methods). Specify a little more your doubt, and I can explain better.

  • Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? You need something to be improved?

1 answer

4

Pointers do not point to classes, but to objects. Classes are abstract concepts that exist during the definition of their form in code, after compilation they do not exist. There may be some static data contained in it, so yes it could have a pointer to these data individually. Maybe that’s what you’re calling class. But deep down these data do not cease to be objects.

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

Pointer is pointer, the size does not vary on the same architecture. On 16-bit architectures they have a size of 2 bytes. On 32-bit architectures, which was the most common, it has 4 bytes. On 64-bit architectures they have 8 bytes. Since the pointer indicates a memory address and 64 bits allows addressing 16 Hexabytes, I don’t think we’ll see it pop (in practice some architectures limit this a little because no one needs it all now and it’s cheaper to reduce).

There is more information on this in that question. There’s a curiosity how it is possible to use an architecture on a specific platform and still allocate pointers of different size.

Use strategies in that other (there’s a link to explain the difference between stack and heap which 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 addresses of the stack 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.

Its allocation is given as any other. On most platforms it is mistaken for a int and these two types of data can be exchanged (some cases may be a long), that is, you can take a pointer and consider it as an integer and vice versa, even if this is not done carefully, it can be disastrous. Pointers can be allocated in the stack or in the heap.

There may be a overhead Extra memory consumption because of the object allocation in the heap, but this has no relation to the pointer. This overhead - an allocation administrative area - is related to the memory management used by the operating system or by an own manager of the Runtime language or application. This may vary, but in many implementations it is 8 bytes at 32 bits (two words). Nor am I speaking of the alignment of memory that is another matter.

Wikipedia article.

Article showing memory allocation working.

  • 1

    not necessarily the pointer has 64 bits on a 64-bit architecture. For example, x32 binaries are x86-64 binaries but with a 32-bit pointer size. It was invented precisely because of this overhead in pointer size, since most applications do not even use the 4GB of memory that the 32 bits allow addressing.

  • 1

    Yeah, I explain this in my other answers linked.

  • Oh yes. I didn’t read them =] It was bad!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.