0

I have a class I've created that represents a molecule and I would like to populate a 1D array with these molecules.

class Molecule {
public:
  enum TypeReplicator {
    p,
    q,
    s,
  };

  enum TypeComplex {
    free,
    occupied,
    temp,
    cata,
  };

private:
  double m_kppp{}; // last two letters stand for template/product
  double m_kppq{};
  double m_kpqq{};
  double m_kpqp{};
  double m_kqpp{};
  double m_kqpq{};
  double m_kqqq{};
  double m_kqqp{};

  int m_cor{}; // This is coordinate in 1D for now.

  TypeReplicator m_typeRep{s};
  TypeComplex m_typeComp{free};

  double m_mutation_probability{};

public:
  Molecule() = default;

  Molecule(int cor, TypeReplicator typeR = s, TypeComplex typeC = free)
      : m_kppp{1}, m_kppq{1}, m_kpqq{1}, m_kpqp{1}, m_kqpp{1}, m_kqpq{1},
        m_kqqq{1}, m_kqqp{1}, m_cor{cor}, m_typeRep{typeR}, m_typeComp{typeC} {}

  const TypeReplicator &getTypeReplicator() { return m_typeRep; }
  int getCor() { return m_cor; }
};

When I create an array of this class object using

Molecule plane[512 * 512]{};

I get a address boundary error. I suspect it's something to do with my constructor definition. What's wrong?

Biffen
  • 6,249
  • 6
  • 28
  • 36
BSB57
  • 1
  • 3
    what is an "address boundary error" ? Please include the complete error message in the question – 463035818_is_not_an_ai Mar 09 '22 at 11:48
  • 1
    Related? [address boundary error](https://stackoverflow.com/questions/33095570/address-boundary-error). What's the debugger saying? – Michael Chourdakis Mar 09 '22 at 11:55
  • fish: Job 1, './a.out' terminated by signal SIGSEGV (Address boundary error) Switching the array to a vector seems to work, but I'm more interested in why the array doesn't work – BSB57 Mar 09 '22 at 12:11
  • Where `Molecule plane[512 * 512]{};` is used? Is this local variable? This array is huge, to big to be kept on the stack. – Marek R Mar 09 '22 at 12:20
  • It's a local variable I use to store molecules during a call to a function that initializes my simulation. So I can't make the array at all since it's too large? When replaced with a vector, I'm able to iterate over the whole vector and populate each element with molecule. How does that not cause the same error? – BSB57 Mar 09 '22 at 12:27
  • 1
    Reference to the site name "stack overflow". 512^2 = 262144 elements. each double is 8 bytes. Int and enum likely 4 bytes. 9x8+3*4 = 84 bytes per object. Totaling 22020096 bytes = 21504 kB is 21 MB. What is your allowed stack size? – JHBonarius Mar 09 '22 at 12:48
  • @BSB57 `std::vector` stores its data on the heap (using `new` and `delete`) where there's way more room. – Debaug Mar 09 '22 at 12:49
  • Don't define your `Molecule plane[512 * 512]{};` on stack, but as a global variable, with a `new` or with `std::vector`. – prapin Mar 09 '22 at 19:17
  • My rule of thumb: never declare objects bigger than 1 kB on stack. – prapin Mar 09 '22 at 19:18
  • Thank you all. I was vague on the concepts of stack/heap before but this has helped distinguish those two a bit. – BSB57 Mar 09 '22 at 22:27

0 Answers0