Not sure of correct syntax to return an array of integers.
Example:
'''
int myarray[3];
int setarray() {
int tmp[3];
.
.
.
return tmp;
}
'''
Not sure of correct syntax to return an array of integers.
Example:
'''
int myarray[3];
int setarray() {
int tmp[3];
.
.
.
return tmp;
}
'''
Be very careful what you wish for - your array goes out of scope when the function returns.
What do you want to do?
instead of passing an array which may make copies and increase ram use, why not refer to the array with a pointer?
Here I make a global array of 60 members.
const int BufferCount = 60;
float CollectionPressure[BufferCount] = {0.0f};
This function makes use of the global array,
void fProcessAirPressure ( void *pvParemeters )
{
int Ticks = 118; // Tick counter
bool Filled = false; // array has been filled before?
float *ptr = CollectionPressure; // pointer to the array
const int ticksTrigger = 120; // triggered at 1 minute intervals
for (;;)
{
//triggered by BME which is triggered by the 1 minute hardware timer.
xEventGroupWaitBits (eg, evtStoreAirPressure, pdTRUE, pdTRUE, portMAX_DELAY );
xSemaphoreTake( sema_CollectPressure, portMAX_DELAY );
xSemaphoreTake ( sema_eData, portMAX_DELAY );
if ( !Filled )
{
//if array has not been filled before, fill array with the same base value
for ( int j = 0; j < BufferCount; j++ )
{
*( ptr + j ) = x_eData.oPressure;
}
Filled = true;// array has been initially filled
} else {
if ( Ticks == ticksTrigger )
{
//when tick counter reaches the trigger level
//shift contents left and insert new value at the end
for ( int i = 0; i <= BufferCount - 2; i++ )
{
*( ptr + i ) = *( ptr + (i + 1) );
}
}
*( ptr + (BufferCount - 1) ) = x_eData.oPressure;//new value to be inserted
}
// find and store highest and lowest value
if ( x_eData.oPressure > x_eData.PressureH )
{
x_eData.PressureH = x_eData.oPressure;
}
if ( x_eData.oPressure < x_eData.PressureL )
{
x_eData.PressureL = x_eData.oPressure;
}
Ticks++;
if ( Ticks > ticksTrigger )
{
Ticks = 1;
}
//log_i( "ticks %d" , Ticks );
x_eData.cngPress = CalculatePressureFactors( *( ptr + 57), *( ptr + 59) ); // going back 4 hours
xSemaphoreGive( sema_eData );
xSemaphoreGive( sema_CollectPressure );
//
//log_i( " high watermark % d", uxTaskGetStackHighWaterMark( NULL ) );
} //for (;;)
vTaskDelete( NULL );
} //void fStoreAirPressure ( void *pvParemeters )
////
by writing to the array's memory locations.
And in this snippet from another function makes use of the global array.
void fDoTheDisplayThing( void * parameter )
{
float *ptr = CollectionPressure;
int BaseLine = (int) * ptr;
int offsetX = 0;
for ( int j = 0; j < BufferCount; j++ )
{
if ( *(ptr + j) != 0.0f )
{
int yAdj = BaseLine - (int) * (ptr + j);
display.setCursor( CurrentX + offsetX, CurrentY + yAdj );
display.print( "-" );
offsetX += 5;
// log_i( "pressure %f item %d", CollectionPressure[j], j );
}
}
I dont want to pass the array TO a subroutine, I want to create an array in the subroutine and then return it to the caller. There are small arrays any many of them to be called.
No, don't tell us how you think something should be done, tell us what you want to do.
of course you can "create" an array using the malloc()
. then how is the memory allocated for the array freed before running out of memory.
why is it necessary for the subroutine to "create" an array? or do you mean that you want the subroutine to return a set of values? what do you plan on doing with the array after processing the values it contains?
No, you can't do that with standard (static memory) arrays. You only have to allocate a dynamic memory to the newly created array and return the pointer to it. But in this case you will MUST to free this memory after using the array.
"static" is absolutely the attribute you must have.
int *
setArray (void)
{
static int myarray [3];
return & myarray [0];
}
@anon56112670 , @gcjr
With local static array in the function you can't create a several different arrays with some calls of the function.
But as far I understand, OP want exactly that.
it's confusing what he means by
Clarity - do you want a new array created every time, or are you processing the same array every time, but thinking you need to create it? If processing the same 'bag of nuts' every time is what you want, then the static data approach will suffice. If you want a new 'bag' every time (for example, because you may collect several before you process any), then malloc() is what you need.
EXCEPT that, you really don't want to be doing dynamic memory allocation in the memory-scarce environment of most of the Arduino products, because you very quickly run out of memory, and there is no 'garbage collection' in the Arduino. Memory quickly becomes fragmented(this also plagues the String class, by the way), and your program dies unpredictably.
There was just a long discussion about this:
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.