Is there some restrictions with malloc function calling in included CPP file?
I can run the same malloc with same parameters from INO file without problems, but there is problem with library CPP file.
malloc does not care the path by which it is called. The answer to your question is no.
What are the symptoms of the "problem"? Error messages?
There is no memory allocated really, therefore it returns 0.
I'll make test package.
Yes, often the process of stripping your program down to an example that can be posted on the forum will show you what the mistake is.
Here is fast test application.
Seems that there is problem with 2D arrays.
//test_malloc.ino
#include "test.h"
testm tm;
void setup() {
Serial.begin(57600);
Serial.println("start");
printBuf();
tm.testarray=(char*)malloc(100);
memset(tm.testarray, 0, 100);
tm.testarray1 = (char**)malloc(2 * sizeof(char*));
for (int i = 0; i < 2; i++)
{
tm.testarray1[i] = (char*)malloc(16 * sizeof(char));
memset(tm.testarray1[i], 0, 16);
}
printBuf();
}
void loop() {
}
void printBuf()
{
Serial.print("array:");
for (int i=0; i<10; i++)
{
Serial.print((uint8_t)tm.testarray[i]);
Serial.print(",");
}
Serial.println();
for (int j=0; j<2; j++)
{
Serial.print("array1-");Serial.println(j);
for (int i=0; i<10; i++)
{
Serial.print((uint8_t)tm.testarray1[j][i]);
Serial.print(",");
}
Serial.println();
}
}
//test.h
#ifndef TEST_h
#define TEST_h
#if (ARDUINO >= 100)
#include <Arduino.h>
#else
#include <WProgram.h>
#endif
class testm
{
public:
char* testarray;
char** testarray1;
testm();
void init();
};
#endif TEST_h
//test.cpp
#include "test.h"
testm::testm()
{
init();
}
void testm::init()
{
testarray=(char*)malloc(100);
memset(testarray, 0, 100);
testarray1 = (char**)malloc(2 * sizeof(char*));
for (int i = 0; i < 2; i++)
{
testarray1[i] = (char*)malloc(16 * sizeof(char));
memset(testarray1[i], 0, 16);
}
}
BTW, I can resume that using malloc() with Arduino isn't good way, I've found very simple and good way out, Static memory pre-allocation - https://www.quora.com/Why-is-malloc-harmful-in-embedded-systems
start
array:0,0,0,0,0,0,0,0,0,0,
array1-0
0,0,0,0,0,0,0,0,0,0,
array1-1
0,0,0,0,0,0,0,0,0,0,
array:0,0,0,0,0,0,0,0,0,0,
array1-0
0,0,0,0,0,0,0,0,0,0,
array1-1
0,0,0,0,0,0,0,0,0,0,
...is what I get.
That was my fault, somehow the test app wasn't working properly, now I get the main project working, there were mistake in my constructor.