Issue with the use of pointers in a function/Problema con el uso de punteros en

Hi Evevybody!!

I got a question about use of pointers (see the bottom part in the code, between //////////////). I’m trying to return an array called vDec of the funcion controlSensor.

But for any reason that I don’t know, happens some weird with the method Serial.print(); (the line code where say //EN ESTA PARTE). For example, if comment the Serial.print(), the values returned are random; neverthless if I let the method how is right now, the code returns the correct values.

someone could explain to me what is happening? D=

I would appreciate the help.

thanks :smiley:

Tengo una duda sobre el uso de punteros (mirar la parte de abajo del codigo la que esta entre //////). Estoy intentando devolver un array llamado vDec de la funcion controlSensor.

Pero por alguna cosa que no se, me sucede algo extraño con el metodo Serial.print();(la linea donde dice //EN ESTA PARTE). Por ejemplo, si comento el Serial.print(), los valores regresados por la funcion Controlsensor son erroneos; pero si lo dejo como esta, el codigo me devuelve los valores correctos.

Alguien podria explicarme que estoy haciendo mal.

Se los agradeceria mucho!!
saludos!

void loop()
{
  //fill the matrix A.
  for (int i = 0; i < 3; i++) {
    vector = controlSensor(stateS[i], sensor[i], npots[i]);
    for (int j = 0; j < 11; j++) {
      A[i][j] = *(vector + j);
      Serial.print(A[i][j]);
      Serial.print(" ");
    }
    Serial.println();
  }
      Serial.println();
  delay(3000);

}

int* controlSensor(int stateS1, const uint8_t pinSensor, uint8_t ns) {
  npots[ns-1]=ns;
  nstatus[ns-1]=ns+1;
  rpm=ns+2;
  v=ns+3;
  nbreaks[ns-1]=ns*10;
  instantTime[0] = 1*ns;
  instantTime[1] = 2*ns;
  instantTime[2] = 3*ns;
  instantTime[3] = 4*ns;
  instantTime[4] = 5*ns;
  totalInstantTime = instantTime[0] + instantTime[1] + instantTime[2] + instantTime[3] + instantTime[4];

//////////////////////////////////////////////////////////////////////////////////////  
  int vDesc[11] = {npots[ns-1],nstatus[ns - 1], rpm, v, nbreaks[ns - 1], instantTime[0], instantTime[1], instantTime[2], instantTime[3], instantTime[4], totalInstantTime};
 
  Serial.print(""); ///ES ESTA PARTE
  return vDesc;
//////////////////////////////////////////////////////////////////////////////////////////////
}

Can you create the array in the calling function, pass the address of that to the function and have it modify the array there?

Otherwise, you're going to be trying to return a pointer to an array that exists on the stack.

Blackfin:
Can you create the array in the calling function, pass the address of that to the function and have it modify the array there?

Otherwise, you're going to be trying to return a pointer to an array that exists on the stack.

Yeah, well, I did! Let me explain to you my problem.
I get the correct values with this function. I return an array, vDesc, and assign it to my variable called vector (was declareted as int* vector), however, the problem occurs when in my function, controlSensor, specifically in this part, where says "ES ESTA PARTE"

T//////////////////////////////////////////////////////////////////////////////////////
int vDesc[11] = {npots[ns-1],nstatus[ns - 1], rpm, v, nbreaks[ns - 1], instantTime[0], instantTime[1], instantTime[2], instantTime[3], instantTime[4], totalInstantTime};

Serial.print(""); ///ES ESTA PARTE
return vDesc;
//////////////////////////////////////////////////////////////////////////////////////////////

When I delete the method Serial.print(); I obtain random values, but if I keep the Serial.print(); I get correct values

I don't know why happen that D=

Thank you for answering me

regards!!!

Post complete code.

Read How to use this forum - please read, specifically point 7 how to post code.

Your problem sounds like writing outside the boundaries of an array.

sterretje:
Post complete code.

Read How to use this forum - please read, specifically point 7 how to post code.

Your problem sounds like writing outside the boundaries of an array.

Yes of course :D, this is the code! thank you for your help

//constants
const byte n = 3; //number of pots
const byte col = 11;
uint8_t num = 3; //counter 
const uint8_t sensor[3] = {A3, A7, A6};
int* vector;
int A[n][col];
uint16_t stateS[n];
uint8_t npots[n]; 
uint8_t nstatus[n];
float v=0, w=0;
uint16_t rpm = 0;
uint16_t instantTime[5];
uint16_t nbreaks[n];
uint16_t totalInstantTime=0;

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
 for (int i = 0; i < num; i++) {
    stateS[i] = 0;
    npots[i] = i + 1; 
    nbreaks[i]=0;
    nstatus[i]=0;
 }
 
   for(int i=0; i<3; i++){
    for(int j=0;j<11;j++){
      A[i][j]=0;
    }
  }

}

void loop()
{

  //fill the matrix A.
  for (int i = 0; i < 3; i++) {
    vector = controlSensor(stateS[i], sensor[i], npots[i]);
    for (int j = 0; j < 11; j++) {
      A[i][j] = *(vector + j);
      Serial.print(A[i][j]);
      Serial.print(" ");
    }
    Serial.println();
  }
      Serial.println();
  delay(3000);

}

int* controlSensor(int stateS1, const uint8_t pinSensor, uint8_t ns) {
  npots[ns-1]=ns;
  nstatus[ns-1]=ns+1;
  rpm=ns+2;
  v=ns+3;
  nbreaks[ns-1]=ns*10;
  instantTime[0] = 1*ns;
  instantTime[1] = 2*ns;
  instantTime[2] = 3*ns;
  instantTime[3] = 4*ns;
  instantTime[4] = 5*ns;
  totalInstantTime = instantTime[0] + instantTime[1] + instantTime[2] + instantTime[3] + instantTime[4];
//////////////////////////////////////////////////////////////////////////////  
  int vDesc[11] = {npots[ns-1],nstatus[ns - 1], rpm, v, nbreaks[ns - 1], instantTime[0], instantTime[1], instantTime[2], instantTime[3], instantTime[4], totalInstantTime};
  Serial.print("");// NOTE: IF YOU COMMENT THIS HAPPENS SOME WIERD
  return vDesc;
  //////////////////////////////////////////////////
}

As @Blackfin says.

Your vDesc array ceases to exists at the moment that you leave the controlSensor function and the pointer now points to memory that is free for the processor to use for other things. By the time you try to print from your for-loop, the processor has done exactly that.

Make your vDesc array static (or global) or create it in loop() and pass it as a parameter to controlSensor.

But

sterretje:
Your vDesc array ceases to exists at the moment that you leave the controlSensor function and the pointer now points to memory that is free for the processor to use for other things. By the time you try to print from your for-loop, the processor has done exactly that.

So the behavior is accidental, the Serial.print(); prevents the heap from being released straight away because Serial.print() want's to hold onto to something in there for just a moment !? is that's what's going on ? or is there another explanation for the values still being available if the Serial.print() is called at the end of the function ?

Deva_Rishi:
But ... So the behavior is accidental, the Serial.print(); prevents the heap from being released straight away because Serial.print() want's to hold onto to something in there for just a moment !? is that's what's going on ? or is there another explanation for the values still being available if the Serial.print() is called at the end of the function ?

Not the heap but the stack. Values stored in the memory making up the stack aren't necessarily erased when a function returns; a stack pointer is moved, values retrieved from it etc but the values on the stack will basically be the same following the return.

The problem is there's no assurance of this. An interrupt may occur, subsequent operations could make use of the stack (creating their own auto variables, calling subroutines etc) so the stack can't be considered a save place to create an array of data a pointer to which you want to return to the calling function.

It'd be much better and determinant to use a global declaration or allocate/free memory as required and have the function manipulate it directly (the former case) or via a pointer (the latter case.)