Hi, I am trying to use a pointer that is a vector of char, I was passing the vector by parameter and it seems that this was consuming the memory because I passed it to 3 functions (one calling inside the another) to try to solve the problem I thought of passing a pointer reference to use the same variable in the 3 functions, but I'm having problems populating the vector and the system prints only one character.
The code I'm just using for learning is this:
void setup()
{
Serial.begin(9600);
}
void loop()
{
criaVariavel();
delay(10000);
}
void criaVariavel() {
char* vetor;
dadosVetor(&vetor);
Serial.println(vetor);
delete[] vetor;
free(vetor);
}
void dadosVetor(char** vetor2) {
*vetor2 = new char[10];
for(int i=0; i<9; i++) {
*vetor2[i] = 'a';
}
*vetor2[9] = '\0';
}
In println it only shows 'a' instead of 'aaaaaaaaa', this may be because of the way I'm setting the value in the variable within the for?
Is it possible to reference the same pointer to another function from the DataVector()?
Regarding memory usage, is this correct the way I did to release the variable using delete[] and free()?