2

I am currently writing an API for reading an OBJ file. In this API, i have a List of Vectors, and a class Describing a Face(3 vectors).

I want to think about memory usage, so i wonder if it is smartest for the face to remember the index of its vectors in the vector array, or if it should just have a pointer/instance of the vectors.

Also, would the same count in C#?

Liam S. Crouch
  • 167
  • 1
  • 10

1 Answers1

0

An integer (which your index would be) is 32 bits, a reference is either 32 or 64 bits an object is a minimum of 64 bits plus its internals. So an integer is either the same size or slightly smaller than a reference. But a copy of the object will be much larger

Index of array or reference

But seriously you shouldn't be worrying about this index to an array of references unless there are an insane number of these and you are memory starved. And of course there will be a small performance penalty for this indirection. Do whichever makes most sense conceptually but its likely you want to stick with references not indexes to an array if both make conceptual sense - very similar memory footprint and a simpler structure.

Copy of vector

Of course the third option is to keep a reference to a copy of the vector, this would involve the full memory of the object for each copy and is worth avoiding unless you need independent objects

Community
  • 1
  • 1
Richard Tingle
  • 16,906
  • 5
  • 52
  • 77