user4530: I am having trouble getting my head around pointers in C++, specifically how they behave when passed around or used within objects. As far as my understanding goes as long as pointers, or more specifically values/objects pointed to - do not go out of scope, I should be able to use that pointer.
I have been playing around with some test code on my Arduino Uno and I am bumping into behaviour I cannot explain.
I have read rather a lot on the topic of pointers in C++ and thought I had a good grasp of it. However, practice doesn't seem to match the theory in my head. I don't know if this is a lack of understanding or if the Arduino platform has idiosyncrasies that I am not aware of.
In my test code I have created two classes, Alpha and Beta. Alpha simply holds a C-type string in a member variable called alpha. Beta is designed to hold two Alpha instances in an array*. 
*While this is "toy" code, the general structure mirrors my actual project. I have used this toy code as it more succinctly describes the set up and the issue I am facing.
pointers.h
#import "Arduino.h"
class Alpha {
    private:
        char alpha[20];
    public:
        Alpha();
        ~Alpha();
        char* getAlpha(char* s, int size=20);
        void setAlpha(char* value, int size=20);
        void debug();
};

class Beta {
    private:
        Alpha** alphas;
    public:
        Beta();
        ~Beta();
        Alpha* getAlpha(int index);
        void setAlpha(Alpha* alpha, int index);
        void doStuffWithAlpha(int index);
};

pointers.cpp
#import "pointers.h"

Alpha::Alpha() {
    setAlpha("Initial");
}

Alpha::~Alpha() {}

char* Alpha::getAlpha(char* s, int size/*=20*/) {
    *s = *alpha;
    return s; //return not strictly needed here since s is passed in
}

void Alpha::setAlpha(char* value, int size/*=20*/) {
    strncpy(alpha, value, size-1);
    alpha[size-1] = '\0';
}

void Alpha::debug() {
    Serial.println(alpha);
}


Beta::Beta() {
    alphas = new Alpha*[2];
}

Beta::~Beta() {}

Alpha* Beta::getAlpha(int index) {
    return alphas[index];
}

void Beta::setAlpha(Alpha* alpha, int index) {
    alphas[index] = alpha;
}

void Beta::doStuffWithAlpha(int index) {
    Alpha* alpha = getAlpha(index);
    /* Alpha* alpha = alphas[index]; */ //This doesn't work either - same outcome.
    alpha->debug();
}

sketch.ino
#import "pointers.h"
Alpha a1;
Alpha a2;
Beta b1;

void setup() {
    Serial.begin(9600);
    Serial.println("SETUP()");
    Serial.println();

    a1 = Alpha();
    a1.setAlpha("Hello");

    Serial.println("----- a1.debug -----");
    Serial.println("Expected: Hello");
    Serial.print("Actual: "); a1.debug();
    Serial.println();

    a2 = Alpha();
    a2.setAlpha("Goodbye");

    Serial.println("----- a2.debug -----");
    Serial.println("Expected: Goodbye");
    Serial.print("Actual: "); a2.debug();
    Serial.println();

    Beta b1 = Beta();
    b1.setAlpha(&a1, 0);
    b1.setAlpha(&a2, 1);

    Serial.println("----- b1.doStuffWithAlpha -----");
    Serial.println("Expected: Hello");
    Serial.print("Actual: "); b1.doStuffWithAlpha(0);
    Serial.println("Expected: Goodbye");
    Serial.print("Actual: "); b1.doStuffWithAlpha(1);
    Serial.println();
}

void loop() {
    Serial.println("LOOP()");
    Serial.println();

    Serial.println("----- a1.debug -----");
    Serial.println("Expected: Hello");
    Serial.print("Actual: "); a1.debug();
    Serial.println();

    Serial.println("----- b1.doStuffWithAlpha(0) ------");
    Serial.println("Expected: Hello");
    Serial.print("Actual: "); b1.doStuffWithAlpha(0);
    Serial.println();

    Serial.println("----- a2.debug -----");
    Serial.println("Expected: Goodbye");
    Serial.print("Actual:   "); a2.debug();
    Serial.println();

    Serial.println("----- b1.doStuffWithAlpha(1) ------");
    Serial.println("Expected: Goodbye");
    Serial.print("Actual:   "); b1.doStuffWithAlpha(1);
    Serial.println();

    delay(10000);
}

This sketch is quite straight-forward. It creates 2 Alpha objects and a Beta object and passes the two Alphas into Beta. The myriad Serial.print() statements produces the output below.
output
SETUP()

----- a1.debug -----
Expected: Hello
Actual:   Hello

----- a2.debug -----
Expected: Goodbye
Actual:   Goodbye

----- b1.doStuffWithAlpha -----
Expected: Hello
Actual:   Hello

Expected: Goodbye
Actual:   Goodbye

//everything is fine up to this point...

LOOP()

----- a1.debug -----
Expected: Hello
Actual:   Hello

----- b1.doStuffWithAlpha(0) ------
Expected: Hello
Actual:   CÃoçëyc=Lä^ÜŽYw¸Nv†Ý¿  //uh oh...

----- a2.debug -----
Expected: Goodbye
Actual:   Goodbye

----- b1.doStuffWithAlpha(1) ------
Expected: Goodbye
Actual:   ÿß/ï //uh oh again...

Everything works as expected when running a1 or a2.debug() or b1.doStuffWithAlpha() in the setup() function. 
The result of a1.debug() and a2.debug() works as expected in the loop() function.
The issue, as you can see, is when running b1.doStuffWithAlpha(0) or b1.doStuffWithAlpha(1). The output is usually junk, which suggests to me the pointer has been mangled and points in the wrong place.
I don't understand what is happening between the transition from setup() to loop(). At first I thought the issue was the "Hello" and "Goodbye" strings going out of scope, but Alpha::setAlpha() makes a copy of the C-string. Also, accessing Alpha::debug() in loop() works, as shown above.
I have tried variations on the above code, such as using a struct or an int for alpha instead of a C-string and the results are the same: Mangled values.
Have I done something obviously silly that my scant C++ experience hasn't picked up on, or is there some fundamental Arduino shenanigans at play?

user3825: Problem is local shadow variable b1 in setup() function declared as Beta b1 = Beta();. In loop() is considered uninitialized global variable b1.

4530: <p>I am having trouble getting my head around pointers in C++, specifically how they behave when passed around or used within objects. As far as my understanding goes as long as pointers, or more specifically values/objects pointed <em>to</em> - do not go out of scope, I should be able to use that pointer.</p>

<p>I have been playing around with some test code on my Arduino Uno and I am bumping into behaviour I cannot explain.</p>

<p>I have read rather a lot on the topic of pointers in C++ and thought I had a good grasp of it. However, practice doesn't seem to match the theory in my head. I don't know if this is a lack of understanding or if the Arduino platform has idiosyncrasies that I am not aware of.</p>

<p>In my test code I have created two classes, <code>Alpha</code> and <code>Beta</code>. Alpha simply holds a C-type string in a member variable called <code>alpha</code>. <code>Beta</code> is designed to hold two <code>Alpha</code> instances in an array*. </p>

<p>*While this is "toy" code, the general structure mirrors my actual project. I have used this toy code as it more succinctly describes the set up and the issue I am facing.</p>

<p><strong>pointers.h</strong></p>

<pre><code>#import "Arduino.h"
class Alpha {
    private:
        char alpha[20];
    public:
        Alpha();
        ~Alpha();
        char* getAlpha(char* s, int size=20);
        void setAlpha(char* value, int size=20);
        void debug();
};

class Beta {
    private:
        Alpha** alphas;
    public:
        Beta();
        ~Beta();
        Alpha* getAlpha(int index);
        void setAlpha(Alpha* alpha, int index);
        void doStuffWithAlpha(int index);
};
</code></pre>

<p><strong>pointers.cpp</strong></p>

<pre><code>#import "pointers.h"

Alpha::Alpha() {
    setAlpha("Initial");
}

Alpha::~Alpha() {}

char* Alpha::getAlpha(char* s, int size/*=20*/) {
    *s = *alpha;
    return s; //return not strictly needed here since s is passed in
}

void Alpha::setAlpha(char* value, int size/*=20*/) {
    strncpy(alpha, value, size-1);
    alpha[size-1] = '\0';
}

void Alpha::debug() {
    Serial.println(alpha);
}


Beta::Beta() {
    alphas = new Alpha*[2];
}

Beta::~Beta() {}

Alpha* Beta::getAlpha(int index) {
    return alphas[index];
}

void Beta::setAlpha(Alpha* alpha, int index) {
    alphas[index] = alpha;
}

void Beta::doStuffWithAlpha(int index) {
    Alpha* alpha = getAlpha(index);
    /* Alpha* alpha = alphas[index]; */ //This doesn't work either - same outcome.
    alpha-&gt;debug();
}
</code></pre>

<p><strong>sketch.ino</strong></p>

<pre><code>#import "pointers.h"
Alpha a1;
Alpha a2;
Beta b1;

void setup() {
    Serial.begin(9600);
    Serial.println("SETUP()");
    Serial.println();

    a1 = Alpha();
    a1.setAlpha("Hello");

    Serial.println("----- a1.debug -----");
    Serial.println("Expected: Hello");
    Serial.print("Actual: "); a1.debug();
    Serial.println();

    a2 = Alpha();
    a2.setAlpha("Goodbye");

    Serial.println("----- a2.debug -----");
    Serial.println("Expected: Goodbye");
    Serial.print("Actual: "); a2.debug();
    Serial.println();

    Beta b1 = Beta();
    b1.setAlpha(&amp;a1, 0);
    b1.setAlpha(&amp;a2, 1);

    Serial.println("----- b1.doStuffWithAlpha -----");
    Serial.println("Expected: Hello");
    Serial.print("Actual: "); b1.doStuffWithAlpha(0);
    Serial.println("Expected: Goodbye");
    Serial.print("Actual: "); b1.doStuffWithAlpha(1);
    Serial.println();
}

void loop() {
    Serial.println("LOOP()");
    Serial.println();

    Serial.println("----- a1.debug -----");
    Serial.println("Expected: Hello");
    Serial.print("Actual: "); a1.debug();
    Serial.println();

    Serial.println("----- b1.doStuffWithAlpha(0) ------");
    Serial.println("Expected: Hello");
    Serial.print("Actual: "); b1.doStuffWithAlpha(0);
    Serial.println();

    Serial.println("----- a2.debug -----");
    Serial.println("Expected: Goodbye");
    Serial.print("Actual:   "); a2.debug();
    Serial.println();

    Serial.println("----- b1.doStuffWithAlpha(1) ------");
    Serial.println("Expected: Goodbye");
    Serial.print("Actual:   "); b1.doStuffWithAlpha(1);
    Serial.println();

    delay(10000);
}
</code></pre>

<p>This sketch is quite straight-forward. It creates 2 <code>Alpha</code> objects and a <code>Beta</code> object and passes the two Alphas into Beta. The myriad <code>Serial.print()</code> statements produces the output below.</p>

<p><strong>output</strong></p>

<pre><code>SETUP()

----- a1.debug -----
Expected: Hello
Actual:   Hello

----- a2.debug -----
Expected: Goodbye
Actual:   Goodbye

----- b1.doStuffWithAlpha -----
Expected: Hello
Actual:   Hello

Expected: Goodbye
Actual:   Goodbye

//everything is fine up to this point...

LOOP()

----- a1.debug -----
Expected: Hello
Actual:   Hello

----- b1.doStuffWithAlpha(0) ------
Expected: Hello
Actual:   CÃoçëyc=Lä^ÜŽYw¸Nv†Ý¿  //uh oh...

----- a2.debug -----
Expected: Goodbye
Actual:   Goodbye

----- b1.doStuffWithAlpha(1) ------
Expected: Goodbye
Actual:   ÿß/ï //uh oh again...
</code></pre>

<p>Everything works as expected when running <code>a1</code> or <code>a2.debug()</code> or <code>b1.doStuffWithAlpha()</code> in the <code>setup()</code> function. </p>

<p>The result of <code>a1.debug()</code> and <code>a2.debug()</code> works as expected in the <code>loop()</code> function.</p>

<p>The issue, as you can see, is when running <code>b1.doStuffWithAlpha(0)</code> or <code>b1.doStuffWithAlpha(1)</code>. The output is usually junk, which suggests to me the pointer has been mangled and points in the wrong place.</p>

<p>I don't understand what is happening between the transition from <code>setup()</code> to <code>loop()</code>. At first I thought the issue was the "Hello" and "Goodbye" strings going out of scope, but <code>Alpha::setAlpha()</code> makes a copy of the C-string. Also, accessing <code>Alpha::debug()</code> in <code>loop()</code> works, as shown above.</p>

<p>I have tried variations on the above code, such as using a struct or an int for <code>alpha</code> instead of a C-string and the results are the same: Mangled values.</p>

<p>Have I done something obviously silly that my scant C++ experience hasn't picked up on, or is there some fundamental Arduino shenanigans at play?</p>
 3825: <p>Problem is local shadow variable <code>b1</code> in setup() function declared as <code>Beta b1 = Beta();</code>. In <code>loop()</code> is considered uninitialized global variable <code>b1</code>.</p>
