-1

Let's say Octree() is a container with Elements of type double.

Can I use sizeof(Octree) to determine how much memory in bytes is taken up by my octree?

Sizeof() should change if I change the resolution/depth of my octree - which does not seem to be the case when I test it.

Is there a way I could determine the dynamically allocated memory size of my octree?

copcor
  • 55
  • 6
  • 3
    `sizeof` only tells you the compile time size of an object. If your object is like a vector that cam get bigger at run time, then you need to have a `size` member function that will return the size at run time. related/dupe: https://stackoverflow.com/questions/34034849/what-is-the-size-of-sizeofvector-c – NathanOliver Dec 15 '21 at 22:11
  • 3
    `sizeof` is always a compile-time constant. – HolyBlackCat Dec 15 '21 at 22:11
  • 2
    I'm pretty sure that you mean `Octree` is a CONTAINER whose ELEMENT TYPE is `double`. Can you confirm ? – Ben Voigt Dec 15 '21 at 22:11
  • 2
    @HolyBlackCat: Unless you have a compiler that for some strange reason allows C VLAs in C++ code. Then it might not be. – Ben Voigt Dec 15 '21 at 22:13
  • @BenVoigt - That is correct. – copcor Dec 15 '21 at 22:16
  • For reference, I just verified that sizeof std::string is 32 for both a null string and a 100-character string on recent g++. – stark Dec 15 '21 at 22:25

2 Answers2

3

No. sizeof returns the size of the object. Which is the size of the type of the object. Which remains constant through the entire program. sizeof does not return the amount of memory that member functions of the object have allocated dynamically, and it cannot be overloaded to do so.

Is there a way I could determine the dynamically allocated memory size of my octree?

Certainly. You can keep track of all dynamic memory that you allocate, and their sizes together to get the total. This won't include overhead consumed by the data structure used by the allocator itself. There's no standard way to measure that.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • To be pedantic, allocation by member functions is a common way that variable amounts of memory can be associated with/owned by an object, but there's nothing special about this way. For example, `std::unique_ptr` very often owns memory allocated by `std::make_unique()` which is not a member function. `sizeof` *never* accounts for owned/associated/attached data which is stored outside the object, no matter how that is accomplished. – Ben Voigt Dec 15 '21 at 22:20
1

As others said, sizeof only gives you the size (in bytes) of a single node (not including any storage pointed to by any member field of your node).

If you want to compute the actual size of a tree, you need something like this:

template <typename T>
std::size_t OctreeSize(const Octree<T> &tree_root_node) {
  std::size_t tree_size = 0;
  Visit(
   tree_root_node,
   [&tree_size] (const Octree<T> &node) {
     tree_size += sizeof(node);
     // If you have any dynamically-allocated object
     // pointed to and owned by Octree<T>, add their size as well
  });
  return tree_size;
}

Where void Visit(const Octree<T> &, std::function<void(const Octree<T>&)> is a function that traverses each node of the tree and invokes the provided function.

akappa
  • 10,220
  • 3
  • 39
  • 56