Re-defining a string in array of string

Hi,

I wrote the following code that in each 0.5 seconds the string in an array of string is re-defined. The code works fine in the beginning, but after some time when I re-define the string position in the array of string (myStrings[k]=strdup(str);), the position of array of string is always equal to zero.

Could you can help me?

int led = 13;
long previousMillis = 0;
int ledState = LOW;
long interval = 1000;
char str[25];
int k=1;
int ll=1;
const int num=10;
char* myStrings[num];

void setup() {
Serial.begin(9600);
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
}

void loop() {
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;}

if (ledState == LOW){ledState = HIGH;
if (k==num+1){k=1;}
snprintf(str, sizeof(str), ", k value = %d", k);
Serial.print("ll value = ");Serial.print(ll);
myStrings[k]=strdup(str);
Serial.println(myStrings[k]);
ll=ll+1; k=k+1;}
else
{delay(500);ledState = LOW;}
}

array of strings ?? I cannot see that
I see pointers to an array of chars..

The problem is that strdup uses malloc to allocate space for the duplicate copy of the string. On a UNO, for example it has a total of 2kB ram which must have space for the stack and local variables. What's left over can be used by malloc but in less than 2kB it will rapidly run out of free memory and start returning NULL.

Pete

So, does anyone knows how to solve this problem?

char* myStrings[num];

Looks like an array of strings, to me.

OP: You keep allocating memory, but never freeing it. You have to expect to run out after a while.