I
I
IliaNeverov2021-08-09 09:51:04
C++ / C#
IliaNeverov, 2021-08-09 09:51:04

How are the elements of an array of class objects arranged in memory?

How are arrays of class objects arranged? In theory, moving through the array occurs when the pointer is shifted by a certain number of bytes, but a class object can contain both a vector and a template can be, which means that different elements of the array (class objects) can occupy a different number of bytes in memory. How is this problem solved in c++?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Armenian Radio, 2021-08-09
@IliaNeverov

Objects in C++ have a constant size, known at compile time, equal to sizeof( instance )
Statement:

a class object can also contain a vector ... which means that different array elements (class objects) can occupy a different number of bytes in memory.

incorrect , since the vector does not store its data in its instance, but where the allocator puts it (usually puts it on the heap).
Statement:
[class object] and template can be ... which means that different array elements (class objects) can occupy a different number of bytes in memory.

false , since all templates are instantiated at compile time.
Consequently, class objects sit in an array one after another, have a fixed size, and you can crawl over them with a pointer, shifting it by this size.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question