Please explain why Pin13 and Serial.write misbehaving

I stripped my code to the minimum and I just don’t understand why I am getting pin 13 toggling and Serial.write not working in one case but not in the other.

I have three files: Debug the arduino sketch, Debug.h (the library header file) and Debug.cpp (the C++ library file)

With Debug.cpp as is shown below, I get pin 13 toggling on an UNO board and nothing is printed to serial out.
If I replace the “array[halfarraySize];” with “float array[halfarraySize];” in Debug.cpp, pin13 and the serial print behave properly. Can you please explain what I am doing wrong and/or what is going on?

Thanks in advance!

Debug sketch:

#include <Debug.h>

const int arraySize=118;

debugClass debug_object(arraySize);

void setup() {

  Serial.begin(115200);  

  Serial.write("test");

}

void loop() {
}

Debug.h:

#ifndef Debug_h
#define Debug_h

#include "Arduino.h"
class debugClass{

public: 
	debugClass(const int arraySize);

    // Keep as many variables private as possible
private:
    int halfarraySize;
    float array[];
};

#endif

Debug.cpp:

#include "Debug.h"
	
debugClass::debugClass(const int arraySize){
	const int halfarraySize = arraySize/2;
    // This needs to be declared or prints have issues
    array[halfarraySize];
    
	for(int i = 0; i < halfarraySize; i++){
		array[i] = 0;
	}
}

Just as a matter of interest, what is the code supposed to do apart from filling up half the array ?

UKHeliBob: Just as a matter of interest, what is the code supposed to do apart from filling up half the array ?

I'm trying to implement a circular buffer for some experiments

Would you expect to be able to define an array member field of unspecified size just by putting 'array;' in the body of the constructor? I would have thought you'd need to use a template to enable the size to be defined at instantiation time, or allocate the array dynamically. Perhaps what you're doing is legitimate, but it doesn't look that way to me; I suspect what you're actually doing is trampling over initialised memory that will be needed by the Arduino runtime later one.

I believe Peter is correct, look at this link, http://www.cplusplus.com/doc/tutorial/templates/ under Non-type parameters for templates

PeterH: Would you expect to be able to define an array member field of unspecified size just by putting 'array;' in the body of the constructor? I would have thought you'd need to use a template to enable the size to be defined at instantiation time, or allocate the array dynamically. Perhaps what you're doing is legitimate, but it doesn't look that way to me; I suspect what you're actually doing is trampling over initialised memory that will be needed by the Arduino runtime later one. [/quote]

I think you are referring to the Debug.h private declaration "float array[];" no?

You make a good point, and suspect your theory is correct. However, shouldn't the variable be declared in the header file? Perhaps my assumptions are incorrect (completely possible as I'm learning this as I go).

When you say the array size should be enabled at instantiation time, where would this be located?

Thanks for the help

template<int N>
class debugClass
{
private:
    const int   array_length;
    float       array[N];

public:
    debugClass() : array_length(N)
    {
        for ( int i = array_length; i--; )
        {
            array[i] = 0;
        }
    }
};

debugClass <12>    debug;

lloyddean:

template<int N>

class debugClass
{
private:
    const int   array_length;
    float       array[N];

public:
    debugClass() : array_length(N)
    {
        for ( int i = array_length; i–; )
        {
            array[i] = 0;
        }
    }
};

debugClass <12>    debug;

First of all, thank you for kindly writing this. Would this code be placed in Debug.h?
I’m reading up on Templates, I was not aware of them.

Except for that last line yes as the last line instantiates and instance with an array length of 12 named 'debug' - place that in an implementation file where used. Adjust array length as necessary.

Sorry but I can’t wrap my head around this yet. I’m still studying templates and dynamic memory to understand what is needed.I tried using templates but am getting errors

Debug sketch:

#include <Debug.h>

const int arraySize=118;

//debugClass debug_object(arraySize);
debugClass <arraySize> debug_object;
void setup() {

  Serial.begin(115200);  

  Serial.write("test");

}

void loop() {
}

Debug.h:

#ifndef Debug_h
#define Debug_h

#include "Arduino.h"

template<int N>
class debugClass
{
private:
    const int   array_length;
    float       array[N];
    
public:
    debugClass() : array_length(N)
    {
        for ( int i < array_length/2; i--; )
        {
            array[i] = 0;
        }
    }
};

#endif

Debug.cpp:

#include "Debug.h"
	
debugClass::debugClass(){

}

Errors:

In file included from Debug.ino:1:
/Applications/Arduino.app/Contents/Resources/Java/libraries/Debug/Debug.h: In constructor 'debugClass<N>::debugClass()':
/Applications/Arduino.app/Contents/Resources/Java/libraries/Debug/Debug.h:16: error: expected initializer before '<' token
/Applications/Arduino.app/Contents/Resources/Java/libraries/Debug/Debug.h:16: error: 'i' was not declared in this scope

Sorry I hadn’t seen your question earlier -

template<int N>
class debugClass
{
    const int   halfarraySize;
    float       array[N / 2];

public:
    debugClass() : halfarraySize(N / 2)
    {
        for ( int i = halfarraySize; i--; )
        {
            array[i] = 0;
        }
    }
};

const int arraySize = 118;

debugClass<arraySize> debug_object;

void setup()
{
    Serial.begin(115200);  
    Serial.write("test");
}

void loop()
{}