user23993: i want to create a somewhat complex structure of classes in an arduino. The short story is: a Device has Presets, and each Preset has a Patterns. For that i created the following structure (i am only posting the important stuff, if something is missing let me know):
INO file
#include "LedDevice.h"

LedDevice device;

void setup() {
  device = LedDevice(10, 10, 10);
}

void loop() {
  Serial.println(F("Ticking"));
  device.Tick();
  delay(500);
}

LedDevice
#if !defined(_LEDDEVICE_H)
#define _LEDDEVICE_H

#include "Preset.h"
#include<Arduino.h>

class LedDevice {
public:
  Preset* Presets[];

  LedDevice(){}
  LedDevice(int numPixels, int numPresets, int numPatterns)
  {
    *Presets = (Preset*)malloc(sizeof(Preset) * numPresets);

    for (int p = 0; p < numPresets; p++)
    {
      Presets[p] = new Preset("test", numPixels, numPatterns);
    }
  }

  void Tick()
  {
    Serial.print(F("Preset ")); Serial.println(Presets[0]->Name);
    Presets[0]->Tick();
  }
};

#endif  //_LEDDEVICE_H

Preset
#if !defined(_PRESET_H)
#define _PRESET_H

#include "Pattern.h"

class Preset {
public:
  char *Name;
  int NumPatterns;
  Pattern* Patterns[];

  Preset(char* name, int numPixels, int numPatterns) : NumPatterns(numPatterns)
  {
    Name = (char *)malloc(strlen(name) + 1);
    strcpy(Name, name);

    *Patterns = (Pattern*)malloc(sizeof(Pattern) * NumPatterns);

    for (int p = 0; p < NumPatterns; p++)
    {
      Patterns[p] = new Pattern(numPixels);
    }
  }

  void Tick()
  {
    for (int p = 0; p < NumPatterns; p++)
    {
      Patterns[p]->Tick();
    }
  }
};

#endif

Pattern
#if !defined(_PATTERN_H)
#define _PATTERN_H

class Pattern {
public:

  Pattern(int numPixels){}

  void Tick(){}
};

#endif

The code compiles but when i run the full version (this is a simplified version of the code) i get some strange behavior, so i would like to know if i am doing the initialization of the Presets and Patterns correctly. 
Also when i try to print the Preset name in the LedDevice Tick function i get strange characters, so either i am not initializing the string correctly or the pointers to the Presets are wrong.
Can you tell if anything is not being done correctly in the constructors?

user10794: This is wrong:

Device device;

void setup() 
{
   device = Device();
}


I don't know what your intentions are, but the first line instantiates device. You don't need to do anything in setup here. It sort-of looks like you are trying to call the constructor but that is called when the class instance is created (in the first line).


Preset* Presets[];


What's this? A pointer to an array of zero length?


    *Presets = (Preset*)malloc(sizeof(Preset) * NumPresets);


Now you are allocating memory for a pointer to the array to be big enough to hold the contents of the array.


for (int p = 0; p < NumPresets; p++)
{
    Presets[p] = new Preset("test", NumPixels, NumPatterns);
}   


Now you are allocating memory for the contents of the array again.

There is too much dereferencing going on here. Please post a Minimal, Complete, and Verifiable example. For example, make the classes in question do not much. Then make code we can actually work on. 


I realize the first call is to the default constructor, but in the setup i do the real construction ...

Well, you can't call the constructor twice. What is commonly done is to do nothing (or almost nothing) in the constructor, and then make a begin function which you call in setup. This function can then do whatever is required at the proper time.

user24459: Three things that need to be different:
Pattern* Patterns[];

should be
Pattern** Patterns;

It's a pointer to some pointers to patterns, not a pointer to an array (which is basically a pointer itself so same basic idea but an array implies a known size).  
and
*Patterns = (Pattern*)malloc(sizeof(Pattern) * NumPatterns);

should be
Patterns = (Pattern*)malloc(sizeof(Pattern*) * NumPatterns);

*Patterns is the thing pointed to by Patterns which at this point is undefined. You want to set Patterns to the allocated address.
And we only need to allocate space for the pointers not the actual objects.
Your for loops then become
for (int p = 0; p < NumPatterns; p++)
{
  (Patterns+p) = new Pattern(numPixels);
}

You then have the same issues with Presets.
And finally 
LedDevice device;
void setup() {
  device = LedDevice(10, 10, 10);
}

should be
LedDevice device(10, 10, 10);
void setup() {      
}


23993: <p>i want to create a somewhat complex structure of classes in an arduino. The short story is: a Device has Presets, and each Preset has a Patterns. For that i created the following structure (i am only posting the important stuff, if something is missing let me know):</p>

<p><strong>INO file</strong></p>

<pre><code>#include "LedDevice.h"

LedDevice device;

void setup() {
  device = LedDevice(10, 10, 10);
}

void loop() {
  Serial.println(F("Ticking"));
  device.Tick();
  delay(500);
}
</code></pre>

<p><strong>LedDevice</strong></p>

<pre><code>#if !defined(_LEDDEVICE_H)
#define _LEDDEVICE_H

#include "Preset.h"
#include&lt;Arduino.h&gt;

class LedDevice {
public:
  Preset* Presets[];

  LedDevice(){}
  LedDevice(int numPixels, int numPresets, int numPatterns)
  {
    *Presets = (Preset*)malloc(sizeof(Preset) * numPresets);

    for (int p = 0; p &lt; numPresets; p++)
    {
      Presets[p] = new Preset("test", numPixels, numPatterns);
    }
  }

  void Tick()
  {
    Serial.print(F("Preset ")); Serial.println(Presets[0]-&gt;Name);
    Presets[0]-&gt;Tick();
  }
};

#endif  //_LEDDEVICE_H
</code></pre>

<p><strong>Preset</strong></p>

<pre><code>#if !defined(_PRESET_H)
#define _PRESET_H

#include "Pattern.h"

class Preset {
public:
  char *Name;
  int NumPatterns;
  Pattern* Patterns[];

  Preset(char* name, int numPixels, int numPatterns) : NumPatterns(numPatterns)
  {
    Name = (char *)malloc(strlen(name) + 1);
    strcpy(Name, name);

    *Patterns = (Pattern*)malloc(sizeof(Pattern) * NumPatterns);

    for (int p = 0; p &lt; NumPatterns; p++)
    {
      Patterns[p] = new Pattern(numPixels);
    }
  }

  void Tick()
  {
    for (int p = 0; p &lt; NumPatterns; p++)
    {
      Patterns[p]-&gt;Tick();
    }
  }
};

#endif
</code></pre>

<p><strong>Pattern</strong></p>

<pre><code>#if !defined(_PATTERN_H)
#define _PATTERN_H

class Pattern {
public:

  Pattern(int numPixels){}

  void Tick(){}
};

#endif
</code></pre>

<p>The code compiles but when i run the full version (this is a simplified version of the code) i get some strange behavior, so i would like to know if i am doing the initialization of the Presets and Patterns correctly. </p>

<p>Also when i try to print the Preset name in the LedDevice Tick function i get strange characters, so either i am not initializing the string correctly or the pointers to the Presets are wrong.</p>

<p>Can you tell if anything is not being done correctly in the constructors?</p>
 10794: <p>This is wrong:</p>

<blockquote>
<pre><code>Device device;

void setup() 
{
   device = Device();
}
</code></pre>
</blockquote>

<p>I don't know what your intentions are, but the first line instantiates <code>device</code>. You don't need to do anything in <code>setup</code> here. It sort-of looks like you are trying to call the constructor but that is called when the class instance is created (in the first line).</p>

<hr>

<blockquote>
<pre><code>Preset* Presets[];
</code></pre>
</blockquote>

<p>What's this? A pointer to an array of zero length?</p>

<hr>

<blockquote>
<pre><code>    *Presets = (Preset*)malloc(sizeof(Preset) * NumPresets);
</code></pre>
</blockquote>

<p>Now you are allocating memory for a <strong>pointer</strong> to the array to be big enough to hold the <strong>contents</strong> of the array.</p>

<hr>

<blockquote>
<pre><code>for (int p = 0; p &lt; NumPresets; p++)
{
    Presets[p] = new Preset("test", NumPixels, NumPatterns);
}   
</code></pre>
</blockquote>

<p>Now you are allocating memory for the contents of the array again.</p>

<hr>

<p>There is too much dereferencing going on here. Please post a <a href="https://stackoverflow.com/help/mcve">Minimal, Complete, and Verifiable example</a>. For example, make the classes in question do not much. Then make code we can actually work on. </p>

<hr>

<blockquote>
  <p>I realize the first call is to the default constructor, but in the setup i do the real construction ...</p>
</blockquote>

<p>Well, you can't call the constructor twice. What is commonly done is to do nothing (or almost nothing) in the constructor, and then make a <code>begin</code> function which you call in <code>setup</code>. This function can then do whatever is required at the proper time.</p>
 24459: <p>Three things that need to be different:</p>

<pre><code>Pattern* Patterns[];
</code></pre>

<p>should be</p>

<pre><code>Pattern** Patterns;
</code></pre>

<p>It's a pointer to some pointers to patterns, not a pointer to an array (which is basically a pointer itself so same basic idea but an array implies a known size).  </p>

<p>and</p>

<pre><code>*Patterns = (Pattern*)malloc(sizeof(Pattern) * NumPatterns);
</code></pre>

<p>should be</p>

<pre><code>Patterns = (Pattern*)malloc(sizeof(Pattern*) * NumPatterns);
</code></pre>

<p>*Patterns is the thing pointed to by Patterns which at this point is undefined. You want to set Patterns to the allocated address.
And we only need to allocate space for the pointers not the actual objects.</p>

<p>Your for loops then become</p>

<pre><code>for (int p = 0; p &lt; NumPatterns; p++)
{
  (Patterns+p) = new Pattern(numPixels);
}
</code></pre>

<p>You then have the same issues with Presets.</p>

<p>And finally </p>

<pre><code>LedDevice device;
void setup() {
  device = LedDevice(10, 10, 10);
}
</code></pre>

<p>should be</p>

<pre><code>LedDevice device(10, 10, 10);
void setup() {      
}
</code></pre>
